簡體   English   中英

如何在托管Windows服務中托管WCF服務?

[英]How to Host a WCF Service in a Managed Windows Service?

我的解決方案中有3個項目。

  • 測試客戶端=>以添加引用並通過tcp ip訪問
  • WcfServiceLibruary1 =>用於執行我的methots
  • WindowsService1 =>用於作為Windows服務安裝並運行(帳戶:網絡服務,啟動類型:自動)

在此處輸入圖片說明

我在msdn示例中使用了所有相同的代碼

http://msdn.microsoft.com/en-us/library/ff649818.aspx

我使用的wcf服務有2個methots。我想在托管Windows服務中使用此wcf服務。我在解決方案中添加了Windows服務並設置了引用內容。

我在wcf-app.config上使用此地址引用:

net.tcp://localhost:2023/Service1

現在的問題是:

我成功使用添加了對我的測試客戶端項目的引用

net.tcp://localhost:2023/Service1

但是此引用地址不能在Windows服務中安裝!!! 當我將其安裝為Windows服務時,無法訪問該地址,並且出現此錯誤: No connection could be made because the target machine actively refused it

WcfServiceLibruary app.config:

<?xml version="1.0"?>
  <configuration>
    <system.web>
    <compilation debug="true"/>
  </system.web>
  <!-- When deploying the service library project, the content of the config file must be added to the host's 
  app.config file. System.Configuration does not support config files for libraries. -->
  <system.serviceModel>
    <services>
      <service name="WcfServiceLibrary1.Service1">
        <endpoint address="" binding="netTcpBinding" bindingConfiguration="" contract="WcfServiceLibrary1.IService1">
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexTcpBinding" bindingConfiguration="" contract="IMetadataExchange"/>
        <host>
          <baseAddresses>
            <add baseAddress="net.tcp://localhost:2023/Service1"/>
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="">
          <serviceMetadata httpGetEnabled="false"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>

我的Windows服務:

protected override void OnStart(string[] args)
{
    if (myServiceHost != null)
    {
        myServiceHost.Close();
    }
    myServiceHost = new ServiceHost(typeof(Service1));
    myServiceHost.Open();
 }

當我在visualstudio服務主機上啟動時,一切工作正常: 在此處輸入圖片說明

閱讀本文

在此構建服務並提供托管代碼

http://msdn.microsoft.com/en-us/library/ms733069.aspx

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM