设置为Ninject WCF [英] Setup Ninject for WCF

查看:193
本文介绍了设置为Ninject WCF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁在WCF中如何设置Ninject一个明确的指示?被周围的Googling,但我不能看到如何在WCF使用Ninject任何更新的指引。

Does anyone have a clear instruction on how to setup Ninject in WCF? been googling around but I cant see any updated guidelines on how to use Ninject in WCF.

推荐答案

使用NInject与WCF是一样的如使用其他的DI容器。要做到这一点,你需要使用3 WCF扩展点: InstanceProvider 的ServiceHost ServiceHostFactory

Using NInject with WCF is the same as using any other DI container. To do this you need to use 3 WCF extensibility points: InstanceProvider, ServiceHost and ServiceHostFactory.

自定义 InstanceProvider 将被用于通过使用一个构造函数来创建服务实例参数。该代码可以看到下面

The custom InstanceProvider will be used to create service instances by using a constructor with parameters. The code can be seen below.

public class NInjectInstanceProvider : IInstanceProvider, IContractBehavior
{
    private readonly IKernel kernel;

    public NInjectInstanceProvider(IKernel kernel)
    {
        if (kernel == null) throw new ArgumentNullException("kernel");
        this.kernel = kernel;
    }

    public object GetInstance(InstanceContext instanceContext, Message message)
    {
        //delegate to GetInstance(InstanceContext)
        return GetInstance(instanceContext);
    }

    public object GetInstance(InstanceContext instanceContext)
    {
        //resolve the service instance
        return kernel.Get(instanceContext.Host.Description.ServiceType);
    }

    public void ReleaseInstance(InstanceContext instanceContext, object instance)
    {
        kernel.Release(instance);
    }

    public void AddBindingParameters(ContractDescription contractDescription, 
        ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
    {
    }

    public void ApplyClientBehavior(ContractDescription contractDescription,
        ServiceEndpoint endpoint, ClientRuntime clientRuntime)
    {
    }

    public void ApplyDispatchBehavior(ContractDescription contractDescription,
        ServiceEndpoint endpoint, DispatchRuntime dispatchRuntime)
    {
        dispatchRuntime.InstanceProvider = this;
    }

    public void Validate(ContractDescription contractDescription, ServiceEndpoint endpoint)
    {
    }
}

这自定义实例提供程序,然后应用到的ServiceHost 类每一个合同。这是通过使用一个合同行为完成。这就是为什么实例供应商也实现了 IContractBehavior 。你可以看到,我们应用了 ApplyDispatchBehavior 方法的实例提供。下面的代码展示了的ServiceHost ServiceHostFactory 的实现。

This custom instance provider is then applied to every contract in the ServiceHost class. This is done by using a contract behavior. This is why the instance provider also implements IContractBehavior. You can see that we apply the instance provider in the ApplyDispatchBehavior method. The code below presents the ServiceHost and ServiceHostFactory implementations.

public class NInjectServiceHostFactory : ServiceHostFactory
{
    private readonly IKernel kernel;

    public NInjectServiceHostFactory()
    {
        kernel = new StandardKernel();
        kernel.Bind<IDummyDependency>().To<DummyDepencency>();
        //add the rest of the mappings here
    }

    protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
    {
        return new NInjectServiceHost(kernel, serviceType, baseAddresses);
    }
}

public class NInjectServiceHost : ServiceHost
{
    public NInjectServiceHost(IKernel kernel, Type serviceType, params Uri[] baseAddresses)
        : base(serviceType, baseAddresses)
    {
        if (kernel == null) throw new ArgumentNullException("kernel");
        foreach (var cd in ImplementedContracts.Values)
        {
            cd.Behaviors.Add(new NInjectInstanceProvider(kernel));
        }
    }
}

您可以看到里面的的ServiceHost 的构造函数,我们遍历所有合同实施和应用,我们需要的行为。在我们的例子,这是 NInjectInstanceProvider

You can see that inside the ServiceHost constructor we iterate over all implemented contracts and apply the behavior we need. In our case this is NInjectInstanceProvider.

自定义 ServiceHostFactory 需要以创建DI容器并用映射填充它。然后,我们覆盖 CreateServiceHost 方法,以提供我们的定制的ServiceHost 实施

The custom ServiceHostFactory is required in order to create the DI container and populate it with mappings. We then override the CreateServiceHost method in order to provide our custom ServiceHost implementation.

的设置是在这一点完成。所有你需要做的就是创建一个对 IDummyDependency 依赖WCF服务。另外,不要忘记设置工厂属性,如下面的SVC文件(右键点击SVC文件,然后选择查看标记):

The setup is complete at this point. All you need to do is create a WCF service that has a dependency on IDummyDependency. Also, don't forget to set the Factory attribute in the svc file like below (right click on svc file, then "View Markup"):

<%@ ServiceHost Language="C#" Debug="true" Service="Service.DummyService" Factory="Service.NInjectServiceHostFactory" %>



希望这有助于。另外,我觉得NInject为此提供一些实现了在NInject.Extensions.Wcf.dll箱子。

Hope this helps. Also, I think NInject offers some implementations for this out of the box in NInject.Extensions.Wcf.dll.

这篇关于设置为Ninject WCF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