c# - WCF 使用反向代理

标签 c# wcf iis-7.5 firewall reverse-proxy

我为长篇大论道歉...
我们有一个在组织内部使用的用 .Net 4.0 编写的 WCF 服务。最近,需要在组织网络之外提供此服务。因此,网络人员使用反向代理使该服务在组织外部可用。这是如何设置的图片。

enter image description here

如图所示..服务http://mywebservice.com/readd.svc托管在 WINdows 2008 R2 上,其内部 IP 地址为 XXX.YYY.ZZZ.RRR。该服务托管在端口 80 上。

问题是什么 ?。

在内部,我可以通过 http://mywebservice.com/readd.svc 访问该服务使用 WCFTestClient 和浏览器。但如果我输入 http://mywebservice.com/readd.svc,则在组织之外我收到“404 文件找不到错误”,在 WCFTestCleint 中我收到错误为“错误:无法从 http://mywebservice.com/readd.svc 获取元数据……”。

但是,如果我像在浏览器中一样输入服务,我会看到显示肥皂链接的屏幕,如下所示。请忽略 LookUpService.svc 名称不匹配。
enter image description here

如上图所示...地址栏是 https ,但肥皂链接是 http 。如果我单击 http 链接 http://.... ?wsdl ,我会得到 not found 错误。这是因为反向代理只允许以 https 方式连接。

如果我使用 WCFTextClient 并将服务添加为 https://mywebsite.com/readd.svc我收到如下错误。

“错误:无法从 https://mywebsite.com/readd.svc 获取元数据如果这是 Windows (R)
您有权访问的通信基础服务,请检查您是否拥有
在指定地址启用元数据发布。如需帮助启用元数据发布,
请参阅 http://go.microsoft.com/fwlink/?LinkId=65455.WS-Metadata 上的 MSDN 文档交换错误
网址:http://mywebsite.com/readd.svc元数据包含无法解析的引用:
' https://mywebsite.com/readd.svc '。内容类型应用程序/soap+xml;不支持 charset=utf-8
按服务 https://mywebsite.com/readd.svc .
客户端和服务绑定(bind)可能不匹配。
远程服务器返回错误:(415) OK.HTTP GET Error
网址:https://mywebsite.com/readd.svc
网址为 http://mywebsite.com/readd.svc 的文档未被识别
作为已知文档类型。每种已知类型的错误消息可能会对您有所帮助
解决问题:- 来自“XML Schema”的报告是
'无法识别文档格式(内容类型为'text/html; charset=UTF-8')。'.-
来自' http://mywebsite.com/readd.svc 的报告' 是
'无法识别文档格式(内容类型为'text/html; charset=UTF-8')。'.-
来自“DISCO 文档”的报告是“下载时出错”http://mywebsite.com/readd.svc?disco '.'。 -
请求失败,HTTP 状态为 404:未找到。- 来自“WSDL 文档”的报告是“无法识别文档格式(内容类型为 'text/html; charset=UTF-8')。”。 "

网络人员通知我以 https 的形式提供服务。这是我的 web.config 文件








<behaviors>

  <serviceBehaviors>

    <behavior name="ServiceLookup.LookupServiceBehavior">
          <serviceMetadata httpGetEnabled="true" httpGetUrl=""/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
          <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
    </behavior>

  </serviceBehaviors>

</behaviors>

 <services>
    <service behaviorConfiguration="ServiceLookup.LookupServiceBehavior" name="SmallApp.ServiceLookUp.LookUpService">

       <endpoint address="" binding="basicHttpBinding" bindingConfiguration="BSBindingConfig" name="SmallApplianceBSEndPoint"
          contract="SmallApp.ServiceLookUp.ILookupService" />


       <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
    </service>
 </services>
<!-- Remove this during deployment-->
<!--<serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>-->

您能否建议我如何解决此问题..以便组织外部的用户可以使用 WCFTestCleint 作为 http/https 访问 api。

更新了 web.config 文件







    </binding>
    </basicHttpBinding>
</bindings>

<behaviors>

  <serviceBehaviors>

    <behavior name="ServiceLookup.LookupServiceBehavior">
          <serviceMetadata httpGetEnabled="true" httpGetUrl="" httpsGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
          <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
    </behavior>

  </serviceBehaviors>

</behaviors>

 <services>
    <service behaviorConfiguration="ServiceLookup.LookupServiceBehavior" name="SmallApp.ServiceLookUp.LookUpService">

       <endpoint address="" binding="basicHttpBinding" bindingConfiguration="BSBindingConfig" name="SmallApplianceBSEndPoint"
          contract="SmallApp.ServiceLookUp.ILookupService" />

       <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
       <endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange" />
    </service>
 </services>
<!-- Remove this during deployment-->
<serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>

我仍然在组织网络外部以 http 或 https 访问时出现错误

最佳答案

WSDL 通常是根据您的模型元数据、属性和主机位置动态生成的。因此,如果 wsdl 然后被代理到另一个环境,就会出现问题。
servicemetadata 中的一个逃生舱口是指定一个 externalMetadataLocation :

A Uri that contains the location of a WSDL file, which is returned to the user in response to WSDL and MEX requests instead of the auto-generated WSDL.


添加 externalMetadataLocation像下面这样:
<system.serviceModel>
  <behaviors>
    <serviceBehaviors>
      <behavior>
        <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" 
             externalMetadataLocation="https://example.com/SOAP/Service1.wsdl" />
      </behavior>
    </serviceBehaviors>
  </behaviors>
</system.serviceModel>
然后,您可以提前预生成 WSDL,随意调整,并上传修改后的文件作为契约(Contract)。
延伸阅读
  • Supply a different endpoint address in the WSDL of a WCF web service
  • WCF behind a public reverse proxy which is used for traffic encryption
  • WebService behind reverse proxy
  • WCF Webservice behind public reverse proxy
  • Fix the Endpoint Address When Using externalMetadataLocation
  • 关于c# - WCF 使用反向代理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23435476/

    相关文章:

    没有 WebServiceHostFactory 的 WCF REST 和 SOAP 服务

    asp-classic - 经典 ASP 中的 500 Vs 500.100 错误

    iis-7.5 - IISExpress 用户界面

    c# - DataAnnotations [Phone] 属性

    单个类类型的 C# 数组或列表

    asp.net-mvc-2 - 从 jQuery 调用启用 AJAX 的 WCF 服务 - MVC 2

    asp.net - 在 Win7 上的 .net 4 中,asp.net 需要对哪个帐户进行权限设置?

    c# - Dapper Multi Mapping 上的某些值返回 null

    c# - 在 NUnit.ConsoleRunner.3.2.1 中运行测试时,NUnit 运行器失败

    .NET 生成服务引用时加载错误的程序集