尝试在 WebApi(Net Framework)中使用 IoC 容器时出错
Error while trying to use an IoC container inside WebApi (Net Framework)
我正在尝试在 WebApi 模板中使用 IoC 容器。我创建了一个空的 Web App Framework 项目,并选择使用 WebApi。然后我添加了 Unity NuGet 包并将以下内容添加到 Application_Start:
protected void Application_Start()
{
GlobalConfiguration.Configure(WebApiConfig.Register);
UnityConfig.Register();
}
UnityConfig:
public class UnityConfig
{
public static UnityContainer Register()
{
var container = new UnityContainer();
container.RegisterType<IMyClass, MyClass>();
GlobalConfiguration.Configuration.DependencyResolver = new UnityResolver(container);
return container;
}
}
我的 UnityResolver 在这里(代码大部分是抄袭的,但看起来很标准):
public class UnityResolver : IDependencyResolver
{
protected IUnityContainer container;
public UnityResolver(IUnityContainer container)
{
if (container == null)
{
throw new ArgumentNullException("container");
}
this.container = container;
}
public object GetService(Type serviceType)
{
try
{
return container.Resolve(serviceType);
}
catch (ResolutionFailedException)
{
return null;
}
}
public IEnumerable<object> GetServices(Type serviceType)
{
try
{
return container.ResolveAll(serviceType);
}
catch (ResolutionFailedException)
{
return new List<object>();
}
}
public IDependencyScope BeginScope()
{
var child = container.CreateChildContainer();
return new UnityResolver(child);
}
public void Dispose()
{
Dispose(true);
}
protected virtual void Dispose(bool disposing)
{
container.Dispose();
}
}
当我现在运行这个时,我得到以下错误:
Unity.Exceptions.ResolutionFailedException: 'Resolution of the
dependency failed, type =
'System.Web.Http.Hosting.IHostBufferPolicySelector', name = '(none)'.
Exception occurred while: while resolving. Exception is:
InvalidOperationException - The current type,
System.Web.Http.Hosting.IHostBufferPolicySelector, is an interface and
cannot be constructed. Are you missing a type mapping?
----------------------------------------------- At the time of the exception, the container was: Resolving
System.Web.Http.Hosting.IHostBufferPolicySelector,(none) '
很明显,在告诉它我想使用哪个 IoC 库之后,我需要注入一些内部依赖项,或者告诉它,或者更早(或更晚)注册它。
Clearly, after telling it which IoC library I want to use, I need to inject some internal dependencies, or tell it to, or perhaps, register this earlier (or later).
没错。您需要注册所有将通过 Unity 解析的类型。报错提示你缺少IHostBufferPolicySelector
,所以你需要注册
public static UnityContainer Register()
{
var container = new UnityContainer();
// Register all types with Unity here
container.RegisterType<IHostBufferPolicySelector, OwinBufferPolicySelector>();
GlobalConfiguration.Configuration.DependencyResolver = new UnityResolver(container);
return container;
}
我不确定 OwinBufferPolicySelector
是否是你需要的,因为你没有提供足够的信息,但你确实需要为它映射一个实现(可能还有几个其他接口)命令继续。
如评论中所述,如果您安装一个预制的 Unity 包(例如 Unity.WebAPI)来提供此实现,可能会更容易。
如果您遵循 Dependency Injection in ASP.NET Web API 2 文档,它也会有很大帮助。
我正在尝试在 WebApi 模板中使用 IoC 容器。我创建了一个空的 Web App Framework 项目,并选择使用 WebApi。然后我添加了 Unity NuGet 包并将以下内容添加到 Application_Start:
protected void Application_Start()
{
GlobalConfiguration.Configure(WebApiConfig.Register);
UnityConfig.Register();
}
UnityConfig:
public class UnityConfig
{
public static UnityContainer Register()
{
var container = new UnityContainer();
container.RegisterType<IMyClass, MyClass>();
GlobalConfiguration.Configuration.DependencyResolver = new UnityResolver(container);
return container;
}
}
我的 UnityResolver 在这里(代码大部分是抄袭的,但看起来很标准):
public class UnityResolver : IDependencyResolver
{
protected IUnityContainer container;
public UnityResolver(IUnityContainer container)
{
if (container == null)
{
throw new ArgumentNullException("container");
}
this.container = container;
}
public object GetService(Type serviceType)
{
try
{
return container.Resolve(serviceType);
}
catch (ResolutionFailedException)
{
return null;
}
}
public IEnumerable<object> GetServices(Type serviceType)
{
try
{
return container.ResolveAll(serviceType);
}
catch (ResolutionFailedException)
{
return new List<object>();
}
}
public IDependencyScope BeginScope()
{
var child = container.CreateChildContainer();
return new UnityResolver(child);
}
public void Dispose()
{
Dispose(true);
}
protected virtual void Dispose(bool disposing)
{
container.Dispose();
}
}
当我现在运行这个时,我得到以下错误:
Unity.Exceptions.ResolutionFailedException: 'Resolution of the dependency failed, type = 'System.Web.Http.Hosting.IHostBufferPolicySelector', name = '(none)'. Exception occurred while: while resolving. Exception is: InvalidOperationException - The current type, System.Web.Http.Hosting.IHostBufferPolicySelector, is an interface and cannot be constructed. Are you missing a type mapping? ----------------------------------------------- At the time of the exception, the container was: Resolving System.Web.Http.Hosting.IHostBufferPolicySelector,(none) '
很明显,在告诉它我想使用哪个 IoC 库之后,我需要注入一些内部依赖项,或者告诉它,或者更早(或更晚)注册它。
Clearly, after telling it which IoC library I want to use, I need to inject some internal dependencies, or tell it to, or perhaps, register this earlier (or later).
没错。您需要注册所有将通过 Unity 解析的类型。报错提示你缺少IHostBufferPolicySelector
,所以你需要注册
public static UnityContainer Register()
{
var container = new UnityContainer();
// Register all types with Unity here
container.RegisterType<IHostBufferPolicySelector, OwinBufferPolicySelector>();
GlobalConfiguration.Configuration.DependencyResolver = new UnityResolver(container);
return container;
}
我不确定 OwinBufferPolicySelector
是否是你需要的,因为你没有提供足够的信息,但你确实需要为它映射一个实现(可能还有几个其他接口)命令继续。
如评论中所述,如果您安装一个预制的 Unity 包(例如 Unity.WebAPI)来提供此实现,可能会更容易。
如果您遵循 Dependency Injection in ASP.NET Web API 2 文档,它也会有很大帮助。