views:

635

answers:

3

Hi,

i have two web services. One with user functionality, one with admin functionality.

Both services effectively work with the same object types, for instance:

  • AdminService provides functionality for deleting/modifying Customer objects
  • UserService provides functionality for listing/reading Customer objects

Now in the client i have two service references, Webservices.Admin and Webservices.User.

If i use the UserService to retrieve Customer objects, i cannot manipulate those via the AdminService, since the UserService retrieves objects of type Webservices.User.Customer, however the AdminService works with objects of type Webservices.Admin.Customer.

On the server side both types are identical, just belong to different namespaces in the client.

Now the question: How can i share types across different service references?

+1  A: 

If you're controlling both ends of the communication, and both ends are .NET only, you could do this:

  • put all your contracts, including your data contracts, into a separate "Contracts" assembly
  • reference that assembly in both the server side implementation code, as well as the client side code

If you do this, when adding the service references, WCF will find and use that shared assembly, and not create new types for the entitites. In your case, you'd only ever have one type Contracts.Customer or whatever you're dealing with.

This works only if you control both ends of the wire and have .NET on both ends! But in that case, it's a great way to share contracts - especially data contracts - across both the server and any number of clients.

marc_s
Yes i control everything. However the client is silverlight, so sharing the contract assembly is not possible (because my datacontracts are coming from Entity Framework).If the client is any other wcf client except silverlight, it works however.
Roasted Battle Squirrel
OK, Silverlight is an important point that should have been mentioned in the first place. But you should still be able to create a Silverlight assembly that contains a type that serializes to the same structure as the Customer from your EF, stick that into a separate assembly, and reference that common assembly from any number of client parts.
marc_s
+1  A: 

Use the slsvcutil to create the WCF proxy on the clientside (assuming the clientside is a .net application), reference the DLL which contains your objects and it will be used for all endpoints that pass the same object in the DLL

Open Visual Studio Command prompt from the Start -> Visual Studio 2008 -> Tools -> Visual Command Prompt

goto directory similar to

C:\Program Files\Microsoft SDKs\Silverlight\v3.0\Tools

type slsvcutil and follow the syntax

slsvcutil http://somewcfservice:8080 /r:CommonLibrary.dll

where CommonLibrary.dll is the dll that contains the business objects

[edit] fixed the fact that the project is a silverlight project

Neil
A: 

There is an easy way to share types between client and service, just by adding reference to shared type assembly to your client BEFORE adding the service reference.

You can find the detailed scenario and sample project there:

http://blog.walteralmeida.com/2010/08/wcf-tips-and-tricks-share-types-between-server-and-client.html

Walter Almeida