【问题标题】:Host HTTP WCF Service in a Windows Service在 Windows 服务中托管 HTTP WCF 服务
【发布时间】:2018-04-22 21:39:42
【问题描述】:

我在 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>

【问题讨论】:

标签: c# wcf http


【解决方案1】:

服务元数据将 http://localhost:80/WCFService/service/ 发布到客户端。无法从本地主机外部访问此 URL。

为了使用公共 IP 从另一个网络访问服务,服务元数据应发布 http://PUBLIC_IP_ADDRESS/WCFService/service/ 到客户端。这可以根据客户端使用的 URL 动态完成。只需将 useRequestHeadersForMetadataAddress 添加到服务行为。

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

Auto-resolving a hostname in WCF Metadata Publishing

【讨论】:

    【解决方案2】:

    我怀疑当你给出这样的配置时:

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

    由于使用了 locahost,它将在环回地址中进行监听。将此更改为实际的公共 IP 地址(即不是 127.0.0.1)或服务器名称,然后再次检查。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-13
      • 2013-08-06
      • 2012-04-28
      相关资源
      最近更新 更多