【问题标题】:Problem with DataContract and hierarchy on WCFWCF 上的 DataContract 和层次结构问题
【发布时间】:2011-07-26 19:39:31
【问题描述】:

我的 wcf 项目中的一个对象有问题。 我可以说这个对象:

[DataContract(Name="ClassA")]
public class Person{
   //---attributes---
}

[DataContract(Name="ClassB")]
public class Men : Person{
  //---attributes---
}

其中 ClassB 是 ClassA 在另一边的子级。 然后我有一个方法是 post:

[OperationContract]
[WebInvoke(UriTemplate= "Person", ResponseFormat = WebMessageFormat.Json, Method= "POST")]
public string PostPerson(Person person) {
    if(person is Men){
       //code...
    }
}

问题是我收到了这个人(在另一边,他们将我作为 B 类发送)但这个人是 Men 返回 false.. 为什么?

【问题讨论】:

    标签: wcf c#-4.0 datacontract


    【解决方案1】:

    您需要将[ServiceKnownType(typeof(Men))] 属性添加到 PostPerson 方法。

    【讨论】:

      【解决方案2】:

      正如 Ryan Gross 所提到的,你需要 Men 是一种众所周知的类型。 Here's a similar question/answer here on SO. 链接文章中未提及的一个选项是KnownType attribute。这是我过去使用的代码示例。前提是这个类是你所有数据合约的基类,并且你所有的数据合约都在同一个程序集中:

      /// <summary>
      ///   Base class for all data contracts.
      /// </summary>
      [DataContract(Name = "Base", Namespace = "your namespace")]
      [KnownType("GetKnownTypes")]
      public class BaseDC : IExtensibleDataObject
      {
        #region Constants and Fields
      
        /// <summary>
        ///   Instance used to control access to the known types list.
        /// </summary>
        private static readonly object _knownTypesLock = new object();
      
        /// <summary>
        ///   Classes derived from this class.  Needed to ensure proper functioning of the WCF data
        ///   constract serializer.
        /// </summary>
        private static List<Type> _knownTypes;
      
        #endregion
      
        #region Properties
      
        /// <summary>
        ///   Gets or sets an <c>ExtensionDataObject</c> that contains data that is not recognized as belonging to the
        ///   data contract.
        /// </summary>
        public ExtensionDataObject ExtensionData { get; set; }
      
        #endregion
      
        #region Public Methods
      
        /// <summary>
        ///   Enumerates the types in the assembly containing <c>BaseDC</c> that derive from <c>BaseDC</c>.
        /// </summary>
        /// <returns>List of <c>BaseDC</c>-derived types.</returns>
        [SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate",
          Justification = "Not appropriate for a property.")]
        public static IEnumerable<Type> GetKnownTypes()
        {
          lock (_knownTypesLock)
          {
            if (_knownTypes == null)
            {
              _knownTypes = new List<Type>();
              Assembly contractsAssembly = Assembly.GetAssembly(typeof(BaseDC));
              Type[] assemblyTypes = contractsAssembly.GetTypes();
              foreach (Type assemblyType in assemblyTypes)
              {
                if (assemblyType.IsClass && !assemblyType.IsGenericType)
                {
                  if (assemblyType.IsSubclassOf(typeof(BaseDC)))
                  {
                    _knownTypes.Add(assemblyType);
                  }
                }
              }
      
              _knownTypes.Add(typeof(BaseDC));
            }
      
            return _knownTypes;
          }
        }
      
        #endregion
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-18
        • 1970-01-01
        • 1970-01-01
        • 2017-08-29
        相关资源
        最近更新 更多