繁体   English   中英

Windows Service和WCF Service之间的通信

[英]Communication between windows service and wcf service

我的情况:

我有一个Windows服务“ A”和另一个Windows服务中托管的WCF服务“ B”。 在我的Windows服务AI中,创建一个列表,然后将其传递给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 Webapp

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


            return View("ShowView");
        }

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

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

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

如果所有这些都正确,那么您应该做一些事情:

WCF服务应配置为通过NetNamedPipes连接,因为如该文章所述,

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

Service2需要向WCF服务添加服务引用 ,您可以在此处找到如何进行操作。

现在,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
}

暂无
暂无

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

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