简体   繁体   English

从解决方案中的多个项目中使用相同的WCF服务

[英]Consume same WCF Service from multiple projects in solution

Situation: 情况:

I have a solution of multiple projects: 我有多个项目的解决方案:

  • Executable App #1 可执行应用程序#1
  • Executable App #2 可执行应用程序2
  • WCF Project WCF项目
  • Shared Project 共享项目
  • "Other" Project “其他”项目
  • ... ...

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个服务引用:

  • Executable App #1 requires only the endpoint (proxy class of shared project could be used) 可执行应用程序#1仅需要端点(可以使用共享项目的代理类)
  • Executable App #2 requires only the endpoint (same as above) 可执行应用程序2仅需要端点(与上面相同)
  • Shared Project requires only the proxy class 共享项目仅需要代理类

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. 我认为它提供了很好的关注点分离,使您不必在所有可执行文件中生成代理类。

  • Create a ServiceContract project. 创建一个ServiceContract项目。 This will contain the interface that defines the contract for your WCF service. 这将包含定义WCF服务合同的接口。
  • Create a ClientProxy project, and include a ClientProxy class. 创建一个ClientProxy项目,并包括一个ClientProxy类。 This project will reference your ServiceContact, and will set up the bindings for your WCF service. 该项目将引用您的ServiceContact,并将为WCF服务设置绑定。 I recommend that you pass the URI of your WCF service into this class in the constructor. 我建议您将WCF服务的URI传递给构造函数中的此类。

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.

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