问题描述
有人可以指出正确的方向来使Ninject与WCF Web API Preview 5一起工作吗?我已使用Ninject.Extensions.Wcf库成功在我的ASP.NET MVC 3项目以及另一个内部WCF服务中启动并运行它.但是,当创建一个新的MVC 3项目并从NuGet获取WebApi.All库时,我无法使其正常工作.
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.
我看过这篇stackoverflow帖子设置Ninject与新的WCF Web API ,但无法正常运行,我认为这可能与最新版本中的某些更改有关.
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.
我也不确定哪个Ninject库可以引用主要库之外的库.我是否使用Ninject.MVC3,Ninject.Extensions.Wcf.
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.
****更新**
我正在使用的代码来自上面提到的问题的答案.我在自己的类文件中有此文件.
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
}
}
我的global.asax中有此内容:
This I have in my global.asax:
var configuration = HttpConfiguration.Create().SetResourceFactory(new NinjectResourceFactory());
RouteTable.Routes.MapServiceRoute<myResource>("resource", configuration);
我遇到的问题是无法识别IResourceFactory接口,并且HttpConfiguration.Create()不再存在,因此我需要以其他方式设置SetResourceFactory,这是我尝试使用HttpConfiguration().CreateInstance进行的其他方式方法,但没有喜悦.
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.
推荐答案
以下是我使用Ninject和WebApi编写的代码,它可以正常工作.创建一个从WebApiConfiguration继承的类
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>();
}
}
并在RegisterRoutes中使用NinjectWebApiConfiguration
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);
}
这篇关于Ninject使用WCF Web API预览5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!