简体   繁体   English

WCF和多个名称空间的问题 - 跨多个服务引用共享对象类型

[英]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 AdminService提供删除/修改Customer对象的功能
  • UserService provides functionality for listing/reading Customer objects UserService提供列出/读取Customer对象的功能

Now in the client i have two service references, Webservices.Admin and Webservices.User. 现在在客户端我有两个服务引用,Webservices.Admin和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. 如果我使用UserService来检索Customer对象,我无法通过AdminService操作它们,因为UserService检索Webservices.User.Customer类型的对象,但是AdminService使用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. 查看http://cgeers.com/2011/07/31/wcf-sharing-types-using-svcmap/通过调整Reference.svcmap文件,您可以确保只为不同服务使用的每个DataContract生成一个类引用。

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: 如果你控制通信的两端,并且两端都只是.NET,你可以这样做:

  • 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. 如果执行此操作,则在添加服务引用时,WCF将查找并使用该共享程序集,而不是为权限创建新类型。 In your case, you'd only ever have one type Contracts.Customer or whatever you're dealing with. 在您的情况下,您只有一种类型Contracts.Customer或您正在处理的任何事情。

This works only if you control both ends of the wire and have .NET on both ends! 只有当您控制电线的两端并且两端都有.NET时,这才有效! 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 使用slsvcutil在clientside上创建WCF代理(假设clientside是.net应用程序),引用包含对象的DLL,它将用于在DLL中传递相同对象的所有端点

Open Visual Studio Command prompt from the Start -> Visual Studio 2008 -> Tools -> Visual Command Prompt 从开始 - > Visual Studio 2008 - >工具 - >可视命令提示符打开Visual Studio命令提示符

goto directory similar to goto目录类似于

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

type slsvcutil and follow the syntax 键入slsvcutil并按照语法

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

where CommonLibrary.dll is the dll that contains the business objects 其中CommonLibrary.dll是包含业务对象的DLL

[edit] fixed the fact that the project is a silverlight project [编辑]修复了该项目是一个Silverlight项目的事实

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 http://blog.walteralmeida.com/2010/08/wcf-tips-and-tricks-share-types-between-server-and-client.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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