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

发布于 2024-08-17 01:22:09 字数 490 浏览 5 评论 0原文

我有两个网络服务。一种具有用户功能,一种具有管理功能。

这两个服务有效地使用相同的对象类型,例如:

  • 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

那些过往 2024-08-24 01:22:09

查看 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'

旧情别恋 2024-08-24 01:22:09

如果您控制通信的两端,并且两端都是 .NET,则可以执行以下操作:

  • 将所有合约(包括数据合约)放入单独的“合约”程序集中,
  • 在服务器端实现中引用该程序集代码以及客户端代码

如果您这样做,则在添加服务引用时,WCF 将查找并使用该共享程序集,而不是为实体创建新类型。就您而言,您只能拥有一种类型 Contracts.Customer 或您正在处理的任何类型。

当您控制线路的两端并且两端都有 .NET 时,此功能才有效!但在这种情况下,这是在服务器和任意数量的客户端之间共享合同(尤其是数据合同)的好方法。

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.

佼人 2024-08-24 01:22:09

使用 slsvcutil 在客户端创建 WCF 代理(假设客户端是 .net 应用程序),引用包含对象的 DLL,它将用于在 DLL 中传递相同对象的所有端点

打开 Visual Studio 命令提示符从一开始-> Visual Studio 2008 ->工具->视觉命令提示符

转到目录类似于

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

输入 slsvcutil 并遵循语法

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

,其中 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

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

只为守护你 2024-08-24 01:22:09

有一种简单的方法可以在客户端和服务之间共享类型,只需在添加服务引用之前向客户端添加对共享类型程序集的引用即可。

您可以在那里找到详细的场景和示例项目:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文