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

Exi*_*tos 29 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>
Run Code Online (Sandbox Code Playgroud)

所以我想知道如何在不使用WebHttpBinding的情况下设置我的消息检查器.这甚至可能吗?

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

Phi*_*rdt 43

这很简单,因为您已为SOAP和REST端点配置了单个endpointBehavior,但SOAP端点不能具有webHttp行为.你需要将它们分开,以便它们是:

  <endpointBehaviors>
    <behavior name="SOAPDemoEndpointBehavior">
      <ipFilter/>
    </behavior>
    <behavior name="RESTDemoEndpointBehavior">
      <ipFilter/>
      <webHttp />
    </behavior>
  </endpointBehaviors>
Run Code Online (Sandbox Code Playgroud)

然后你的终点应该是:

    <endpoint address="rest" binding="webHttpBinding" contract="VLSCore2.Interfaces.IHmlApi" behaviorConfiguration="RESTDemoEndpointBehavior" />
    <endpoint address="soap" binding="basicHttpBinding" contract="VLSCore2.Interfaces.IHmlApi" behaviorConfiguration="SOAPDemoEndpointBehavior" />
Run Code Online (Sandbox Code Playgroud)

  • 非常感谢这是现场。公平地说,我认为现在为服务设置所有正确的端点很重要! (2认同)