Click here to Skip to main content
15,881,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
CSS
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."}




////////////////

XML
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
  }



///////////////////


C#
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;
            }
        }

}
Posted
Updated 14-May-15 20:47pm
v3

If you are referring derived class in it. you need to use KnownTypeAttribute attribute for the same.

Please refer below article for more details.

What is KnownType Attribute and How to Use It in WCF Technology[^]
 
Share this answer
 
Comments
Member 3975629 14-May-15 8:23am    
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
{
Member 3975629 15-May-15 2:47am    
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
}
Hi I believe, you have to use [KnownType(typeof(SubscriptionDataContracts ))] in your Service class. Hope this will help.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900