【问题标题】:How to configure WCF REST to work with SSL?如何配置 WCF REST 以使用 SSL?
【发布时间】:2012-12-11 22:29:40
【问题描述】:

我有一个很好的 WCF REST-ful JSON 服务,它运行良好。当我尝试将其用于 HTTPS 时,就会出现问题。 Web.config如下:

 <system.serviceModel>
    <bindings>
      <webHttpBinding>
        <binding name="StreamedRequestWebBinding" bypassProxyOnLocal="true" useDefaultWebProxy="false" hostNameComparisonMode="WeakWildcard" sendTimeout="10:15:00" openTimeout="10:15:00" receiveTimeout="10:15:00" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" maxBufferPoolSize="2147483647" transferMode="StreamedRequest">
          <security mode="Transport">
            <transport clientCredentialType = "None"  />
          </security>
          <readerQuotas maxArrayLength="2147483647" maxStringContentLength="2147483647" />
        </binding>
      </webHttpBinding>
    </bindings>
    <services>
      <service name="WcfRESTFullJSON.Service1" behaviorConfiguration="ServiceBehaviour">
        <endpoint address="" binding="webHttpBinding" bindingConfiguration="StreamedRequestWebBinding" contract="WcfRESTFullJSON.IService1" />
        <endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehaviour">
          <serviceMetadata httpsGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="web">
          <!--Him-->
          <webHttp automaticFormatSelectionEnabled="true" />
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true" />
  </system.serviceModel>

我在 IIS 上配置了一个自制的证书。 在我的客户端上:

WebClient WC;
...
WC.UploadData(URL, "POST", asciiBytes);

我们得到了什么:

The remote server returned an error: (500) Internal Server Error.

可能是什么问题?

【问题讨论】:

  • 您的服务中有任何登录方式吗?你也可以enable WCF tracing看看发生了什么。
  • System.ServiceModel.FaultException:由于 EndpointDispatcher 的 AddressFilter 不匹配,消息 ... 无法在接收方处理。检查发送方和接收方的 EndpointAddresses 是否一致... 当 'system.serviceModel/serviceHostingEnvironment/multipleSiteBindingsEnabled' 在配置中设置为 true 时,端点需要指定相对地址。如果您在端点上指定相对侦听 URI,则地址可以是绝对地址。要解决此问题,请为端点指定一个相对 uri..
  • ... 如何指定相对地址?非常感谢。
  • 现在剩下的错误是:System.InvalidOperationException:找不到与绑定 MetadataExchangeHttpsBinding 的端点的方案 https 匹配的基地址。注册的基地址方案是 [http]。
  • 在 IIS 中为您的站点添加 https 绑定。

标签: wcf rest ssl


【解决方案1】:

问题已解决。以下行中缺少对端点行为的引用 ...端点 behaviorConfiguration="web" address="" binding="webHttpBinding" bindingConfiguration="StreamedRequestWebBinding" contract="WcfRESTFullJSON.IService1"... 在下面的 Web.config 中。感谢各位朋友的帮助!解决方法终于在Implementing 5 important principles of REST using WCF Services文章中找到了

    <services>
  <service name="WcfRESTFullJSON.Service1" behaviorConfiguration="ServiceBehaviour">
    <endpoint  behaviorConfiguration="web" address="" binding="webHttpBinding" bindingConfiguration="StreamedRequestWebBinding" contract="WcfRESTFullJSON.IService1" />
    <endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange" />
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior name="ServiceBehaviour">
      <serviceMetadata httpsGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="false" />
    </behavior>
  </serviceBehaviors>
  <endpointBehaviors>
    <behavior name="web">
      <!--Him-->
      <webHttp automaticFormatSelectionEnabled="true" />
    </behavior>
  </endpointBehaviors>

【讨论】:

    【解决方案2】:

    由于 SSL 是 HTTP 下的协议,因此通常无需在 web.config 中配置任何额外内容(只有一个新绑定)。如果您使用 SSL,另一个会发生变化的是端口……您可能需要更新防火墙或检查您的请求是否发送到正确的端口。

    【讨论】:

    • 请求被发送到正确的端口(443)。在提琴手中验证。关闭防火墙不会改变任何事情。
    • 当我尝试使用 IIS 在 Visual Studio 中进行调试时,我收到消息:“无法在 Web 服务器上开始调试。Web 服务器配置不正确。有关常见配置错误,请参阅帮助。 ..” 我在 2 台机器上配置,行为是一样的。
    【解决方案3】:
     See Complete config file
    <configuration>   <system.web>
            <compilation debug="true" targetFramework="4.0" />
            <webServices>
              <protocols>
                <add name="HttpsGet"/>
                <add name="HttpsPost"/>
              </protocols>
            </webServices>   </system.web>   <system.serviceModel>
            <services>
              <service  name="RESTTEST.RestTestService" behaviorConfiguration="ServiceBehavior">
    
                <endpoint address="" binding="webHttpBinding" 
                          contract="RESTTEST.IRestTestService" 
                          bindingConfiguration="sslBinding"
                          behaviorConfiguration="web"
                          ></endpoint>
                <endpoint address="mex"
                      binding="mexHttpsBinding"
                      contract="IMetadataExchange" />
              </service>
            </services>
            <behaviors>
              <serviceBehaviors>
                <behavior name="ServiceBehavior">
                  <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above 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="true"/>
                </behavior>
              </serviceBehaviors>
              <endpointBehaviors>
                <behavior name="web">
                  <webHttp/>
                </behavior>
              </endpointBehaviors>
            </behaviors>
            <bindings>
              <webHttpBinding>
                <binding name="sslBinding" crossDomainScriptAccessEnabled="true">
                  <security mode="Transport">
                  </security>
                </binding>
              </webHttpBinding>    
              </bindings>
            <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="false">
            </serviceHostingEnvironment>
            <standardEndpoints>
              <webHttpEndpoint>
                <standardEndpoint crossDomainScriptAccessEnabled="true"></standardEndpoint>
              </webHttpEndpoint>
              <webScriptEndpoint>
                <standardEndpoint name="" crossDomainScriptAccessEnabled="true" />
              </webScriptEndpoint>
            </standardEndpoints>   </system.serviceModel>   <system.webServer>
            <modules runAllManagedModulesForAllRequests="true" />
            <httpProtocol>
              <customHeaders>
                <add name="Access-Control-Allow-Origin" value="*" />
                <add name="Access-Control-Allow-Headers" value="Content-Type" />
              </customHeaders>
            </httpProtocol>   </system.webServer> </configuration>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-21
      • 1970-01-01
      • 2011-04-04
      • 1970-01-01
      • 1970-01-01
      • 2011-09-12
      • 2018-05-07
      相关资源
      最近更新 更多