具有多个服务和命名空间问题的 WCF

发布于 2024-08-19 17:06:03 字数 1081 浏览 2 评论 0原文

我创建了许多 WCF 服务,为了论证起见,它们被称为 Service1Service2

这两个服务都返回(在某些时候,可能通过对象内部的关系)一个 Customer 对象。

为了进行测试,我向 Service1 和 Service2 添加了 GetCustomer() 方法,并在基本 WinForms 应用程序中添加了对这两个服务的服务引用。

<块引用>

Service1Client proxy1 = new Service1Client();

客户 customer1 = proxy1.GetCustomer(); //

<块引用>

^^^^^^ 引用不明确,要求我将其命名为 WcfTestClient.Service1.Customer

Service2Client proxy2 = new Service2Client();

客户 customer2 = proxy2.GetCustomer();

^^^^^ 引用不明确,要求我将其命名为 WcfTestClient.Service2.Customer

问题是,Service1Service2 返回的 Customer 对象都是相同类型的 Customer (WcfTestService.Customer)。为了解决这个问题,我需要包含完整的程序集名称,而不仅仅是客户。

我读过 Stack Overflow 上的一些帖子,指出可以将数据契约编译成单独的程序集,但我不太喜欢这个想法,因为它仍然可能会给使用其他语言(例如 Java)的客户端带来问题。

我看到的另一个解决方案是 SvcUtil.exe 方法,但从我所看到的情况来看,这个解决方案并没有解决我的命名空间问题,因为我需要单独为每个服务运行 Util?

如果有人有任何有用的建议,请联系我们!

I have created a number of WCF Services, for arguments sake they are called Service1 and Service2.

Both of the services return (at some point, possibly through a relationship inside an object) a Customer object.

For testing sake, I have added a GetCustomer() method to both Service1 and Service2 and I have added a service reference to both services in a basic WinForms application.

Service1Client proxy1 = new Service1Client();

Customer customer1 = proxy1.GetCustomer(); //

^^^^^^ Ambiguous reference, requires me to name as WcfTestClient.Service1.Customer

Service2Client proxy2 = new Service2Client();

Customer customer2 = proxy2.GetCustomer();

^^^^^ Ambiguous reference, requires me to name as WcfTestClient.Service2.Customer

The problem is, the Customer object returned by Service1 and Service2 are both the same type of Customer (WcfTestService.Customer). To remedy this I need to include the full assembly name rather than just Customer.

I have read a few posts on Stack Overflow stating that it is possible to compile the Data Contracts into a separate assembly, I don’t particularly like this idea as it may still cause problems with clients using other languages, such as Java.

Another solution I have seen is the SvcUtil.exe method, but from what I have seen this solution doesn’t address my namespace issue as I need to run the Util for each service individually?

If anyone has any helpful suggestions, please get in touch!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

下雨或天晴 2024-08-26 17:06:03

这两个服务都返回(在某些时候,可能通过对象内部的关系)一个 Customer 对象。

这就是您错误的地方。 WCF 不返回对象,REST 不返回对象,SOAP 不返回对象。他们都传递消息

现在,当您添加对 Web 服务的引用时,Visual Studio 会愉快地为这些消息创建一个包装类,将它们的内容公开为属性,仅此而已。因为您要添加两个服务,所以这些包装器类彼此不了解,因此最终会得到两个命名空间和两个包装器类。

是的,正如您所说,您可以将消息类移动到单独的程序集,链接它并避免添加引用,然后它将充当适当的对象,但仍在幕后将消息传递、序列化和反序列化到此共享对象中。停止考虑对象传递并开始考虑消息,您会意识到您要么被两个包装对象困住,要么需要链接外部程序集。

Both of the services return (at some point, possibly through a relationship inside an object) a Customer object.

Here's where you're wrong. WCF doesn't return objects, REST doesn't return objects, SOAP doesn't return objects. They all pass messages.

Now what happens when you add a reference to a web service is Visual Studio happily creates a wrapper class for these messages, exposing their contents as properties, nothing more. Because you are adding two services these wrapper classes have no knowledge of each other and thus you end up with two namespaces and two wrapper classes.

Yes, as you say you could move the message classes to a separate assembly, link that and avoid Add Reference and that will then act as a proper object, but still behind the scenes it's messages being passed and serialized and deserialized into this shared object. Stop thinking in terms of object passing and start thinking in terms of messages and you'll realise you're either stuck with two wrapper objects, or you need to link an external assembly.

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