簡體   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