IDIspatchMessageInspector

tzl*_*lil 12 c# wcf idispatchmessageinspector

我实现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>
Run Code Online (Sandbox Code Playgroud)

但它不起作用.

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

我需要做什么?

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

Mon*_*nch 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() );
    }
}
Run Code Online (Sandbox Code Playgroud)

然后在执行服务合同时,您只需将该属性添加到该类中即可.

[ServiceBehavior( InstanceContextMode = InstanceContextMode.PerCall )]
[MyServiceBehavior]
public class ContractImplementation : IServiceContract
{
Run Code Online (Sandbox Code Playgroud)

  • 如果没有StackOverflow,+1 WCF将完全无法使用:) (26认同)