Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1110749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:26:30+00:00 2026-05-17T02:26:30+00:00

How would I tell the WCF service what KnownTypes to use when passing data

  • 0

How would I tell the WCF service what KnownTypes to use when passing data back to the client?

I know I can use the [ServiceKnownType] attribute, which makes the service call run fine from a WCF Test Server, however it still fails from the client. Am I missing something here?

[OperationContract]
[ServiceKnownType(typeof(SubClassA))]
[ServiceKnownType(typeof(SubClassB))]
BaseClassZ GetObject();

Error message from client is:

{“Element
‘http://schemas.datacontract.org/2004/07/BaseClassZ’
contains data from a type that maps to
the name
‘http://schemas.datacontract.org/2004/07/SubClassA’.
The deserializer has no knowledge of
any type that maps to this name.
Consider using a DataContractResolver
or add the type corresponding to
‘SubClassA’ to the list of known types
– for example, by using the KnownTypeAttribute attribute or by
adding it to the list of known types
passed to DataContractSerializer.”}

Serializing/Deserializing the object on the WCF server using a DataContractSerializer and a list of KnownTypes works fine.

UPDATE: It appears I can get the client to read the object correctly if I add KnownType attributes to the base class, but I am still looking for a way around this if possible since the base class is used for a lot of items and I don’t want to have to modify the KnownType attributes on the base class anytime I add a new item.

[DataContract]
[KnownType(typeof(SubClassA))]
[KnownType(typeof(SubClassB))]
public class BaseClassZ 
{
    ...
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T02:26:31+00:00Added an answer on May 17, 2026 at 2:26 am

    To avoid deterring your service code put the known types into web.config of the service:

    <system.runtime.serialization>
        <dataContractSerializer>
            <declaredTypes>
                <add type="SomeNs.BaseClassZ, SomeAssembly">
                    <knownType type="SomeNs.SubClassA, SomeAssembly" />
                    <knownType type="SomeNs.SubClassB, SomeAssembly" />
                </add>
            </declaredTypes>
        </dataContractSerializer>
    </system.runtime.serialization>
    

    If you want to do it by code you need to use this attribute on the service interface and not on the operation method but I would prefer the declarative approach:

    [ServiceContract]
    [ServiceKnownType(typeof(SubClassA))]
    [ServiceKnownType(typeof(SubClassB))]
    public interface IFoo
    {
        [OperationContract]
        BaseClassZ GetObject();
    }
    

    UPDATE:

    I’ve put up a sample project illustrating the use of web.config to configure known types which is my preferred approach. And another sample project demonstrating the second approach.


    UPDATE 2:

    After looking at your updated code with the Silverlight application client we notice the following definition:

    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
    [System.ServiceModel.ServiceContractAttribute(ConfigurationName="ServiceReference1.IService1")]
    public interface IService1 {
    
        [System.ServiceModel.OperationContractAttribute(AsyncPattern=true, Action="http://tempuri.org/IService1/GetMany", ReplyAction="http://tempuri.org/IService1/GetManyResponse")]
        System.IAsyncResult BeginGetMany(System.AsyncCallback callback, object asyncState);
    
        System.Collections.ObjectModel.ObservableCollection<MyCommonLib.BaseClassZ> EndGetMany(System.IAsyncResult result);
    
        [System.ServiceModel.OperationContractAttribute(AsyncPattern=true, Action="http://tempuri.org/IService1/GetSingle", ReplyAction="http://tempuri.org/IService1/GetSingleResponse")]
        [System.ServiceModel.ServiceKnownTypeAttribute(typeof(MyCommonLib.SubClassA))]
        [System.ServiceModel.ServiceKnownTypeAttribute(typeof(MyCommonLib.SubClassB))]
        System.IAsyncResult BeginGetSingle(System.AsyncCallback callback, object asyncState);
    
        MyCommonLib.BaseClassZ EndGetSingle(System.IAsyncResult result);
    }
    

    Notice how the BeginGetSingle method contains the known type attributes while the BeginGetMany method doesn’t. In fact those attributes should be placed on the service definition so that the class looks like this.

    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
    [System.ServiceModel.ServiceContractAttribute(ConfigurationName="ServiceReference1.IService1")]
    [System.ServiceModel.ServiceKnownTypeAttribute(typeof(MyCommonLib.SubClassA))]
    [System.ServiceModel.ServiceKnownTypeAttribute(typeof(MyCommonLib.SubClassB))]
    public interface IService1
    {
        [System.ServiceModel.OperationContractAttribute(AsyncPattern=true, Action="http://tempuri.org/IService1/GetMany", ReplyAction="http://tempuri.org/IService1/GetManyResponse")]
        System.IAsyncResult BeginGetMany(System.AsyncCallback callback, object asyncState);
    
        System.Collections.ObjectModel.ObservableCollection<MyCommonLib.BaseClassZ> EndGetMany(System.IAsyncResult result);
    
        [System.ServiceModel.OperationContractAttribute(AsyncPattern=true, Action="http://tempuri.org/IService1/GetSingle", ReplyAction="http://tempuri.org/IService1/GetSingleResponse")]
        System.IAsyncResult BeginGetSingle(System.AsyncCallback callback, object asyncState);
    
        MyCommonLib.BaseClassZ EndGetSingle(System.IAsyncResult result);
    }
    

    As this is an autogenerated class there could be a bug in the SLsvcUtil.exe and svcutil.exe as it exhibits the same behavior. Putting the known type attributes on their correct place solves the problem. The problem is that this class is autogenerated by a tool and if you try to regenerate it from the WSDL it will mess up again.

    So it seems that if you have the following service definition:

    [ServiceContract]
    [ServiceKnownType(typeof(SubClassA))]
    [ServiceKnownType(typeof(SubClassB))]
    public interface IService1
    {
        [OperationContract]
        BaseClassZ[] GetMany();
    
        [OperationContract]
        BaseClassZ GetSingle();
    }
    

    And the 3 data contracts used here are shared between the client and the server when importing the definition of the service, the method that returns a collection doesn’t get the correct known type attributes in the generated client proxy. Maybe this is by design.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We are trying to create a proxy class that would serve the WCF service
I have a WCF service with many methods. I would like that after executing
When sending messages from a self hosted WCF service to many clients (about 10
I have been working on a Silverlight app that consumes a WCF service. [on
Is anybody familiar with setting up WCF-nettcp adapters for BTS? When I create a
We use BigIP to load balance between our two IIS servers. We recently deployed
I am in the process of building my first iPhone app (xCode,objective-c) which has
I've seen several similar scenarios explained here but not my particular one. I wonder
I do most of my work with Microsoft technologies these days, so naturally I'm
I am presently designing a message queue system that will be used by various

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.