WCF Web服务通过HTTPS返回404,但通过HTTP

时间:2019-07-11 13:44:40

标签: .net wcf https

几年前,我有一个Web服务可以在http上正常运行。它可以通过Web浏览器查看服务说明,并且可以正确响应肥皂呼叫。现在,我们要将其移至https。

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

我正在使用Postman测试肥皂呼叫,因此这似乎表明问题出在web.config或IIS。

我发现许多帖子都涉及到与https上的WCF有关的问题,但到目前为止尚未成功解决。任何帮助将不胜感激。

如下所示,来自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 Web请求-确定

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

1 个答案:

答案 0 :(得分:0)

如果要通过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基址。
随时让我知道是否有什么可以帮助您的。