通过 HTTPS 和 HTTP 访问时,WCF 服务不工作

Posted

技术标签:

【中文标题】通过 HTTPS 和 HTTP 访问时,WCF 服务不工作【英文标题】:WCF service not working when accessed over HTTPS and HTTP 【发布时间】:2014-12-20 21:44:30 【问题描述】:

我在 IIS 中安装了 SSL 证书。

现在,当我导航到我的域 https://www.example.com/ 时,页面加载正确。 但是,当我尝试通过 https:https://www.example.com/service.svc/newprofile/?id=8&ipaddress=124.162.13.109 访问 Web 服务(通常通过 http 完美运行)时,我得到:

The resource cannot be found.

Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable.  Please review the following URL and make sure that it is spelled correctly. 

Requested URL: /service.svc/newprofile/

我也检查了这里

Webservice over SSL endpoint not found with 404 create asp.net webservice for SSL HTTPS Moved my ASP.NET website to IIS 8 on windows server 2012... services missing: .svc files are viewable, but their methods give a 404

但是将 webHttpBinding 重命名为 basicHttpBinding 会在与 jsonp 结合使用时引发不受支持的错误。 此外,web.config 中的服务元素名称与实现我的合同的类的完全限定名称相匹配。

这是我的 web.config

  <system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
<bindings>
  <webHttpBinding>
    <binding name="webHttpBindingWithJsonP" crossDomainScriptAccessEnabled="true">          
    </binding>
    <binding name="webHttpsBindingWithJsonP" crossDomainScriptAccessEnabled="true">
      <security mode="Transport"></security>
    </binding>        
  </webHttpBinding>
</bindings>
<client />
<services>
  <service name="RestService.service">
    <endpoint behaviorConfiguration="webHttp" binding="webHttpBinding" contract="RestService.Iservice" />
    <endpoint address="/service.svc" binding="webHttpBinding" bindingConfiguration="webHttpBindingWithJsonP" contract="RestService.Iservice" />
    <endpoint address="/service.svc" binding="webHttpBinding" bindingConfiguration="webHttpsBindingWithJsonP" contract="RestService.Iservice" />
  </service>
</services>
    <behaviors>
    <serviceBehaviors>
      <behavior name="">
        <serviceMetadata httpGetEnabled="true" />
        <serviceDebug includeExceptionDetailInFaults="true" />
      </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="webHttp">
          <webHttp />
        </behavior>
        <behavior name="webHttpBehavior">
          <webHttp />
        </behavior>        
      </endpointBehaviors>
    </behaviors>
  </system.serviceModel>

我查看了wsdl:http://www.example.com/service.svc?wsdl

<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsx="http://schemas.xmlsoap.org/ws/2004/09/mex" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsa10="http://www.w3.org/2005/08/addressing" xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsap="http://schemas.xmlsoap.org/ws/2004/08/addressing/policy" xmlns:msc="http://schemas.microsoft.com/ws/2005/12/wsdl/contract" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://tempuri.org/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" name="service" targetNamespace="http://tempuri.org/">
<wsdl:types>
    <xsd:schema targetNamespace="http://tempuri.org/Imports">
        <xsd:import schemaLocation="http://www.example.com/service.svc?xsd=xsd0" namespace="http://tempuri.org/"/>
        <xsd:import schemaLocation="http://www.example.com/service.svc?xsd=xsd1" namespace="http://schemas.microsoft.com/2003/10/Serialization/"/>
        <xsd:import schemaLocation="http://www.example.com/service.svc?xsd=xsd2" namespace="http://schemas.datacontract.org/2004/07/RestService"/>
        <xsd:import schemaLocation="http://www.example.com/service.svc?xsd=xsd3" namespace="http://schemas.microsoft.com/Message"/>
        <xsd:import schemaLocation="http://www.example.com/service.svc?xsd=xsd4" namespace="http://schemas.datacontract.org/2004/07/AjaxControlToolkit"/>
        <xsd:import schemaLocation="http://www.example.com/service.svc?xsd=xsd5" namespace="http://schemas.microsoft.com/2003/10/Serialization/Arrays"/>
    </xsd:schema>
</wsdl:types>
<wsdl:message name="Iservice_newProfile_InputMessage">
    <wsdl:part name="parameters" element="tns:newProfile"/>
</wsdl:message>

当我检查 https 时:https://www.example.com/service.svc?wsdl

