rafale
rafale

Reputation: 1735

Using multiple DataContracts for one service

I've got a ServiceContract that has an OperationContract with the following method signature: Manipulate(int fileid, param object[] operations).

I also have five DataContracts defined for the WCF service, and I'd like object[] operations to accept any number and combination of those. The problem is that they aren't visible unless I use them in the method signature. If I do that though, then only one type of DataContract can be used at a time, defeating the purpose of using an object[].

How do I make all five DataContracts visible on the client side without having to alter the method signature?

Upvotes: 2

Views: 315

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

Use known types. For example in your config file you could define the different known types:

<configuration>
   <system.runtime.serialization>
       <dataContractSerializer>
           <declaredTypes>
             <add type="System.Object, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
                 <knownType type="SomeNs.Foo, MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=XXXXXX" />
                 <knownType type="SomeNs.Bar, MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=XXXXXX" />
                 <knownType type="SomeNs.Baz, MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=XXXXXX" />
             </add>
           </declaredTypes>
       </dataContractSerializer>
   </system.runtime.serialization>
</configuration> 

Now clients will know about the Foo, Bar and Baz data contracts.

This being said, I would recommend you to use a common base type for your data contracts instead of object. Having a method signature that takes object as input is hard to understand from a consumer standpoint.

Upvotes: 6

Related Questions