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

问题描述

所以我一直在摆弄 WCF网站API 并决定我想潜入实施Ninject吧。

So I've been playing around with the latest release of the WCF Web API and decided I wanted to dive into implementing Ninject with it.

根据关我读过,我需要实现该接口IResourceFactory这就要求下面的方法:

Based off what I've read I need to implement the interface IResourceFactory which demands the following methods:

    public object GetInstance(System.Type serviceType,
                              System.ServiceModel.InstanceContext instanceContext,
                              System.Net.Http.HttpRequestMessage request);

    public void ReleaseInstance(System.ServiceModel.InstanceContext instanceContext,
                                object service);

所以,我的鸡抓伤以下的:

So I chicken scratched the following out:

public class NinjectResourceFactory : IResourceFactory
{
    private readonly IKernel _kernel;

    public NinjectResourceFactory()
    {
        var modules = new INinjectModule[]
                          {
                              new ServiceDIModule(),    //Service Layer Module
                              new RepositoryDIModule(), //Repo Layer Module
                              new DataServiceDIModule()
                          };

        _kernel = new StandardKernel(modules);
    }

    #region IResourceFactory Members

    public object GetInstance(Type serviceType,
                              InstanceContext instanceContext,
                              HttpRequestMessage request)
    {
        return Resolve(serviceType);
    }

    public void ReleaseInstance(InstanceContext instanceContext, object service)
    {
        throw new NotImplementedException();
    }

    #endregion

    private object Resolve(Type type)
    {
        return _kernel.Get(type);
    }

    //private T Resolve<T>()
    //{
    //    return _kernel.Get<T>();
    //}

    //private T Resolve<T>(string name)
    //{
    //    return _kernel.Get<T>(metaData => metaData.Has(name));
    //    return _kernel.Get<T>().Equals(With.Parameters.
    //                                   ContextVariable("name", name));
    //}
}

和有线它与

var configuration = HttpHostConfiguration.Create().SetResourceFactory(new NinjectResourceFactory());
     RouteTable.Routes.MapServiceRoute<StateProvinceResource>("States", configuration);

神奇的是,这似乎工作。第一个资源的方法,我创建以服务出状态的列表/省份产生输出HTTP 200 OK。

Amazingly, this seems to work. The first resource method I created to serve out a list of states/provinces generates output with HTTP 200 OK.

所以,这个问题。是否有一个的清洁的写作这家工厂的方式吗?我真的通过它烂醉,它只是感觉不对。我觉得我失去了一些东西明显的盯着我的脸。我在新的解决方法制造的黑客感觉特别脏的,所以我想我会挖掘到那些更有经验的收紧这件事。有其他人实现Ninject与WCF的Web API,并实施了清洁的解决方案?

So, to the question. Is there a cleaner way of writing this factory? I really fuddled through it and it just doesn't feel right. I feel like I'm missing something obvious staring me in the face. The hack I made in the new Resolve method feels especially dirty so I figured I'd tap into those more experienced to tighten this up. Has anyone else implemented Ninject with the WCF Web API and implemented a cleaner solution?

感谢任何投入!

推荐答案

您可以通过从应用范围传递的内核实现它。

You could implement it by passing in the Kernel from the application scope.

public class NinjectResourceFactory : IResourceFactory
{
    private readonly IKernel _kernel;

    public NinjectResourceFactory(IKernel kernel)
    {
        _kernel = kernel;
    }

    public object GetInstance(Type serviceType, InstanceContext instanceContext, HttpRequestMessage request)
    {
        return _kernel.Get(serviceType);
    }

    public void ReleaseInstance(InstanceContext instanceContext, object service)
    {
        // no op
    }
}

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

09-05 21:12