【问题标题】:WCF web service returning 404 over HTTPS but works over HTTPWCF Web 服务通过 HTTPS 返回 404,但通过 HTTP 工作
【发布时间】:2019-11-21 06:15:06
【问题描述】:

我有一个多年前编写的 Web 服务,它在 http 上运行良好。它通过网络浏览器工作以查看服务描述并正确响应肥皂调用。我们现在要将其移至 https。

我对 web.config 文件进行了一些更改,以尝试使其能够通过 https 工作。我现在可以在浏览器中通过 https 调用它并查看服务描述,但我无法通过 https 使用肥皂调用调用 Web 服务 - 我收到 404 错误。

我正在使用 Postman 测试肥皂调用,所以这似乎表明问题出在 web.config 或 IIS 上。

我发现很多帖子都存在与 WCF over https 相关的问题,但尚未成功解决此问题。任何帮助将不胜感激。

web.config 中的服务相关代码如下所示。

<system.serviceModel>
    <services>
  <service name="EComAPI" behaviorConfiguration="WCFAuthBehavior">
    <endpoint address="soap" binding="wsHttpBinding" contract="IEComAPI" bindingConfiguration="httpbinding1"></endpoint>
    <endpoint address="soap" binding="wsHttpBinding" contract="IEComAPI" bindingConfiguration="httpsbinding1"></endpoint>
    <endpoint address="rest" binding="webHttpBinding" contract="IEComAPI" behaviorConfiguration="rest" bindingConfiguration="httpbinding2"></endpoint>
    <endpoint address="rest" binding="webHttpBinding" contract="IEComAPI" behaviorConfiguration="rest" bindingConfiguration="httpsbinding2"></endpoint>
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"></endpoint>
  </service>
</services>

    <bindings>
      <wsHttpBinding>
        <binding name="httpbinding1">
          <security mode="None"></security>
        </binding>
        <binding name="httpsbinding1">
          <security mode="Transport">
            <transport clientCredentialType="None"></transport>
          </security>
        </binding>
      </wsHttpBinding>

      <webHttpBinding>
        <binding name="httpbinding2">
          <security mode="None">
          </security>
        </binding>
        <binding name="httpsbinding2">
          <security mode="Transport">
            <transport clientCredentialType="None"></transport>
          </security>
        </binding>
      </webHttpBinding>
    </bindings>

    <behaviors>
      <serviceBehaviors>
        <behavior name="WCFAuthBehavior">
          <serviceMetadata httpsGetEnabled="false" httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
        <behavior name="">
          <serviceMetadata httpsGetEnabled="true" httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="rest">
          <webHttp />
        </behavior>
      </endpointBehaviors>
    </behaviors>
  </system.serviceModel>

总结:

  • 对此服务的 http web 请求 - 好的
  • 对此服务的 http soap 请求 - 好的

  • 对该服务的 https 网络请求 - 好的

  • 对此服务的 https 肥皂请求 - 给出 404

【问题讨论】:

    标签: .net wcf https


    【解决方案1】:

    如果我们想通过 HTTPS 调用 SOAP 风格的服务,我们应该使用 Wshttpbinding 发布服务端点并指定客户端凭据类型(默认值为 windows)。

        <system.serviceModel>
          <services>
            <service name="VM1.MyService" behaviorConfiguration="mybehavior">
              <endpoint address="" binding="wsHttpBinding" contract="VM1.IService" bindingConfiguration="mybinding"></endpoint>
              <endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange"></endpoint>
            </service>
          </services>
          <bindings>
            <wsHttpBinding>
              <binding name="mybinding">
                <security mode="Transport">
                  <transport clientCredentialType="None"></transport>
                </security>
              </binding>
            </wsHttpBinding>
          </bindings>
          <behaviors>
            <serviceBehaviors>
              <behavior name="mybehavior">
                <serviceMetadata />
              </behavior>
            </serviceBehaviors>
          </behaviors>
    </system.serviceModel>
    

    然后在 IIS 站点绑定模块中指定一个 https 端点。
    如果我们想以Restful风格发布服务,我们应该使用WebHttpBinding。
    https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/wcf-web-http-programming-model
    最后我做了一个类似的配置,支持SOAP风格和Restful风格(需要在操作方法中添加webget/webinvoke),同时支持https和http。

    <system.serviceModel>
          <services>
            <service name="VM1.MyService" behaviorConfiguration="mybehavior">
              <endpoint address="soap” binding="wsHttpBinding" contract="VM1.IService" bindingConfiguration="httpbinding1"></endpoint>
              <endpoint address="soap” binding="wsHttpBinding" contract="VM1.IService" bindingConfiguration="httpsbinding1"></endpoint>
              <endpoint address="rest” binding="webHttpBinding" contract="VM1.IService" behaviorConfiguration="rest" bindingConfiguration="httpbinding2"></endpoint>
              <endpoint address="rest” binding="webHttpBinding" contract="VM1.IService" behaviorConfiguration="rest" bindingConfiguration="httpsbinding2"></endpoint>
              <endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange" ></endpoint>
            </service>
          </services>
          <bindings>
            <wsHttpBinding>
              <binding name="httpbinding1">
                <security mode="None"></security>
              </binding>
              <binding name="httpsbinding1">
                <security mode="Transport">
                  <transport clientCredentialType="None"></transport>
                </security>
              </binding>
            </wsHttpBinding>
            <webHttpBinding>
              <binding name="httpbinding2">
                <security mode="None">
                </security>
              </binding>
              <binding name="httpsbinding2">
                <security mode="Transport">
                  <transport clientCredentialType="None"></transport>
                </security>
              </binding>
            </webHttpBinding>
          </bindings>
          <behaviors>
            <serviceBehaviors>
              <behavior name="mybehavior">
                <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
              </behavior>
            </serviceBehaviors>
            <endpointBehaviors>
              <behavior name="rest">
                <webHttp />
              </behavior>
            </endpointBehaviors>
          </behaviors>
        </system.serviceModel>
    

    如果我们将服务托管在 IIS 中,请不要忘记在 IIS 站点绑定模块中指定 Http 和 Https 基地址。
    如果有什么可以帮助的,请随时告诉我。

    【讨论】:

    • 感谢您的帮助。我试图使我的配置尽可能与您的相似,但它仍然给我一个 404 用于通过 HTTPS 进行的 SOAP 调用。你能看到我的配置有什么问题吗?我已经用更新的配置更新了我的问题。谢谢。
    • 抱歉,endpoints基地址配置有问题,已更新。
    • 谢谢,但同样的问题很遗憾
    • 发送soap quest时你使用哪种方式调用服务? 404表示服务地址可能错误或托管服务有问题。
    • 我正在使用一个名为 Postman 的应用程序来发送肥皂电话。它在 http 上运行良好,所以我认为肥皂电话不是问题。也许呼叫最终会转到另一个也在服务器上使用 https 的站点。所有站点都使用相同的 IP,但通过 SNI 使用不同的证书。服务器是 2012 年,我的开发电脑是 Win 10,所以我认为 SNI 支持不是问题。
    猜你喜欢
    • 1970-01-01
    • 2012-05-27
    • 2020-04-19
    • 1970-01-01
    • 2014-12-20
    • 2013-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多