Skip to Content
Programming WCF Services
book

Programming WCF Services

by Juval Lowy
February 2007
Intermediate to advanced
634 pages
16h 1m
English
O'Reilly Media, Inc.
Content preview from Programming WCF Services

Generics

You cannot define WCF contracts that rely on generic type parameters. Generics are specific to .NET, and using them would violate the service-oriented nature of WCF. However, you can use bounded generic types in your data contracts, as long as you specify the type parameters in the service contract and as long as the specified type parameters have valid data contracts, as shown in Example 3-13.

Example 3-13. Using bounded generic types

[DataContract]
class MyClass<T>
{
   [DataMember]
   public T m_MyMember;
}

[ServiceContract]
interface IMyContract
{
   [OperationContract]
   void MyMethod(MyClass<int> obj);
}

When you import the metadata of a data contract such as the one in Example 3-13, the imported types have all type parameters replaced with specific types, and the data contract itself is renamed to:

<Original name>Of<Type parameter names><hash>

Using the same definitions as in Example 3-13, the imported data contract and service contract will look like this:

[DataContract]
class MyClassOfint
{
   int MyMemberField;

   [DataMember]
   public int  MyMember
   {
      get
      {
         return MyMemberField;
      }
      set
      {
         MyMemberField = value;
      }
   }
}

[ServiceContract]
interface IMyContract
{
   [OperationContract]
   void MyMethod(MyClassOfint obj);
}

If instead of the int the service contract were to use a custom type such as SomeClass:

[DataContract]
class SomeClass
{...}

[DataContract]
class MyClass<T>
{...}

[OperationContract]
void MyMethod(MyClass<SomeClass> obj);

then the exported data contract may look like this:

[DataContract] ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Programming WCF Services, 2nd Edition

Programming WCF Services, 2nd Edition

Juval Lowy
Pro WCF: Practical Microsoft SOA Implementation

Pro WCF: Practical Microsoft SOA Implementation

Chris Peiris, Dennis Mulder, Shawn Cicoria, Amit Bahree, Nishith Pathak
Mastering ASP.NET Web API

Mastering ASP.NET Web API

Mithun Pattankar, Malendra Hurbuns

Publisher Resources

ISBN: 0596526997Supplemental ContentErrata Page