<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsx="http://schemas.xmlsoap.org/ws/2004/09/mex" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsa10="http://www.w3.org/2005/08/addressing" xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsap="http://schemas.xmlsoap.org/ws/2004/08/addressing/policy" xmlns:msc="http://schemas.microsoft.com/ws/2005/12/wsdl/contract" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://tempuri.org/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" name="service" targetNamespace="http://tempuri.org/">
<wsdl:types>
<xsd:schema targetNamespace="http://tempuri.org/Imports">
<xsd:import schemaLocation="https://www.example.com/service.svc?xsd=xsd0" namespace="http://tempuri.org/"/>
<xsd:import schemaLocation="https://www.example.com/service.svc?xsd=xsd1" namespace="http://schemas.microsoft.com/2003/10/Serialization/"/>
<xsd:import schemaLocation="https://www.example.com/service.svc?xsd=xsd2" namespace="http://schemas.datacontract.org/2004/07/RestService"/>
<xsd:import schemaLocation="https://www.example.com/service.svc?xsd=xsd3" namespace="http://schemas.microsoft.com/Message"/>
<xsd:import schemaLocation="https://www.example.com/service.svc?xsd=xsd4" namespace="http://schemas.datacontract.org/2004/07/AjaxControlToolkit"/>
<xsd:import schemaLocation="https://www.example.com/service.svc?xsd=xsd5" namespace="http://schemas.microsoft.com/2003/10/Serialization/Arrays"/>
</xsd:schema>
</wsdl:types>
<wsdl:message name="Iservice_newProfile_InputMessage">
<wsdl:part name="parameters" element="tns:newProfile"/>
</wsdl:message> 

本例中的服务定义为:

    <OperationContract()> _
<Web.WebInvoke(Method:="GET", ResponseFormat:=Web.WebMessageFormat.Json, BodyStyle:=Web.WebMessageBodyStyle.Bare, _
       UriTemplate:="newprofile/?id=id&ipaddress=ipaddress")> _
        Function newProfile(ByVal Id As String,  Optional ByVal IPAddress As String = "") As service.Profile

我的网络服务

Namespace RestService
    <AspNetCompatibilityRequirements(RequirementsMode:=AspNetCompatibilityRequirementsMode.Allowed)> _
    Public Class service
        Implements Iservice

        'my code
    End Class
End Namespace   

我也在我的 web.config 中启用了跟踪,这是日志的一部分(所以不允许全部发布):

<E2ETraceEvent
    xmlns="http://schemas.microsoft.com/2004/06/E2ETraceEvent">
    <System
        xmlns="http://schemas.microsoft.com/2004/06/windows/eventlog/system">
        <EventID>262155</EventID>
        <Type>3</Type>
        <SubType Name="Error">0</SubType>
        <Level>2</Level>
        <TimeCreated SystemTime="2014-10-27T19:07:08.0773174Z" />
        <Source Name="System.ServiceModel" />
        <Correlation ActivityID="f7c83e1b-3e28-4e4f-8978-23f5073be422" />
        <Execution ProcessName="w3wp" ProcessID="1428" ThreadID="19" />
        <Channel/>
        <Computer>MYPC</Computer>
    </System>
    <ApplicationData>
        <TraceData>
            <DataItem>
                <TraceRecord
                    xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Error">
                    <TraceIdentifier>http://msdn.microsoft.com/nl-NL/library/System.ServiceModel.Channels.HttpChannelMessageReceiveFailed.aspx</TraceIdentifier>
                    <Description>Failed to lookup a channel to receive an incoming message. Either the endpoint or the SOAP action was not found.</Description>
                    <AppDomain>/LM/W3SVC/1/ROOT-2-130589104148009222</AppDomain>
                    <Source>System.ServiceModel.Activation.HostedHttpTransportManager/33997547</Source>
                    <ExtendedData
                        xmlns="http://schemas.microsoft.com/2006/08/ServiceModel/StringTraceRecord">
                        <IsRecycling>False</IsRecycling>
                    </ExtendedData>
                </TraceRecord>
            </DataItem>
        </TraceData>
    </ApplicationData>
</E2ETraceEvent>
<E2ETraceEvent
    xmlns="http://schemas.microsoft.com/2004/06/E2ETraceEvent">
    <System
        xmlns="http://schemas.microsoft.com/2004/06/windows/eventlog/system">
        <EventID>131074</EventID>
        <Type>3</Type>
        <SubType Name="Error">0</SubType>
        <Level>2</Level>
        <TimeCreated SystemTime="2014-10-27T19:07:08.0803197Z" />
        <Source Name="System.ServiceModel" />
        <Correlation ActivityID="00000000-0000-0000-0000-000000000000" />
        <Execution ProcessName="w3wp" ProcessID="1428" ThreadID="19" />
        <Channel/>
        <Computer>MYPC</Computer>
    </System>
    <ApplicationData>
        <TraceData>
            <DataItem>
                <TraceRecord
                    xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Error">
                    <TraceIdentifier>http://msdn.microsoft.com/nl-NL/library/System.ServiceModel.Diagnostics.EventLog.aspx</TraceIdentifier>
                    <Description>Wrote to the EventLog.</Description>
                    <AppDomain>/LM/W3SVC/1/ROOT-2-130589104148009222</AppDomain>
                    <ExtendedData
                        xmlns="http://schemas.microsoft.com/2006/08/ServiceModel/DictionaryTraceRecord">
                        <CategoryID.Name>EventLogCategory</CategoryID.Name>
                        <CategoryID.Value>5</CategoryID.Value>
                        <InstanceID.Name>EventId</InstanceID.Name>
                        <InstanceID.Value>3221356547</InstanceID.Value>
                        <Value0>System.ServiceModel.Activation.HostedHttpRequestAsyncResult/52951402</Value0>
                        <Value1>System.Web.HttpException (0x80004005): There was no channel actively listening at 'https://www.example.com/service.svc/newprofile/?id=8&amp;amp;ipaddress=25.62.133.109'. This is often caused by an incorrect address URI. Ensure that the address to which the message is sent matches an address on which a service is listening. ---&amp;gt; System.ServiceModel.EndpointNotFoundException: There w

我希望通过 http 和 https 都可以使用此服务。

我还检查了我的 web.config 和 global.asax.vb,但我没有专门针对 https 请求做任何事情,也没有在 IIS 中...这个问题是什么?

更新

好的,删除了一些端点,现在尝试找出在端点的地址值下提供什么。 使用下面的这个配置,我得到了错误: The endpoint at 'http://&lt;pcname&gt;/service.svc' does not have a Binding with the None MessageVersion. 'System.ServiceModel.Description.WebHttpBehavior' is only intended for use with WebHttpBinding or similar bindings.

  <system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
    <bindings>
      <webHttpBinding>
        <binding name="webHttpBindingWithJsonP" crossDomainScriptAccessEnabled="true">
          <security mode="Transport"></security>
        </binding>
      </webHttpBinding>
      <wsHttpBinding>
        <binding name="wsHttpBinding" >
          <security mode="Transport">          
          </security>
        </binding>
      </wsHttpBinding>
    </bindings>
    <client />
    <services>
      <service name="RestService.service">

        <endpoint behaviorConfiguration="webHttp" binding="webHttpBinding" contract="RestService.Iservice" />

        <endpoint behaviorConfiguration="webHttp" binding="wsHttpBinding" contract="RestService.Iservice" />

        <!--
        <endpoint address="/service.svc" binding="webHttpBinding" bindingConfiguration="webHttpBindingWithJsonP" contract="RestService.Iservice" />
-->

        <!--
        <endpoint behaviorConfiguration="webHttp" binding="webHttpBinding" contract="RestService.Iservice" />
        <endpoint address="/service.svc" binding="webHttpBinding" bindingConfiguration="webHttpBindingWithJsonP" contract="RestService.Iservice" />
-->
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="webHttp">
          <webHttp />
        </behavior>
        <behavior name="webHttpBehavior">
          <webHttp />
        </behavior>
      </endpointBehaviors>
    </behaviors>
  </system.serviceModel>

【问题讨论】:

您可能需要将绑定更改为 webHttpsBinding 以通过 https 通道进行通信。 但是我的所有服务都不能通过 http 获得,对吧?我希望这些服务在 http 和 https 上都可用。 这可能不会立即帮助你,但我保证你的答案在amazon.com/Programming-WCF-Services-Mastering-AppFabric/dp/… @Flo 如果需要,您可以始终保持旧端点不变,并使用 webHttpsBinding 为 https 添加新端点。看链接***.com/questions/26248759/… @JenishRabadiya 我尝试将&lt;webHttpsBinding&gt;&lt;/webHttpsBinding&gt; 添加到我的bindings 节点,但它在我的web.config 中无法识别。你能举例说明在哪里添加什么吗? 【参考方案1】:

