简体   繁体   English

在Windows服务中托管HTTP WCF服务

[英]Host HTTP WCF Service in a Windows Service

I'm hosting a HTTP WCFService in a Windows Service, in local network it works perfectly, but if the client is in another network and try to connect with de public IP doesn't work. 我正在Windows服务中托管HTTP WCFService,在本地网络中它可以正常工作,但是如果客户端在另一个网络中并尝试使用公共IP连接则不起作用。

Config file: 配置文件:

<?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>

The service metadata publishes http:// localhost:80 /WCFService/service/ to the client. 服务元数据将http:// localhost:80 / WCFService / service /发布到客户端。 This URL is not accessible from outside the local host. 无法从本地主机外部访问此URL。

In order to access the service from another network using a public IP the service metadata should publish http:// PUBLIC_IP_ADDRESS /WCFService/service/ to the client. 为了使用公共IP从另一个网络访问服务,服务元数据应向客户端发布http:// PUBLIC_IP_ADDRESS / WCFService / service / This can be done dynamically depending on the URL used by the client. 可以根据客户端使用的URL动态完成此操作。 Just add useRequestHeadersForMetadataAddress to service behaviors. 只需将useRequestHeadersForMetadataAddress添加到服务行为即可。

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

See Auto-resolving a hostname in WCF Metadata Publishing . 请参阅WCF元数据发布中的自动解析主机名

I suspect that when you give a config like this: 我怀疑当您提供这样的配置时:

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

It would be listening in the loopback address due to the usage of locahost . 由于locahost的使用,它将在回送地址中侦听。 Change this to the actual public IP address (ie, not 127.0.0.1) or Server name and check again. 将其更改为实际的公共IP地址(即不是127.0.0.1)或服务器名称,然后再次检查。

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

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