具有多个服务和命名空间问题的 WCF
我创建了许多 WCF 服务,为了论证起见,它们被称为 Service1 和 Service2。
这两个服务都返回(在某些时候,可能通过对象内部的关系)一个 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
问题是,Service1 和 Service2 返回的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这两个服务都返回(在某些时候,可能通过对象内部的关系)一个 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.