[英]Consume same WCF Service from multiple projects in solution
Situation: 情况:
I have a solution of multiple projects: 我有多个项目的解决方案:
Now I need to consume the WCF service in my executable app #1. 现在,我需要在我的可执行应用程序#1中使用WCF服务。 Eg to retrieve information about a user from a UserService
in the WCF project. 例如,从WCF项目中的UserService
检索有关用户的信息。 Due to both executables require a login, handled by the UserService
, I added a ´Authenticate´ method in my Shared Project . 由于这两个可执行文件都需要由UserService
处理的登录,因此我在Shared Project中添加了“ Authenticate”方法。 This authentication is also handled by the UserService
. 此认证也由UserService
处理。
Problem: 问题:
Now I have to add a service reference inside each project, which is actually not a problem. 现在,我必须在每个项目中添加一个服务引用,这实际上不是问题。 But now I get generated the proxy classe and the endpoint in all three projects. 但是现在我在所有三个项目中都生成了代理类和端点。
I see this as an issue, due to I have to update all 3 service references: 我认为这是一个问题,因为我必须更新所有3个服务引用:
Question: 题:
Is there any nice solution to add a service reference without creating the endpoint configuration in the shared project and without creating the proxy classes in the executable apps? 是否有任何不错的解决方案来添加服务引用,而无需在共享项目中创建端点配置,也无需在可执行应用程序中创建代理类?
Okay I implemented something similar to this recently, and this is what I did. 好的,最近我实现了与此类似的功能,这就是我所做的。 I think it provides nice separation of concerns and keeps you from having to generate a proxy class in all your executables. 我认为它提供了很好的关注点分离,使您不必在所有可执行文件中生成代理类。
The proxy class should look something like this: 代理类应如下所示:
public class ClientProxy
{
public IMyWCFService service;
public ClientProxy(string uri)
{
// Any channel setup code goes here
EndpointAddress address = new EndpointAddress(uri);
NetTcpBinding binding = new NetTcpBinding(SecurityMode.Transport);
binding.TransferMode = TransferMode.Streamed;
binding.MaxBufferSize = //whatever
binding.MaxReceivedMessageSize = //whatever
...
ChannelFactory<IMyWCFService> factory = new ChannelFactory<IMyWCFService>(binding, address);
service = factory.CreateChannel();
}
}
Now, your WCF service project will contain a reference to the ServiceContract, and you will implement the interface here. 现在,您的WCF服务项目将包含对ServiceContract的引用,并且您将在此处实现该接口。
Your executables will contain references to the ServiceContract and ClientProxy projects. 您的可执行文件将包含对ServiceContract和ClientProxy项目的引用。
I hope this helps you. 我希望这可以帮助你。 Let me know if you need clarification on anything. 让我知道您是否需要任何澄清。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.