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

时间:2011-09-28 15:09:16

标签: c# wcf

我创建了一个有效的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 个答案:

答案 0 :(得分:41)

这很简单,因为您已为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" />

答案 1 :(得分:1)

对我来说,这是因为我将'webHttp'定义为SOAP配置的行为。只有它的缺席解决了它..

答案 2 :(得分:0)

4 天以来,我一直在浏览 MS 文档、堆栈溢出和其他所有内容。在我开始对来自我的服务的请求使用 HTTPS/SSL 后,试图让配置文件正常工作。这个配置文件的设置非常关键。

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

此配置适用于使用 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>

相关问题