Ninject 使用 WCF Web API Preview 5

发布于 2024-12-07 03:59:25 字数 1485 浏览 0 评论 0原文

有人能为我指出正确的方向,让 Ninject 与 WCF Web API Preview 5 一起使用吗?我已在我的 ASP.NET MVC 3 项目以及使用 Ninject.Extensions.Wcf 库的另一个内部 WCF 服务中成功启动并运行它。但是,在创建新的 MVC 3 项目并从 NuGet 获取 WebApi.All 库时,我无法让它工作。

我看过这篇 stackoverflow 帖子 使用新的 WCF 设置 Ninject Web API 但无法使其正常工作,我认为这可能与最新版本中的一些更改有关。

我也不确定除了主要库之外还应该参考哪些 Ninject 库。我使用 Ninject.MVC3 、 Ninject.Extensions.Wcf 吗?

对此的任何帮助将不胜感激。

****更新**

我正在使用的代码来自上述问题的答案。我把这个放在它自己的类文件中。

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

这是我在 global.asax 中遇到的问题:

var configuration = HttpConfiguration.Create().SetResourceFactory(new NinjectResourceFactory());
RouteTable.Routes.MapServiceRoute<myResource>("resource", configuration);

我遇到的问题是 IResourceFactory 接口无法识别,并且 HttpConfiguration.Create() 不再存在,因此我需要使用我尝试使用的其他方式设置 SetResourceFactory HttpConfiguration().CreateInstance 方法但没有乐趣。

Can anybody point me in the right direction to get Ninject working with WCF Web API Preview 5? I have it successfully up and running in my ASP.NET MVC 3 project and also in another internal WCF Service using the Ninject.Extensions.Wcf library. However I cannot get it to work when creating a new MVC 3 project and getting the WebApi.All library from NuGet.

I have looked at this stackoverflow post Setting up Ninject with the new WCF Web API but cannot get it working which I believe could be to do with some of the changes in the latest release.

I am also unsure which Ninject Libraries to reference beyond the main one. Do I use the Ninject.MVC3 , Ninject.Extensions.Wcf.

Any help on this would be much appreciated.

****UPDATE**

Code I am using which is from the answer in the question mentioned above. I have this in its own class file.

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

This I have in my global.asax:

var configuration = HttpConfiguration.Create().SetResourceFactory(new NinjectResourceFactory());
RouteTable.Routes.MapServiceRoute<myResource>("resource", configuration);

The issue I am having is that the IResourceFactory interface is not recognised and that the HttpConfiguration.Create() no longer exists so I need to set the SetResourceFactory some other way which I have tried to do using the HttpConfiguration().CreateInstance method but no joy.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我偏爱纯白色 2024-12-14 03:59:25

以下是我使用 Ninject 和 WebApi 的代码,它可以工作。
创建一个继承自WebApiConfiguration的类

public class NinjectWebApiConfiguration : WebApiConfiguration {
    private IKernel kernel = new StandardKernel();

    public NinjectWebApiConfiguration() {
        AddBindings();
        CreateInstance = (serviceType, context, request) => kernel.Get(serviceType);
    }

    private void AddBindings() {
        kernel.Bind<IProductRepository>().To<MockProductRepository>();
    }

}

,并在RegisterRoutes中使用NinjectWebApiConfiguration

public static void RegisterRoutes(RouteCollection routes) {

    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    var config = new NinjectWebApiConfiguration() { 
        EnableTestClient = true
    };

    routes.MapServiceRoute<ContactsApi>("api/contacts", config);
}

Following is my code with Ninject and WebApi,it works.
Create a class inherites from WebApiConfiguration

public class NinjectWebApiConfiguration : WebApiConfiguration {
    private IKernel kernel = new StandardKernel();

    public NinjectWebApiConfiguration() {
        AddBindings();
        CreateInstance = (serviceType, context, request) => kernel.Get(serviceType);
    }

    private void AddBindings() {
        kernel.Bind<IProductRepository>().To<MockProductRepository>();
    }

}

and use the NinjectWebApiConfiguration in RegisterRoutes

public static void RegisterRoutes(RouteCollection routes) {

    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    var config = new NinjectWebApiConfiguration() { 
        EnableTestClient = true
    };

    routes.MapServiceRoute<ContactsApi>("api/contacts", config);
}
本宫微胖 2024-12-14 03:59:25

在 P5 中,您必须从 WebApiConfiguration 派生并使用派生的配置:

public class NinjectConfiguration : WebApiConfiguration
    {
        public NinjectConfiguration(IKernel kernel)
        {
            CreateInstance((t, i, m) =>
            {
                return kernel.Get(t);
            }); 
        }
    }

In P5 you have to derive from WebApiConfiguration and use your derived configuration:

public class NinjectConfiguration : WebApiConfiguration
    {
        public NinjectConfiguration(IKernel kernel)
        {
            CreateInstance((t, i, m) =>
            {
                return kernel.Get(t);
            }); 
        }
    }
淡淡離愁欲言轉身 2024-12-14 03:59:25

这里有关于这个问题的很好的答案,但我想向您展示默认 WebApi 配置的方法:

    protected void Application_Start(object sender, EventArgs e) {

        RouteTable.Routes.SetDefaultHttpConfiguration(new Microsoft.ApplicationServer.Http.WebApiConfiguration() { 
            CreateInstance = (serviceType, context, request) => GetKernel().Get(serviceType)
        });

        RouteTable.Routes.MapServiceRoute<People.PeopleApi>("Api/People");
    }

    private IKernel GetKernel() { 

        IKernel kernel = new StandardKernel();

        kernel.Bind<People.Infrastructure.IPeopleRepository>().
            To<People.Models.PeopleRepository>();

        return kernel;
    }

下面的博客文章讨论了一些关于 WCF Web API 上的 Ninject 集成:

http://www.tugberkugurlu.com/archive/introduction-to-wcf-web-api-new-rest-face-ofnet

There are great answers to the question here but I would like to show you the way with default WebApi configuration:

    protected void Application_Start(object sender, EventArgs e) {

        RouteTable.Routes.SetDefaultHttpConfiguration(new Microsoft.ApplicationServer.Http.WebApiConfiguration() { 
            CreateInstance = (serviceType, context, request) => GetKernel().Get(serviceType)
        });

        RouteTable.Routes.MapServiceRoute<People.PeopleApi>("Api/People");
    }

    private IKernel GetKernel() { 

        IKernel kernel = new StandardKernel();

        kernel.Bind<People.Infrastructure.IPeopleRepository>().
            To<People.Models.PeopleRepository>();

        return kernel;
    }

The below blog post talks a little bit about Ninject integration on WCF Web API:

http://www.tugberkugurlu.com/archive/introduction-to-wcf-web-api-new-rest-face-ofnet

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文