views:

345

answers:

3

I have two WCF services which are logically different and I wish to keep them seperate. They each have an operation which returns the same datacontract. A single silverlight client consumes both services. When I add a service reference in the silverlight client to each service, the single datacontract ends up twice in the generated code. Is it possible to have both service references use the same definition of the datacontract?

From what I've read, for non silverlight clients it is possible by compiling the datacontracts into a seperate assembly which is referenced by both the server and the client. However this is not possible with silverlight (at least in silverlight 3).

Thanks,
Rob

+2  A: 

Stian,

Thanks for the quick reply. I watched the screencast by Miguel Castro and it was very informative. Unfortunately it doesn't cover the silverlight situation. However using that as a starting place, I was able to get something working which others may find useful.

Create two assemblies: ServerDataContracts and SilverlightDataContracts (this one as a silverlight assembly). In each one's AssemblyInfo.cs add the following line:

[assembly: ContractNamespace("http://YourNamespaceUri",
 ClrNamespace = "YourDataContractsNamespace")]

Add all of your DataContracts to the ServerDataContracts assembly. In the SilverlightDataContracts assembly, right click on the project (or sub-folder) and select "Add Existing Item". Browse to the DataContracts in the ServerDataContracts assembly and selected the ones you want to add. DO NOT HIT "Add"!!! Instead, click the down arrow next to "Add" and select "Add As Link". This links the SilverlightDataContracts project to the existing files in the ServerDataContracts project. Now when you edit one, both projects get the change. When you compile, the projects compile separately with their correct targets.

Now add the ServerDataContracts assembly as a reference to your service contract project. Do the same for your SilverlightDataContracts assembly to your silverlight application. When you add a service reference from your silverlight client to your service, it should not generate the datacontracts. It should instead use the ones from your assembly.

I have gotten this working, however it is still missing some of the features provided by the generated datacontract classes. The generated classes implement INotifyPropertyChanged which is extremely useful when databinding in silverlight/wpf. While you can implement this yourself, it is fairly tedious and now your server code must execute all of these event handlers on every property set even though you will probably never subscribe to the PropertyChanged event on the server.

I'm still looking for a cleaner way to handle this. It looks like silverlight 4 will make this easier in that you can use the same assembly in both the client and the server (the compiled code is compatible). Unfortunately that is too far away for my needs.

DarthChucks
This should have really been added to your question as additional information.
ChrisF
A: 

This is irritating however it worked. I have one main service, and several supporting services. The main service it where all the datacontracts belong, and the supporting services should reuse the datacontracts specified in the main service.

I created a silverlight assembly to act solely as the client of the main service. Then I had my silverlight application reference it. Then in the silverlight application, I added my supporting services. When adding a supporting service, the advanced screen allows you to specify if it should reuse types in referenced assemblies. It is on by default. Since the main service is specified in a referenced assembly, all the supporting services use the datacontracts from the main one.

It isn't pretty, but it works.

DarthChucks
You should either edit your question with more information or just post a single answer. Stack Overflow is not a traditional forum, it's a Question and Answer site, so you don't have conversations as you would in a forum.
ChrisF
+1  A: 

Have you tried generating the clients using svcutil. IF you give multiple service endpoints it all work fine but it gives you a ton of errors generating the clients.

rerun