12

我实现 IDispatchMessageInspector.AfterReciveRequest 然后我像这样配置:

<configuration>
  <system.serviceModel>
    <services>
      <service 
        name="Microsoft.WCF.Documentation.SampleService"
        behaviorConfiguration="inspectorBehavior">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8080/SampleService" />
          </baseAddresses>
        </host>
        <endpoint
          address=""
          binding="wsHttpBinding"
          contract="Microsoft.WCF.Documentation.ISampleService"
        />

      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="inspectorBehavior">
          <serviceInspectors />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <extensions>
      <behaviorExtensions>
        <add 
          name="serviceInspectors" 
          type="Microsoft.WCF.Documentation.InspectorInserter, HostApplication, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"
        />
      </behaviorExtensions>
    </extensions>
  </system.serviceModel>
</configuration>

但它不起作用。

我检查了我的程序集和我的本地参考,我没有找到Microsoft.WCF.Documentation.InspectorInserterHostApplicationdll 我在网上搜索以下载HostApplicationdll 但我什么也没找到。

我需要做什么?

我需要实现更多的东西,或者我只需要这个配置。

4

1 回答 1

22

我发现使用还扩展 Attribute 的 IServiceBehavior 实现来附加 IDispatchMessageInspector 实现要容易得多。然后在 ApplyDispatchBehavior 方法中,将消息检查器附加到所有通道中的所有端点。

这篇文章对我帮助很大。

示例代码:

public class MyServiceBehavior : Attribute, IServiceBehavior
{
    public void ApplyDispatchBehavior( ServiceDescription serviceDescription,
        ServiceHostBase serviceHostBase )
    {
        foreach( ChannelDispatcher cDispatcher in serviceHostBase.ChannelDispatchers )
            foreach( EndpointDispatcher eDispatcher in cDispatcher.Endpoints )
                eDispatcher.DispatchRuntime.MessageInspectors.Add( new RequestAuthChecker() );
    }
}

然后在你的服务契约的实现中,你可以将属性添加到类中。

[ServiceBehavior( InstanceContextMode = InstanceContextMode.PerCall )]
[MyServiceBehavior]
public class ContractImplementation : IServiceContract
{
于 2011-03-10T21:32:55.777 回答