【问题标题】:WCF & Ninject - Getting Ninject Kernel Into An IDispatchMessageInspector InstanceWCF & Ninject - 将 Ninject 内核放入 IDispatchMessageInspector 实例
【发布时间】:2015-05-20 18:41:11
【问题描述】:

我目前已将 Ninject 安装到 WCF 应用程序中(使用 ninject.extensions.wcf),并且基本工作正常。但是,我添加了自定义 IDispatchMessageInspectorIServiceBehavior 以验证每个服务调用(通过根据某些凭据检查数据库),但我不完全确定如何让 Ninject 在 IDispatchMessageInspector 中工作。

我目前的代码是:

public class MyServiceInspector: IDispatchMessageInspector
{
    #region Methods

    public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
    {
        // TODO: Get credentials from request here, and hit database.

        return null;
    }

    public void BeforeSendReply(ref Message reply, object correlationState)
    {
        // Do nothing
    } 

   #endregion
}

发起此操作的IServiceBehavior如下:

public class MyServiceBehaviour : Attribute, IServiceBehavior
{
    #region Methods

    public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters)
    {
        // Do nothing
    }

    public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
    {
        // Loop through channels and endpoints
        foreach (ChannelDispatcher cDispatcher in serviceHostBase.ChannelDispatchers)
        {
            foreach (EndpointDispatcher eDispatcher in cDispatcher.Endpoints)
            {
                // Add credentials inspector
                eDispatcher.DispatchRuntime.MessageInspectors.Add(new MyServiceInspector());
            }
        }
    }

    public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
    {
        // Do nothing
    }

    #endregion
}

我在网上搜索了一些例子,但我似乎找不到任何东西。这甚至可能吗?

【问题讨论】:

    标签: wcf ninject


    【解决方案1】:

    我知道这个答案很晚,但希望它可以帮助其他遇到这个问题的人。

    我今天遇到了同样的问题。我需要一个带有注入依赖项的消息检查器。他们从 3.2 版向 Ninjext.Extensions.WCF 添加了一个 NinjectBehaviorExtensionElement 类。所以这就是我在消息检查器中注入依赖项的方式:

    消息检查器:

     public class MyMessageInspector : IDispatchMessageInspector
     {
        public MyMessageInspector(IInjectedDependency injectedDependency)
        {
    
        }
    
        public object AfterReceiveRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel, System.ServiceModel.InstanceContext instanceContext)
        {
        }
    
        public void BeforeSendReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
        {
        }
     }
    

    行为:

    public class MyMessageInspectionBehavior : IEndpointBehavior
    {
        private readonly IDispatchMessageInspector _messageInspector;
    
        public MyMessageInspectionBehavior(IDispatchMessageInspector messageInspector)
        {
            _messageInspector = messageInspector;
        }
    
        public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
        {
        }
    
        public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
        {
        }
    
        public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
        {
            endpointDispatcher.DispatchRuntime.MessageInspectors.Add(_messageInspector);
        }
    
        public void Validate(ServiceEndpoint endpoint)
        {
        }
    

    }

    忍者绑定:

    this.Bind<IDispatchMessageInspector>()
        .To<MyMessageInspector>()
        .WhenInjectedInto<IEndpointBehavior>();
    

    最后一步是配置:

    <system.serviceModel>
    <extensions>
      <behaviorExtensions>
        <add name="MyCustomMessageInspector" 
             type="Ninject.Extensions.Wcf.BaseNinjectBehaviorExtensionElement+NinjectBehaviorExtensionElement`1[[MyNamespace.MyMessageInspectionBehavior, MyAssemblyName]], Ninject.Extensions.Wcf" />
      </behaviorExtensions>
    </extensions>
    <behaviors>
      <endpointBehaviors>
        <behavior>
          <MyCustomMessageInspector />
        </behavior>
      </endpointBehaviors>
    

    【讨论】:

    • 嗨,我知道这是几年后的事了,但我想知道 MyMessageInspector 是单例还是每个请求的新实例?
    【解决方案2】:

    我暂时把它停了下来,因为似乎没有任何解决办法。我只是直接连接到我的上下文,以期在未来改进它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-15
      • 1970-01-01
      • 1970-01-01
      • 2015-11-23
      • 1970-01-01
      • 2012-02-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多