WCF 和多个命名空间的问题 - 跨多个服务引用共享对象类型
我有两个网络服务。一种具有用户功能,一种具有管理功能。
这两个服务有效地使用相同的对象类型,例如:
- AdminService 提供删除/修改 Customer 对象的功能
- UserService 提供列出/读取 Customer 对象的功能
现在在客户端中我有两个服务引用,Webservices.Admin 和 Webservices.User。
如果我使用 UserService 检索 Customer 对象,则无法通过 AdminService 操作这些对象,因为 UserService 检索 Webservices.User.Customer 类型的对象,但 AdminService 可以处理 Webservices.Admin.Customer 类型的对象。
在服务器端,两种类型是相同的,只是在客户端属于不同的命名空间。
现在的问题是:如何在不同的服务引用之间共享类型?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
查看 https://github.com/geersch/WcfSvcMap
通过调整 Reference.svcmap 文件,您可以确保为不同服务引用使用的每个 DataContract 只生成一个类。
注意:请记住在按“更新服务引用”之前删除节点的内容
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'
如果您控制通信的两端,并且两端都是 .NET,则可以执行以下操作:
如果您这样做,则在添加服务引用时,WCF 将查找并使用该共享程序集,而不是为实体创建新类型。就您而言,您只能拥有一种类型
Contracts.Customer
或您正在处理的任何类型。仅当您控制线路的两端并且两端都有 .NET 时,此功能才有效!但在这种情况下,这是在服务器和任意数量的客户端之间共享合同(尤其是数据合同)的好方法。
If you're controlling both ends of the communication, and both ends are .NET only, you could do this:
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.
使用 slsvcutil 在客户端创建 WCF 代理(假设客户端是 .net 应用程序),引用包含对象的 DLL,它将用于在 DLL 中传递相同对象的所有端点
打开 Visual Studio 命令提示符从一开始-> Visual Studio 2008 ->工具->视觉命令提示符
转到目录类似于
输入 slsvcutil 并遵循语法
,其中 CommonLibrary.dll 是包含业务对象的 dll
[编辑] 修复了该项目是 silverlight 项目的事实
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
type slsvcutil and follow the syntax
where CommonLibrary.dll is the dll that contains the business objects
[edit] fixed the fact that the project is a silverlight project
有一种简单的方法可以在客户端和服务之间共享类型,只需在添加服务引用之前向客户端添加对共享类型程序集的引用即可。
您可以在那里找到详细的场景和示例项目:
http://blog.walteralmeida.com/2010/08/wcf-tips-and-tricks-share-types- Between-server-and-client.html
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