none
How to resolve the wcf data contract problem RRS feed

  • Question

  • Hi All ,

    I have two data contracts of same contents in two different  namespaces. i have copied one datacontract to another and

    passed to a particular method. but it is giving the below error and throwing exception. it is not going into that method.

    Please let me know any ideas /suggestions on how to resolve this.Appreciate your help:

    Exception errror:

    {"Type 'System.Collections.Generic.List`1[[GlobalWcfServiceLib.TopicDetailsInfo, GlobalWcfContracts, Version=1.2.2.0, Culture=neutral,

     PublicKeyToken=17c64733a9775004]]' with data contract name 'ArrayOfTopicDetailsInfo:http://CName.GlobalService/11.1/2010/11' 
     is not expected. Consider using a DataContractResolver or add any types not known statically to the list of
      known types - for example, by using the KnownTypeAttribute attribute or by 
    adding them to the list of known types passed to DataContractSerializer."}


    Sudhakar Mandaleeka

     I have the above mentioned  data contract in one namespace, when i am passing this datacontract 
     to create subscriptions from a C# application it is working fine.
     
     and when I pass this data contract to a managed C++(C++/CLI) layer from C# 
     by creating a new data contract , and then copy the data contract 
     to a newly created data contract and passing this datacontract to create subscriptions
     I am getting the error.
     
     I am wondering beacuse it is the same copy of data contract ,in one approach it is working working fine.
     but in the second approach when i copy the data contract from one name space to other in a different name space
     it is not working.
     
     Please let me know if you have any suggetions on this.
     Appreciate yur help on this.
    • Edited by Sudhakar_M Friday, May 15, 2015 5:09 AM
    Thursday, May 14, 2015 9:57 AM

Answers

  •  Am i missing here anything ? could you please take a closer look into the above data contract i have provided and help me in resolving this error.

    [DataMember]
            public object DBHandle
            {
                get
                {
    return creator;
                }
                set
                {
    creator = value;
                }
            }

    I would say that anyting with 'object' is casuing you problems. It's not a known type. It is a base class/object  for all .NET objects. I don't think you can use 'object' in the manner you are using it with WCF in a datacontract.

    https://msdn.microsoft.com/en-us/library/system.object%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

    Thursday, May 14, 2015 1:48 PM
  • And all of these things you now are trying to make into objects used in a datacontract are classes/objects using the <Serializable> attribute.

    SubscriptionTopic topic = new SubscriptionTopic();
    topic.DBHandle = topicsInfo[i].DBHandle;
    topic.Topic = topicsInfo[i].Topic;
    topic.Target = topicsInfo[i].Target;

    Yeah, you can pass this stuff around in a solution that is not using seralization, but if WCF is invloved, everything all objects within the Datacontract  must be serializable using the <Serializable> attribute at the top of the class for each class/object in the Datacontract.

    If this is not so, then it's not going to work with what you are thrying to do here.

    Friday, May 15, 2015 6:04 PM

