HttpConfiguration.get_ServiceResolver()失踪 [英] HttpConfiguration.get_ServiceResolver() Missing
问题描述
我连接最多Ninject与MVC4并已将其致力于它试图真正解决依赖的地步。不过,我得到了以下异常:
I am wiring up Ninject with MVC4 and have it working to the point it's trying to actually resolve dependencies. However, I am getting the following exception:
找不到方法:System.Web.Http.Services.DependencyResolver System.Web.Http.HttpConfiguration.get_ServiceResolver()
Method not found: 'System.Web.Http.Services.DependencyResolver System.Web.Http.HttpConfiguration.get_ServiceResolver()'.
任何人都遇到了这一点,并有周围的工作?
Anyone ran into this and have a work around?
推荐答案
GlobalConfiguration.Configuration.ServiceResolver
与 GlobalConfiguration.Configuration.DependencyResolver <被替换/ code>在RC。所以我猜你正在使用的Ninject包根本就不是专为RC。这是断裂的变化之一。
GlobalConfiguration.Configuration.ServiceResolver
was replaced with GlobalConfiguration.Configuration.DependencyResolver
in the RC. So I guess the Ninject package you are using is simply not designed for the RC. It was one of the breaking changes.
因此,这里有使Ninject与ASP.NET MVC 4的Web API RC的工作步骤:
So here are the steps to make Ninject work with ASP.NET MVC 4 Web API RC:
- 创建使用空模板创建新的ASP.NET MVC应用程序4
-
声明一个接口:
- Create a new ASP.NET MVC 4 application using the Empty template
Declare an interface:
public interface IFoo
{
string GetBar();
}
然后实现:
Then an implementation:
public class Foo : IFoo
{
public string GetBar()
{
return "the bar";
}
}
然后添加一个API控制器:
Then add an API controller:
public class ValuesController : ApiController
{
private readonly IFoo _foo;
public ValuesController(IFoo foo)
{
_foo = foo;
}
public string Get()
{
return _foo.GetBar();
}
}
安装 Ninject.Mvc3
的NuGet包(安装封装Ninject.Mvc3
)
定义一个定制的API依赖解析器如显示此要旨:
Define a custom API dependency resolver as shown in this gist:
public class NinjectDependencyScope : IDependencyScope
{
private IResolutionRoot resolver;
internal NinjectDependencyScope(IResolutionRoot resolver)
{
Contract.Assert(resolver != null);
this.resolver = resolver;
}
public void Dispose()
{
IDisposable disposable = resolver as IDisposable;
if (disposable != null)
disposable.Dispose();
resolver = null;
}
public object GetService(Type serviceType)
{
if (resolver == null)
throw new ObjectDisposedException("this", "This scope has already been disposed");
return resolver.TryGet(serviceType);
}
public IEnumerable<object> GetServices(Type serviceType)
{
if (resolver == null)
throw new ObjectDisposedException("this", "This scope has already been disposed");
return resolver.GetAll(serviceType);
}
}
public class NinjectDependencyResolver : NinjectDependencyScope, IDependencyResolver
{
private IKernel kernel;
public NinjectDependencyResolver(IKernel kernel)
: base(kernel)
{
this.kernel = kernel;
}
public IDependencyScope BeginScope()
{
return new NinjectDependencyScope(kernel.BeginBlock());
}
}
在你的〜/ App_Start / NinjectWebCommon.cs / CreateKernel
方法,当您安装的NuGet的 RegisterServices(内核);
行:
In your ~/App_Start/NinjectWebCommon.cs/CreateKernel
method that was created when you installed the NuGet add the following line after the RegisterServices(kernel);
line:
GlobalConfiguration.Configuration.DependencyResolver = new NinjectDependencyResolver(kernel);
配置内核:
Configure your kernel:
private static void RegisterServices(IKernel kernel)
{
kernel.Bind<IFoo>().To<Foo>();
}
按<大骨节病> F5 骨节病>并导航到 / API /值
显然,当RC打RTM我希望会有一个 Ninject.Mvc4
的NuGet会缩短这些10个步骤,最多5个。
Obviously when the RC hits RTM I hope there will be a Ninject.Mvc4
NuGet that will shorten those 10 steps to maximum 5.
这篇关于HttpConfiguration.get_ServiceResolver()失踪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!