简体   繁体   English

使用新的WCF Web API设置Ninject

[英]Setting up Ninject with the new WCF Web API

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. 所以我一直在玩最新版本的WCF Web API,并决定用它来实现Ninject。

Based off what I've read I need to implement the interface IResourceFactory which demands the following methods: 基于我读过的内容,我需要实现IResourceFactory接口,该接口需要以下方法:

    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));
    //}
}

and wired it up with 用它连接起来

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

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. 我创建的第一个用于提供状态/省份列表的资源方法使用HTTP 200 OK生成输出。

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. 我在新的Resolve方法中做的黑客感觉特别脏,所以我想我会利用那些经验丰富的人来收紧它。 Has anyone else implemented Ninject with the WCF Web API and implemented a cleaner solution? 有没有其他人使用WCF Web API实现Ninject并实现了更清洁的解决方案?

Thanks for any input! 感谢您的任何意见!

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
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM