WCF 服务在内部工作但不在外部工作

WCF service works internally but not externally

我有一个框架 4 上的 WCF 服务在内部网络中运行良好。在外部我可以看到目录浏览,因为在 web.config 中启用但是一旦我单击 svc 文件就会抛出 404 "Server Error in '/' Application"。还有一个 DMZ 反向代理,我确定这是我的问题。我检查了 IIS-7 日志,没有记录 404 错误,只记录了来自内部访问的 200 OK。 IIS 有很多服务和站点 运行 但这是我公司第一次尝试使用带有 REST 服务的 WCF。几天来,我一直在寻找答案,只找到对我的项目不起作用的解决方案。下面是我的配置文件。在此先感谢您的帮助。

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.net>
<defaultProxy useDefaultCredentials="true"></defaultProxy>
</system.net>

<system.diagnostics>
<sources>
<source propagateActivity="true" name="System.ServiceModel"  switchValue="Warning,ActivityTracing">
 <listeners>
   <add type="System.Diagnostics.DefaultTraceListener" name="Default">
     <filter type="" />
      </add>
      <add name="ServiceModelTraceListener">
        <filter type="" />
      </add>
    </listeners>
  </source>
</sources>
<sharedListeners>
  <add initializeData="X:\RestService\DrillSvc\web_tracelog.svclog" type="System.Diagnostics.XmlWriterTraceListener, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" name="ServiceModelTraceListener" traceOutputOptions="Timestamp">
    <filter type="" />
  </add>
</sharedListeners>
</system.diagnostics>
<appSettings />
<connectionStrings>
<add name="cn" connectionString="Data Source =test ;  initial catalog=Drillers; User Id=userid; Password=password" providerName="System.Data.SqlClient" />
</connectionStrings>
<system.web>

<compilation debug="true" targetFramework="4.0">
  <assemblies>
    <add assembly="System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364123" />
  </assemblies>
</compilation>
<authentication mode="Windows" />
<pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID" />
    <identity impersonate="false" />
</system.web>

<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
  <!--<remove name="webDAVModule"/>-->
</modules>

<directoryBrowse enabled="true" />
</system.webServer>
<system.serviceModel>
<bindings>
  <webHttpBinding>
    <binding name="WCFwebBinding">
      <security mode="None">
        <transport clientCredentialType="None" proxyCredentialType="Windows"/>
      </security>
    </binding>  

  </webHttpBinding>
</bindings>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true">    </serviceHostingEnvironment>
<services>
  <service behaviorConfiguration="DrillServiceBehavior" name="DrillService">
    <endpoint address="" behaviorConfiguration="WCFDrillWebBehavior" binding="webHttpBinding" contract="IDrillService">
      <identity>
        <dns value="mycompany.com" />
      </identity>
    </endpoint>
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
  </service>
</services>
<behaviors>
  <endpointBehaviors>
    <behavior name="WCFDrillWebBehavior">
      <webHttp />
    </behavior>
  </endpointBehaviors>
  <serviceBehaviors>
    <behavior name="ServiceBehavior">
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="true" />
    </behavior>
    <behavior name="DrillServiceBehavior">
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="true" />
    </behavior>
  </serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>`

更新:我添加了一个简单的 HTML 页面,它可以从外部 public 工作,但 svc 文件仍然抛出 404 错误未找到。

如果不了解环境、代理或您测试服务的方式,很难做出肯定的判断,但这里有几件事需要检查:

  • 验证代理配置是否正确。
  • 验证该服务是否可以从代理服务器访问。

最后,如果您尝试通过 HTTPS 从外部浏览服务,则需要确保服务元数据在您的配置中通过 HTTPS 公开:

<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />

希望这可以帮助您指明正确的方向。

终于在做了各种更改之后我发现了问题。在 inetpub/wwwroot 下的 wcf 服务的托管内部服务器中,有一个专门针对 IIS 站点而不是服务 web.config 的配置文件。添加此代码后,它第一次尝试就成功了。 DMZ 无需更改。希望这会对其他人有所帮助。

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