WCF exposing generic type 'T'

后端 未结 3 1564
走了就别回头了
走了就别回头了 2020-12-11 07:07

I write a WCF service for Insert and delete operation here we used generic method but it gives following error \"System.Runtime.Serialization.InvalidDataContractException: T

3条回答
  •  不思量自难忘°
    2020-12-11 07:19

    1. Answer to this question is both Yes and No. Yes for server prospective and No for client prospective.
    2. We can create a generic Data Contract on server but while using that in any operation contract we have to specify a data type of the generic.
    3. And at client end that data contract will be exposed only as a strongly data type not generic.

      [DataContract]
      public class MyGenericObject
      {
         private T _id;
      
         [DataMember]
         public T ID
         {
            get { return _id; }
            set { _id = value; }
         }
      }
      
      [OperationContract]
      MyGenericObject GetGenericObject();
      

    This is what we have in Server we can see while using generic data contract we have to specify the type otherwise it’ll give compile time error.

    On client what we get from WSDL is a follow:

    [DataContract]
    public class MyGenericObjectOfint
    

    We can see here what we get from WSDL is not a generic data contract WSDL proxy generate a class with a new name using some convention.

    Convention used is

    Generic Class Name + "Of" + Type Parameter Name + Hash

    Hash is not always generated, it’ll be generated only when there is a chance of name collision.

提交回复
热议问题