Ninject 使用 WCF Web API Preview 5
有人能为我指出正确的方向,让 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
以下是我使用 Ninject 和 WebApi 的代码,它可以工作。
创建一个继承自WebApiConfiguration的类
,并在RegisterRoutes中使用NinjectWebApiConfiguration
Following is my code with Ninject and WebApi,it works.
Create a class inherites from WebApiConfiguration
and use the NinjectWebApiConfiguration in RegisterRoutes
在 P5 中,您必须从 WebApiConfiguration 派生并使用派生的配置:
In P5 you have to derive from WebApiConfiguration and use your derived configuration:
这里有关于这个问题的很好的答案,但我想向您展示默认 WebApi 配置的方法:
下面的博客文章讨论了一些关于 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:
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