简体   繁体   English

WCF 使用反向代理

[英]WCF using Reverse proxy

My apologize for long post... We have a WCF service written in .Net 4.0 used internally within the organization.我为长篇文章道歉...我们有一个用 .Net 4.0 编写的 WCF 服务,在组织内部使用。 Recently there was a requirement to make this service available outside the organization network.最近,需要在组织网络之外提供此服务。 So the network guys used reverse proxy to make this service available outside the organization.因此,网络人员使用反向代理使该服务在组织外部可用。 Here is the picture how it is set up.这是如何设置的图片。

在此处输入图片说明

As you can see in the picture..the service http://mywebservice.com/readd.svc is hosted on WIndows 2008 R2 which has a internal IP address as XXX.YYY.ZZZ.RRR.正如您在图片中看到的..服务http://mywebservice.com/readd.svc托管在 WINdows 2008 R2 上,其内部 IP 地址为 XXX.YYY.ZZZ.RRR。 The service is hosted on port 80.该服务托管在端口 80 上。

what is the problem ?.问题是什么 ?。

Internally I can access the service as http://mywebservice.com/readd.svc using WCFTestClient and browser.在内部,我可以使用 WCFTestClient 和浏览器以http://mywebservice.com/readd.svc 的形式访问该服务。 But outside the organization if I type http://mywebservice.com/readd.svc I get "404 file not find error" and in WCFTestCleint I get error as "Error: Cannot obtain Metadata from http://mywebservice.com/readd.svc .........".但是在组织外部,如果我输入http://mywebservice.com/readd.svc我得到“404 文件未找到错误”并且在 WCFTestCleint 我得到错误为“错误:无法从http://mywebservice.com/readd获取元数据.svc …………”。

However if I type in the service as in browser I get the screen which shows soap link as shown below.但是,如果我在浏览器中输入服务,我会看到如下所示的显示soap链接的屏幕。 Please ignore the LookUpService.svc name mismatch.请忽略 LookUpService.svc 名称不匹配。在此处输入图片说明

As you can see in above figure ...address bar is https , but the soap link is http .正如您在上图中所见...地址栏是 https ,而soap 链接是 http 。 If I click the http link http://.... ?wsdl , I get not found error.如果我单击 http 链接 http://.... ?wsdl ,则会出现未找到错误。 This is since Reverse proxy only allows connection as https .这是因为反向代理只允许作为 https 连接。

If I use WCFTextClient and add servicie as https://mywebsite.com/readd.svc I get error as below.如果我使用 WCFTextClient 并将服务添加为https://mywebsite.com/readd.svc ,则会出现如下错误。

"Error: Cannot obtain Metadata from https://mywebsite.com/readd.svc If this is a Windows (R) Communication Foundation service to which you have access, please check that you have enabled metadata publishing at the specified address. For help enabling metadata publishing, please refer to the MSDN documentation at http://go.microsoft.com/fwlink/?LinkId=65455.WS-Metadata Exchange Error URI: http://mywebsite.com/readd.svc Metadata contains a reference that cannot be resolved: ' https://mywebsite.com/readd.svc '. Content Type application/soap+xml; charset=utf-8 was not supported by service https://mywebsite.com/readd.svc . “错误:无法从https://mywebsite.com/readd.svc获取元数据如果这是您有权访问的 Windows (R) Communication Foundation 服务,请检查您是否已在指定地址启用元数据发布。寻求帮助启用元数据发布,请参阅 MSDN 文档,网址http://go.microsoft.com/fwlink/?LinkId=65455.WS-Metadata Exchange 错误 URI: http : //mywebsite.com/readd.svc元数据包含参考无法解析:“ https://mywebsite.com/readd.svc ”。内容类型 application/soap+xml;服务https://mywebsite.com/readd.svc不支持 charset=utf-8。
The client and service bindings may be mismatched.客户端和服务绑定可能不匹配。
The remote server returned an error: (415) OK.HTTP GET Error远程服务器返回错误:(415) OK.HTTP GET Error
URI: https://mywebsite.com/readd.svc URI: https : //mywebsite.com/readd.svc
The document at the url http://mywebsite.com/readd.svc was not recognized as a known document type.The error message from each known type may help you fix the problem:- Report from 'XML Schema' is 'The document format is not recognized (the content type is 'text/html; charset=UTF-8').'.- Report from ' http://mywebsite.com/readd.svc ' is 'The document format is not recognized (the content type is 'text/html; charset=UTF-8').'.- Report from 'DISCO Document' is 'There was an error downloading ' http://mywebsite.com/readd.svc?disco '.'. url http://mywebsite.com/readd.svc 上的文档未被识别为已知文档类型。来自每个已知类型的错误消息可能会帮助您解决问题:-“XML 架构”报告为“文档”格式无法识别(内容类型为“text/html;charset=UTF-8”)。.- 来自“ http://mywebsite.com/readd.svc ”的报告为“文档格式无法识别(内容类型为“文本/html;字符集=UTF-8”)。.- 来自“DISCO 文档”的报告是“下载时出错” http://mywebsite.com/readd.svc?disco '.'。 - The request failed with HTTP status 404: Not Found.- Report from 'WSDL Document' is 'The document format is not recognized (the content type is 'text/html; charset=UTF-8').'. - 请求失败,HTTP 状态为 404:未找到。- 来自“WSDL 文档”的报告为“无法识别文档格式(内容类型为 'text/html; charset=UTF-8')。”。 "

I was informed by network guys to make service available as https.网络人员通知我以 https 形式提供服务。 Here is my web.config file这是我的 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"/>-->

Can you suggest how I can resole this issue ..so that users outside organization can access the api with WCFTestCleint as http/https .您能否建议我如何解决此问题..以便组织外部的用户可以使用 WCFTestCleint 作为 http/https 访问 api。

Updated web.config file更新的 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"/>

I am still gettign error accessing as http or https outside organization network我在组织网络外部以 http 或 https 访问时仍然出现错误

The WSDL is typically generated on the fly based on your models metadata, attributes, and host location. WSDL 通常是根据您的模型元数据、属性和主机位置动态生成的。 So if the wsdl is then proxied to another environment, it's going to have issues.因此,如果随后将 wsdl 代理到另一个环境,则会出现问题。

One escape hatch in servicemetadata is to specify a externalMetadataLocation : 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.一个包含 WSDL 文件位置的 Uri,它会返回给用户以响应 WSDL 和 MEX 请求,而不是自动生成的 WSDL。

Add externalMetadataLocation like this below:添加externalMetadataLocation如下所示:

<system.serviceModel>
  <behaviors>
    <serviceBehaviors>
      <behavior>
        <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" 
             externalMetadataLocation="https://example.com/SOAP/Service1.wsdl" />
      </behavior>
    </serviceBehaviors>
  </behaviors>
</system.serviceModel>

You can then pre-generate your WSDL ahead of time, tweak at will, and upload the modified file to serve as the contract.然后,您可以提前预先生成您的 WSDL,随意调整,并上传修改后的文件以作为合同。

Further Reading进一步阅读

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

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