【问题标题】:WCF only accepts localhostWCF 只接受本地主机
【发布时间】:2015-01-17 12:46:52
【问题描述】:

我正在 Visual Studio Express 中编写我的第一个 WCF,并将其配置为在 IIS Express 下运行。我的 Web.config 如下所示。如果我在 http://localhost:50000/Service1.svc 上执行 Get 请求,但不能在 http://10.0.0.26:50000/Service1.svc 上执行请求,我可以从浏览器访问该服务,其中 10.0.0.26 是我的 IP。如何配置 IIS Express 的 WCF 以接受 IP 地址。最终我的服务可以通过网络到达。

<?xml version="1.0"?>
<configuration>

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5"/>
  </system.web>
  <system.serviceModel>
    <services>
    <service name="medSaveWCF.Service1">
      <endpoint address="../Service1.svc"
        binding="webHttpBinding"
        contract="medSaveWCF.IService1"
        behaviorConfiguration="webBehaviour" />
    </service>
  </services>

    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="webBehaviour">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <protocolMapping>
        <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>    
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*" />
        <add name="Access-Control-Allow-Headers" value="Content-Type, Accept" />
      </customHeaders>
    </httpProtocol> 
    <modules runAllManagedModulesForAllRequests="true"/>
    <!--
        To browse web app root directory during debugging, set the value below to true.
        Set to false before deployment to avoid disclosing web app folder information.
      -->
    <directoryBrowse enabled="true"/>
  </system.webServer>

</configuration>

【问题讨论】:

    标签: wcf iis-express


    【解决方案1】:

    (旁注:我已经在博客中介绍了这一点,包括使用 SSL 所需的额外设置步骤:http://blog.kutulu.org/2015/01/using-iis-express-with-remote-systems.html

    问题在于 IIS Express 默认只侦听 localhost 地址。原因是,IIS 作为用户进程运行,但使用与完整 IIS 相同的 HTTPD.SYS 系统库。默认情况下,HTTPD.SYS 配置不允许用户进程绑定到外部地址。要解决此问题,您需要做三件事:

    1. 编辑 IIS 配置以绑定到新端口
    2. 更新 HTTPD.SYS 以允许您的用户使用该新绑定。
    3. 告诉 WCF 您有多个绑定。

    第一步:IIS Express 设置

    IIS Express 配置直接通过 XML 配置文件完成,该文件位于:

    C:\Users\[username]\Documents\IISExpress\config\applicationhost.config
    

    如果您的项目已经设置为使用 IIS Express,您会在文件中找到大约 150 行开始的配置块——查找 XML &lt;sites&gt; 标记,您会找到 &lt;site&gt;元素:

    <site name="MySolution.MyProject" id="2">
        <application path="/" applicationPool="Clr4IntegratedAppPool">
            <virtualDirectory path="/"          
                              physicalPath="C:\Projects\MySolution\MyProject" />
        </application>
        <bindings>
            <binding protocol="http" bindingInformation="*:50000:localhost" />
        </bindings>
    </site>
    

    &lt;bindings&gt; 元素内部是 IIS Express 在运行该特定站点时绑定到的端口和主机名列表,您只需添加一个新的绑定元素:

            <binding protocol="http" bindingInformation="*:50000:10.0.0.26" />
    

    第二步:HTTPD.SYS 权限

    完全披露:如果您愿意以管理员用户身份运行 Visual Studio 和 IIS Express,则此步骤是可选的。但这违背了 IIS Express 的全部目的,它是一个 用户模式 Web 服务器,所以不要这样做。

    相反,您只需要使用netsh 命令重新配置 HTTPD.SYS 以允许您绑定到所需的端口。具体来说,需要使用http add urlacl命令。

    启动管理命令提示符和/或 PowerShell 提示符并执行以下操作:

    netsh http add urlacl url=http://10.0.0.26:5000 user=Everyone
    

    两者都完成后,关闭 IIS Express,以便 VS 重新启动它,您应该一切就绪。

    我为自己编写了一个小型 Powershell 脚本,以便为各种端口执行此操作:

    $LowPort = 50000
    $RangeSize = 99
    
    for ( $i = 0; $i -le $RangeSize; $i++ ) 
    {
      netsh http delete urlacl url="http://${IISHost}:$($LowPort + $i)/"
      netsh http add urlacl url="http://${IISHost}:$($LowPort + $i)/" user=Everyone
    }
    

    这样我就不用每次都记得这样做了,我只需要使用 50000 - 50100 范围内的端口即可。

    第三步:通知 WCF

    默认情况下,WCF 仅绑定到每个项目的一个站点。对于真正的 IIS,这很好,因为那是您可能想要的 *:80 绑定。对于 IIS Express,您需要为每个 IP 地址单独绑定,因此您需要告诉 WCF 全部使用它们。这很简单,只需将其添加到您的 WCF 配置中即可:

    <configuration>
      <system.serviceModel>
        <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
      </system.serviceModel>
    </configuration>
    

    所有这些都完成后,关闭 IIS Express 并让 VS 重新启动它,您应该一切就绪。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-23
      • 1970-01-01
      • 2019-07-04
      • 2019-09-24
      • 2011-11-26
      • 2017-02-28
      • 2017-12-16
      • 1970-01-01
      相关资源
      最近更新 更多