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

后端 未结 4 779
闹比i
闹比i 2021-01-13 09:53

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

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

相关标签:
4条回答
  • 2021-01-13 10:05

    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

    0 讨论(0)
  • 2021-01-13 10:17

    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.

    0 讨论(0)
  • 2021-01-13 10:20

    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

    0 讨论(0)
  • 2021-01-13 10:31

    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'

    0 讨论(0)
提交回复
热议问题