English 中文(简体)
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 https://github.com/geersch/WcfSvcMap 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 FilesMicrosoft SDKsSilverlightv3.0Tools

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





相关问题
Silverlight Rich text box control

Our team decided that we need our own custom Rich text box control for Silverlight app we are developing. We looked at existing controls mentioned at A good rich text control for Silverlight but ...

Silverlight ImageBrush not rendering (with Bing Map Control)

I m trying to add an image to a Pushpin instance from the Silverlight Bing Map Control, but I can t seem to get it to render (the pushpin renders fine). This is probably a general WPF question rather ...

Silverlight OpenFileDialog DoEvents equivalent

I m processing large files after they are selected by the user. My code looks like the following: if (FileDialog.ShowDialog() == true) { // process really big file } This freezes up the UI so ...

list of controls with templates in silverlight

Does anyone know where to find a list of controls that you can set the template on in Silverlight? I ve wasted several hours now trying to create control templates only to find that the control doesn ...

Silverlight, Updating the UI during processing

I have a simple silverlight multifile upload application, and i want to provide the user with some feedback, right now its only in a test phase and i dont have the webservice. Somehow i cant get the ...

Silverlight 3 - FindName returns null

This looks a bug to me.. Using Silverlight 3 and i have a user control defined in XAML and trying to access the object during runtime returns a null. <Grid> <common:CommonGridEditPanel x:...

silverlight 3 collection binding

Someone please help me understand why this binding does not work... I have a class called SelectionManager with a property called dates which is populated by a WCF service. The property is an ...

热门标签