如何解决WCF数据交换问题 [英] How to resolve WCF datacontracts problem

查看:54
本文介绍了如何解决WCF数据交换问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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."}









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







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

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

}

推荐答案

如果您要引用派生类它。您需要使用KnownTypeAttribute属性。



请参阅以下文章了解更多详情。



什么是KnownType属性及其如何在WCF技术中使用 [ ^ ]
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[^]


您好
我相信,您必须在Service类中使用[KnownType(typeof(SubscriptionDataContracts))]。希望这会有所帮助。
Hi I believe, you have to use [KnownType(typeof(SubscriptionDataContracts ))] in your Service class. Hope this will help.


这篇关于如何解决WCF数据交换问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