简体   繁体   中英

Problem with WCF and multiple namespaces - sharing object types across multiple service references

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?

Check out http://cgeers.com/2011/07/31/wcf-sharing-types-using-svcmap/ By tweaking the Reference.svcmap file you can make sure only one class is generated for each DataContract used by the different service references.

Note: Remember to delete the content of the node before pressing 'Update Service Reference'

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM