为什么我的WCF服务会出现“没有与None MessageVersion绑定的绑定”消息?

33

我创建了一个可用的 WCF 服务,现在想为它添加一些安全性以过滤 IP 地址。我已经按照 Microsoft 在示例中发布的示例尝试添加 IDispatchMessageInspector,并在其中引发调用 AfterReceiveRequest ,然后如果 IP 地址不在允许列表中就抛出错误。

在查看代码后,他们使用了 'wsHttpBinding' 进行配置,但我想使用 'webHttpBinding' 或 'basicHttpBinding'。但当我设置后,会出现以下错误:

'http://upload/api/Api.svc/soap' 上的终结点没有 None MessageVersion 的绑定。 'System.ServiceModel.Description.WebHttpBehavior' 仅适用于 WebHttpBinding 或类似的绑定。

我的配置如下:

<system.serviceModel>


    <serviceHostingEnvironment multipleSiteBindingsEnabled="true">
    </serviceHostingEnvironment>
    <!--Set up the service-->
    <services>
      <service behaviorConfiguration="SOAPRESTDemoBehavior" name="HmlApi">
        <endpoint address="rest" binding="webHttpBinding" contract="VLSCore2.Interfaces.IHmlApi" behaviorConfiguration="SOAPRESTDemoEndpointBehavior" />
        <endpoint address="soap" binding="basicHttpBinding" contract="VLSCore2.Interfaces.IHmlApi" behaviorConfiguration="SOAPRESTDemoEndpointBehavior" />
      </service>
    </services>

    <!--Define the behaviours-->
    <behaviors>
      <serviceBehaviors>
        <behavior name="SOAPRESTDemoBehavior">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>

      <!---Endpoint -->
      <endpointBehaviors>
        <behavior name="SOAPRESTDemoEndpointBehavior">
          <ipFilter/>
          <webHttp />
        </behavior>
      </endpointBehaviors>
    </behaviors>

    <extensions>
      <behaviorExtensions>
        <add name="ipFilter" type="VLSCore2.Api.IpFilterBehaviourExtensionElement, VLSCore2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
      </behaviorExtensions>
    </extensions>

  </system.serviceModel>

我想知道如何在不使用WebHttpBinding的情况下设置我的消息检查器。这是否可能?

我想使用SOAP的'basicHttpBinding'而不是wsHttpBinding(以及所有与WS *相关的开销....)

3个回答

49

这是因为您为SOAP和REST端点配置了单个endpointBehavior,但是SOAP端点不能具有webHttp行为。 您需要将其拆分为:

  <endpointBehaviors>
    <behavior name="SOAPDemoEndpointBehavior">
      <ipFilter/>
    </behavior>
    <behavior name="RESTDemoEndpointBehavior">
      <ipFilter/>
      <webHttp />
    </behavior>
  </endpointBehaviors>

然后你的端点应该是:

    <endpoint address="rest" binding="webHttpBinding" contract="VLSCore2.Interfaces.IHmlApi" behaviorConfiguration="RESTDemoEndpointBehavior" />
    <endpoint address="soap" binding="basicHttpBinding" contract="VLSCore2.Interfaces.IHmlApi" behaviorConfiguration="SOAPDemoEndpointBehavior" />

2
非常感谢,这非常准确。可以说,我认为现在为一个服务设置所有正确的端点非常重要! - Exitos
1
我已经苦苦挣扎了几个小时,到这里才发现我还没有改变绑定:))) 我正在使用移动应用程序的基本http请求... 点赞+ - dpaul1994

2
对我来说,这是因为我在SOAP配置中将“webHttp”定义为一种行为。只有删除它才解决了问题。

0

我已经花了4天的时间在MS文档、Stack Overflow和其他所有东西中来回查找,试图在我开始使用HTTPS/SSL请求我的服务后正确地使配置文件工作。这个配置文件的设置非常关键。

我不需要修改任何代码,只需在web.config文件system.serviceModel部分中切换从HTTP到HTTPS即可。

这个配置对于使用JavaScript访问ASP.NET C# WCF中的JSON数据非常有效。我很高兴!

<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="false"> 
</serviceHostingEnvironment>

<behaviors>
  <endpointBehaviors> 
    <behavior name="webBehavior"> 
        <webHttp /> 
    </behavior> 
  </endpointBehaviors> 
  <serviceBehaviors>
    <behavior>
      <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
      <serviceMetadata httpsGetEnabled="false"/>
      <!-- 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>
</behaviors>

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

<services>
  <service name="buildingsWebService.Service1">
    <endpoint address="https://www.azsfb.gov/Service/Service1.svc" 
        binding="webHttpBinding" 
        bindingConfiguration="sfbSecureBinding" 
        contract="buildingsWebService.IService1"
        behaviorConfiguration="webBehavior"> 
    </endpoint>
    <host>
      <baseAddresses>
        <add baseAddress="https://www.azsfb.gov/"/>
      </baseAddresses>
    </host>
  </service>
</services>


<protocolMapping>
  <add binding="basicHttpBinding" scheme="https"/>
</protocolMapping>

</system.serviceModel>


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接