【问题标题】:Communication between windows service and wcf servicewindows服务与wcf服务之间的通信
【发布时间】:2016-01-08 14:00:00
【问题描述】:

我的情况:

我有一个 Windows 服务“A”和一个托管在其他 Windows 服务中的 wcf 服务“B”。在我的 Windows 服务 A 中,我创建了一个列表,然后我想将其传递给 wcf 服务 B。然后 wcf 服务 B 编辑此列表并将其传递回来。

我应该如何连接这两个服务?我必须在 wcf 服务中引用服务 A 吗?还是我必须从我的 Windows 服务 A 中创建一个客户端并添加服务引用?

感谢任何形式的帮助。我花了很多时间在 google stackoverflow 和 msdn 上搜索,但找不到 帮助我的东西。

编辑

Windows 服务

public long GetSize(string path)
    {
        DirectoryInfo direc = new DirectoryInfo(path);
        long size = CalculateDirectorySize(direc, true);

        return size;
    }

WCF 服务

using WindowsService;

WindowsServiceMethode wsm = new WindowsServiceMethod();

public long GetWcfCheck(string path)
        {
            long size = wsm.GetSize(path);

            return size;
        }

ASP.Net Web 应用程序

public ActionResult Index()
        {
            WCF.CommunicationServiceClient client = new WCF.CommunicationServiceClient("BasicHttpBinding_ICommunicationService");
            ViewBag.s = client.GetWcfCheck(@"_somepath_").ToString();


            return View("ShowView");
        }

这是传递数据的正确方式吗?

【问题讨论】:

    标签: c# .net wcf service


    【解决方案1】:

    这是我从你的问题中可以理解的:

    1. 您有 2 个 Windows 服务(为简单起见,Service1 和 Service2)
    2. Service1 托管 WCF 服务
    3. Service2 需要使用 WCF 服务的双向方法

    如果这一切都是正确的,以下是你应该做的一些事情:

    应将 WCF 服务配置为通过NetNamedPipes 进行连接,因为如那篇文章所述,

    NetNamedPipeBinding 提供安全可靠的绑定,并针对机器上的通信进行了优化。

    Service2需要在WCF服务中添加Service Reference,你可以找到怎么做here

    既然 Service1 知道 Service2(而 Service2 知道 Service1 绝对没有意义),您必须声明您的服务方法以允许双向通信。这是我所做的一个实现示例:

    [ServiceContract(Namespace = "http://Your.Namespace", SessionMode = SessionMode.Required)]
    public interface ICommunicator
    {
        [OperationContract(IsOneWay = false)]
        string StartServer(string serverData);
    } 
    
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Reentrant)]
    public class CommunicatorService : ICommunicator
    {
        public string StartServer(string serverData)
        {
            //example method that takes a string as input and returns another string
            return "Hello!!!!";
        }
    }
    

    编辑:添加客户端和服务器配置。但是,我必须告诉你,由于我遇到了多个不相关的 app.config 问题,因此客户端配置是通过代码完成的。

    服务器 app.config:

    <system.serviceModel>
    <services>
      <service behaviorConfiguration="MyNamespace.CommunicatorServiceBehavior" name="MyNamespace.CommunicatorService">
        <endpoint address="" binding="netNamedPipeBinding" name="netNamedPipeCommunicator" contract="MyNamespace.ICommunicator">
        </endpoint>
        <endpoint address="mex" binding="mexNamedPipeBinding" name="mexNamedPipeCommunicator" contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="net.pipe://localhost/CommunicatorService" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="MyNamespace.CommunicatorServiceBehavior">
          <serviceMetadata httpGetEnabled="false" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    

    客户代码:

    private static ICommunicator Proxy;
    ServiceEndpoint endpoint = new ServiceEndpoint(ContractDescription.GetContract(typeof(ICommunicator)))
    {
       Address = new EndpointAddress("net.pipe://localhost/CommunicatorService"),
       Binding = new NetNamedPipeBinding()
    };
    Proxy = (new DuplexChannelFactory<ICommunicator>(new InstanceContext(this), endpoint)).CreateChannel();
    ((IClientChannel)Proxy).Open();
    
    public void StartServer(string serverData)
    {
        string serverStartResult = Proxy.StartServer(serverData); // calls the method I showed above
    }
    

    【讨论】:

    • 您是否有链接来解释 app.config 文件的外观?
    • @Duck你想看服务端app.config还是客户端的配置?
    • 两者都很好:)
    • 我还有一个问题。我是 Wcf 的新手,理解起来有些困难。当我想将 Windows 服务中的方法的返回值传递给 wcf 服务时,我该如何正确执行?我已经做过一次了。我在wcf服务中引用了windows服务并调用了方法,你们是这样的吗?
    • @Duck 请查看我的更新,如果您有任何其他问题,请告诉我
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-10
    • 2012-06-05
    • 2012-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多