簡體   English   中英

在Windows服務中托管HTTP WCF服務

[英]Host HTTP WCF Service in a Windows Service

我正在Windows服務中托管HTTP WCFService,在本地網絡中它可以正常工作,但是如果客戶端在另一個網絡中並嘗試使用公共IP連接則不起作用。

配置文件:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <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="WCFService.ServiceBehavior">
        <endpoint address="" binding="wsHttpBinding"     
          contract="WCFService.ServiceContract">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" 
        contract="IMetadataExchange" />
    <host>
      <baseAddresses>
        <add baseAddress="http://localhost:80/WCFService/service/" />
      </baseAddresses>
    </host>
  </service>
</services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, 
          set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="True" httpsGetEnabled="True"/>
      <serviceDebug includeExceptionDetailInFaults="False" />
    </behavior>
  </serviceBehaviors>
</behaviors>
  </system.serviceModel>

</configuration>

服務元數據將http:// localhost:80 / WCFService / service /發布到客戶端。 無法從本地主機外部訪問此URL。

為了使用公共IP從另一個網絡訪問服務,服務元數據應向客戶端發布http:// PUBLIC_IP_ADDRESS / WCFService / service / 可以根據客戶端使用的URL動態完成此操作。 只需將useRequestHeadersForMetadataAddress添加到服務行為即可。

<behaviors>
    <serviceBehaviors>
       <behavior name="...">
         ...
         <useRequestHeadersForMetadataAddress />
       </behavior>
    </serviceBehaviors>
</behaviors>

請參閱WCF元數據發布中的自動解析主機名

我懷疑當您提供這樣的配置時:

<add baseAddress="http://localhost:80/WCFService/service/" />

由於locahost的使用,它將在回送地址中偵聽。 將其更改為實際的公共IP地址(即不是127.0.0.1)或服務器名稱,然后再次檢查。

暫無
暫無

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

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