简体   繁体   English

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

[英]WCF with multiple services and namespace issues

I have created a number of WCF Services, for arguments sake they are called Service1 and Service2 . 我创建了许多WCF服务,但为了论证,它们被称为Service1Service2

Both of the services return (at some point, possibly through a relationship inside an object) a Customer object. 两个服务都返回(在某些时候,可能通过对象内部的关系) Customer对象。

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. 为了测试,我向Service1和Service2添加了一个GetCustomer()方法,并且我在基本的WinForms应用程序中添加了对这两个服务的服务引用。

Service1Client proxy1 = new Service1Client(); Service1Client proxy1 = new Service1Client();

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

^^^^^^ Ambiguous reference, requires me to name as WcfTestClient.Service1.Customer ^^^^^^不明确的引用,要求我命名为WcfTestClient.Service1.Customer

Service2Client proxy2 = new Service2Client(); Service2Client proxy2 = new Service2Client();

Customer customer2 = proxy2.GetCustomer(); 客户customer2 = proxy2.GetCustomer();

^^^^^ Ambiguous reference, requires me to name as WcfTestClient.Service2.Customer ^^^^^不明确的引用,要求我命名为WcfTestClient.Service2.Customer

The problem is, the Customer object returned by Service1 and Service2 are both the same type of Customer (WcfTestService.Customer). 问题是,由服务1服务2返回的客户对象都是相同类型客户(WcfTestService.Customer)的。 To remedy this I need to include the full assembly name rather than just Customer. 为了解决这个问题,我需要包含完整的程序集名称,而不仅仅是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. 我已经阅读了Stack Overflow上的一些帖子,说明可以将数据合同编译成一个单独的程序集,我不是特别喜欢这个想法,因为它可能仍然会导致使用其他语言(如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? 我看到的另一个解决方案是SvcUtil.exe方法,但从我所看到的这个解决方案没有解决我的命名空间问题,因为我需要单独为每个服务运行Util?

If anyone has any helpful suggestions, please get in touch! 如果有人有任何有用的建议,请联系我们!

Both of the services return (at some point, possibly through a relationship inside an object) a Customer object. 两个服务都返回(在某些时候,可能通过对象内部的关系)Customer对象。

Here's where you're wrong. 这是你错了。 WCF doesn't return objects, REST doesn't return objects, SOAP doesn't return objects. WCF不返回对象,REST不返回对象,SOAP不返回对象。 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. 现在,当您添加对Web服务的引用时会发生什么,Visual Studio很乐意为这些消息创建一个包装类,将其内容作为属性公开,仅此而已。 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. 不要考虑对象传递并开始考虑消息,你会发现自己要么遇到两个包装对象,要么需要链接一个外部程序集。

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

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