WCF exposing generic type 'T'

后端 未结 3 1561
走了就别回头了
走了就别回头了 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:15

    I think it is imposible, how could it generate the wsdl that way?

    You have two options:

    • You could send the type as a parameter.

    • If you want to expose crud operations for entities I would recommend to use a code generator, maybe a T4 template for EF.

    0 讨论(0)
  • 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<T>
      {
         private T _id;
      
         [DataMember]
         public T ID
         {
            get { return _id; }
            set { _id = value; }
         }
      }
      
      [OperationContract]
      MyGenericObject<int> 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.

    0 讨论(0)
  • 2020-12-11 07:28

    This post is old indeed, but maybe someone find this solution useful: WCF and Generics

    0 讨论(0)
提交回复
热议问题