All replies

  • Hi Suhakar,

    Try this 

    Try using the KnownTypeAttribute as detailed in the exception 

    Refer : http://stackoverflow.com/questions/6153093/unexpected-type-serialization-exception


    Thursday, May 14, 2015 10:28 AM
  • You can also try 

    Add this in web.config 

    <configuration>
       <system.diagnostics>
          <sources>
                <source name="System.ServiceModel" 
                        switchValue="Information, ActivityTracing"
                        propagateActivity="true">
                <listeners>
                   <add name="traceListener" 
                       type="System.Diagnostics.XmlWriterTraceListener" 
                       initializeData= "c:\log\Traces.svclog" />
                </listeners>
             </source>
          </sources>
       </system.diagnostics>
    </configuration>

    this creates a log and you can view traces.svclog - see for error and once you locate error - check inner exception 



    I had used this once in my SharePoint WCF and this is very useful 


    Refer : https://msdn.microsoft.com/en-us/library/ms733025(v=vs.110).aspx 
    Thursday, May 14, 2015 10:37 AM
  • Hi ,

    I have data contract as follows. It has knownType attribute to most of them. but still i am getting the above mentioned error while passing this data contract to another method. Please let me know where exactly i need to add the knowntype attribute. I am really in confusion in finding out where exaclty is the problem ?

    Appreciate your help.

     [DataContract(Name = "TopicDetailsInfo", Namespace = "http://CName.GlobalService")]
        [Serializable]
        public class TopicDetailsInfo
        {        
            protected object topic;        
            protected object baseObjectType;       
            [DataMember]
            public object BaseObjectType
            {
                get
                {
                    return baseObjectType;
                }
                set
                {
                    baseObjectType = value;
                }
            }

            [DataMember]
            public object TopicID
            {
                get
                {
                    return topic;
                }
                set
                {
                    topic = value;
                }
            }

            static public TopicDetailsInfo CreateTopic<T, mT>(IComparable<T> objectType, IComparable<mT> objectID)
            {
                var topicDetails = new TopicDetailsInfo();
                topicDetails.BaseObjectType = objectType;
                topicDetails.TopicID = objectID;
                return topicDetails;

            }

        }


        [DataContract(Name = "SubscriptionTopicInfo", Namespace = "http://CName.GlobalService")]
        [KnownType(typeof(List<TopicDetailsInfo>))]
         [Serializable]
        public class SubscriptionTopicInfo
        {
            private object topic;
            private object target;
            private object creator;
            [DataMember]
            public object Topic
            {
                get
                {
                    return topic;
                }
                set
                {
                    topic = value;
                }
            }
            [DataMember]
            public object Target
            {
                get
                {
                    return target;
                }
                set
                {
                    target = value;
                }
            }

            [DataMember]
            public object DBHandle
            {
                get
                {
                    return creator;
                }
                set
                {
                    creator = value;
                }
            }
            static public SubscriptionTopicInfo CreateSubscriptions<T, mT, nT>(IList<TopicDetailsInfo> topic, IComparable<mT> target, IComparable<nT> handle)
            {

                var subscriptionTopic = new SubscriptionTopicInfo();

                subscriptionTopic.Target = target;
                subscriptionTopic.Topic = topic;
                subscriptionTopic.DBHandle = handle;

                return subscriptionTopic;
            }

        }


        [DataContract(Name = "SubscriptionData", Namespace = "http://CName.GlobalService")]
        [KnownType(typeof(List<SubscriptionTopicInfo>))]
        [Serializable]
        public class SubscriptionDataContracts : IExtensibleDataObject
        {
            private ExtensionDataObject extensionDataObjectValue;       
            [DataMember]         
            public string UserID 
            {
                get;
                set;

            }
           
            [DataMember]
            public string ProjectID 
            {
                get;
                set;

            }
            [DataMember]
            public string FromDiscipline 
            {
                get;
                set;

            }
            
            [DataMember]
            public string ModuleID 
            {
                get;
                set;

            }
            
            [DataMember]
            public string SessionID
            {
                get;
                set;

            }
            
            [DataMember]
            public List<SubscriptionTopicInfo> TopicList
            {
                get;
                set;
            }        
            public ExtensionDataObject ExtensionData
            {
                get
                {
                    return extensionDataObjectValue;
                }
                set
                {
                    extensionDataObjectValue = value;
                }
            }

    }


    Sudhakar Mandaleeka

     I have the above mentioned  data contract in one namespace, when i am passing this datacontract 
     to create subscriptions from a C# application it is working fine.
     
     and when I pass this data contract to a managed C++(C++/CLI) layer from C# 
     by creating a new data contract , and then copy the data contract 
     to a newly created data contract and passing this datacontract to create subscriptions
     I am getting the error.
     
     I am wondering beacuse it is the same copy of data contract ,in one approach it is working working fine.
     but in the second approach when i copy the data contract from one name space to other in a different name space
     it is not working.
     
     Please let me know if you have any suggetions on this.
     Appreciate yur help on this.
    • Edited by Sudhakar_M Friday, May 15, 2015 5:09 AM
    Thursday, May 14, 2015 11:45 AM
  • Please go through the link which i provided you..
    Thursday, May 14, 2015 12:56 PM
  • I have gone thru the link .and also applied the knowntypeattribute as follows. but i am facing the same error.

    Am i missing here anything ? could you please take a closer look into the above data contract i have provided and help me in resolving this error.

    I have tried as follows so far:

    [KnownType(typeof(List<SubscriptionTopicInfo>))]
        [KnownType(typeof(List<TopicDetailsInfo>))]    
        [Serializable]
        public class SubscriptionDataContracts : IExtensibleDataObject


    Sudhakar Mandaleeka

    Thursday, May 14, 2015 1:17 PM
  •  Am i missing here anything ? could you please take a closer look into the above data contract i have provided and help me in resolving this error.

    [DataMember]
            public object DBHandle
            {
                get
                {
    return creator;
                }
                set
                {
    creator = value;
                }
            }

    I would say that anyting with 'object' is casuing you problems. It's not a known type. It is a base class/object  for all .NET objects. I don't think you can use 'object' in the manner you are using it with WCF in a datacontract.

    https://msdn.microsoft.com/en-us/library/system.object%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

    Thursday, May 14, 2015 1:48 PM
  • Thanks for the info on the object.

    So in order to resolve this error what should I do  and how i need to handle the object in this data contract.

    appreciate your help.

    Thanks,
    Sudhakar


    Sudhakar Mandaleeka

    Thursday, May 14, 2015 2:11 PM
  •  

    private object topic;
     private object target;
    private object creator;

    I don't know what they are supposed to be. But if they are just string data or some other primitive type data like an int or something, then make them that. If they are not, then you need to make concrete classes/known types and point to them that represnts sometype of a known class/object.   

    Thursday, May 14, 2015 5:22 PM
  •  I have the above mentioned  data contract in one namespace, when i am passing this datacontract 
     to create subscriptions from a C# application it is working fine.
     
     and when I pass this data contract to a managed C++(C++/CLI) layer from C# 
     by creating a new data contract , and then copy the data contract 
     to a newly created data contract and passing this datacontract to create subscriptions
     I am getting the error.
     
     I am wondering beacuse it is the same copy of data contract ,in one approach it is working working fine.
     but in the second approach when i copy the data contract from one name space to other in a different name space
     it is not working.
     
     Please let me know if you have any suggetions on this.
     Appreciate yur help on this.

    Sudhakar Mandaleeka

    Friday, May 15, 2015 5:08 AM
  • Here is my scenario : i am copying the data from dc to new data contract as below. after copying , when i am executing the createsubscriptions method i am getting the above mentioned error. i have given the details of data contract and error in the original question. please refer to that as well.

    Method1(SubscriptionDataContracts dc)
    {
    SubscriptionDataContract subscriptionDataContract = new SubscriptionDataContract();

                  List<SubscriptionTopicInfo> topicsInfo = dc.TopicList;
                  List<SubscriptionTopic> newTopicsList = new List<SubscriptionTopic>();
                  subscriptionDataContract.ExtensionData = dc.ExtensionData;

                  subscriptionDataContract.UserID = dc.UserID;

                    for (int i = 0; i < topicsInfo.Count; i++)
                    {
                        SubscriptionTopic topic = new SubscriptionTopic();
                        topic.DBHandle = topicsInfo[i].DBHandle;
                        topic.Topic = topicsInfo[i].Topic;
                        topic.Target = topicsInfo[i].Target;
                        newTopicsList.Add(topic);    
                    }
                    subscriptionDataContract.TopicList = newTopicsList;

                    CreateSubscriptions(subscriptionDataContract);   //getting teh above mentioned error here 
      }

                                                 

    Sudhakar Mandaleeka

    Friday, May 15, 2015 6:09 AM
  • Sorry to disappoint you but I am no expert in .Net WCF 

    I have helped you as much as I can 

    Friday, May 15, 2015 6:42 AM
  • And all of these things you now are trying to make into objects used in a datacontract are classes/objects using the <Serializable> attribute.

    SubscriptionTopic topic = new SubscriptionTopic();
    topic.DBHandle = topicsInfo[i].DBHandle;
    topic.Topic = topicsInfo[i].Topic;
    topic.Target = topicsInfo[i].Target;

    Yeah, you can pass this stuff around in a solution that is not using seralization, but if WCF is invloved, everything all objects within the Datacontract  must be serializable using the <Serializable> attribute at the top of the class for each class/object in the Datacontract.

    If this is not so, then it's not going to work with what you are thrying to do here.

    Friday, May 15, 2015 6:04 PM