这类网络服务对您是使用 HTTP 还是 HTTPS 访问它们非常敏感。

尝试使用它在 web.config 中为 WCF 服务启用 SSL:

<system.serviceModel>
    <bindings>
        <webHttpBinding>
            <binding>
                <security mode="Transport" />

我刚刚遇到了类似的问题,有关更多信息,请参阅我对此问题的回答:Moved my ASP.NET website to IIS 8 on windows server 2012... services missing: .svc files are viewable, but their methods give a 404

【讨论】:

对不起,我的帖子中还没有更新,但我已经启用了安全模式。查看更新后的帖子。 谢谢,看到这篇文章,它解决了我的问题:)。 @MGOwen OP 帮助我解决了 WSDL 未显示为 https 的问题,但您解决了我的方法抛出 404 的问题。谢谢!! 谢谢@MGOwen,与 OP 的问题不完全相同,但解决了我的问题:serverfault.com/questions/776390/…【参考方案2】:

@Flo,我终于可以让它工作了。我已经更新了&lt;endpoint behaviorConfiguration .. 并设置了&lt;service behaviorConfiguration=..,它起作用了,我的意思是当你同时点击httphttps 版本的www.example.com/service.svc/newprofile/?id=8&amp;ipaddress=124.162.13.109 时,它会为你提供正确的输出。以下是web.configsystem.serviceModel 部分。请试试这个,如果你仍然无法让它工作,请告诉我。

<system.serviceModel>
  <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  <bindings>
    <webHttpBinding>
      <binding name="Binding" crossDomainScriptAccessEnabled="true">
        <security mode="Transport">
          <transport clientCredentialType="None" />
        </security>
      </binding>
      <binding name="httpbind" crossDomainScriptAccessEnabled="true">
      </binding>
    </webHttpBinding>
  </bindings>
  <client />
  <services>
    <service name="RestService.service"  behaviorConfiguration="ServiceBehaviour">
      <endpoint address="" binding="webHttpBinding" bindingConfiguration="Binding" contract="RestService.Iservice" behaviorConfiguration="web">
      </endpoint>
      <endpoint address="" binding="webHttpBinding" bindingConfiguration="httpbind" contract="RestService.Iservice" behaviorConfiguration="web">
      </endpoint>
    </service>
  </services>
  <behaviors>
    <serviceBehaviors>
      <behavior name="ServiceBehaviour">
        <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
        <serviceDebug includeExceptionDetailInFaults="true" />
      </behavior>
      <behavior name="web">
        <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
        <serviceDebug includeExceptionDetailInFaults="true" />
      </behavior>
    </serviceBehaviors>
    <endpointBehaviors>
      <behavior name="web">
        <webHttp helpEnabled="true" />
      </behavior>
    </endpointBehaviors>
  </behaviors>
</system.serviceModel>

【讨论】:

完美!太感谢了! :-D 还有一个问题:现在看来,当我登录到我的站点时,我无法再调用这些服务了。我收到错误:Cross domain javascript callback is not supported in authenticated services.。知道那可能是什么吗?请注意:我必须让这些服务跨域可用。 @Flo,这类问题我已经回答过很多次了,请在这里找到一个***.com/questions/25889599/… 好的,再次感谢,我去看看。但在我开始实施一切之前,您能否判断您的解决方案是否支持以下内容: 1. 通过 http 和 https 访问我的网络服务? 2.调用我的webservices跨域? 3. 即使经过身份验证(通过表单身份验证),也可以跨域调用我的 web 服务?谢谢! @Flo, 1. 支持 http, https 只是基于配置,所以肯定会工作。 2. 要处理 POST 请求,您必须按照我给出的链接进行操作,不确定为什么 GET 请求在那里不起作用。 3. 我认为 CORS 不会有问题。

以上是关于通过 HTTPS 和 HTTP 访问时,WCF 服务不工作的主要内容,如果未能解决你的问题,请参考以下文章

带有消息的零星 WCF 服务激活错误 - 访问 IIS 元数据库时发生错误

如何通过WCF SOAP服务访问EF导航属性?

通过 HTTP 访问 WCF 服务

我们可以在跨域中使用 Jquery $.ajax() 调用通过 https 访问 WCF 服务吗?

使用 WCF 的 Http 请求中缺少授权标头

WCF发布到IIS 7.0,并以https访问