[英]OWIN and Global.asax.cs file
I use in my project Owin and Katana for OAuth authorization. 我在我的项目Owin和Katana中使用OAuth授权。 And all work good but in global.asax.cs file I have some code for IOC container:
并且所有工作都很好但在global.asax.cs文件中我有一些IOC容器的代码:
Bootstrapper.IncludingOnly.Assembly(Assembly.Load("Dashboard.Rest")).With.SimpleInjector().With.ServiceLocator().Start();
Container container = Bootstrapper.Container as Container;
GlobalConfiguration.Configuration.DependencyResolver = new SimpleInjectorWebApiDependencyResolver(container);
I added this code in Startup.cs file but after it I catch next exception: 我在Startup.cs文件中添加了这段代码,但之后我抓住了下一个异常:
An exception of type 'Bootstrap.Extensions.Containers.NoContainerException' occurred in Bootstrapper.dll but was not handled in user code
Bootstrapper.dll中出现“Bootstrap.Extensions.Containers.NoContainerException”类型的异常,但未在用户代码中处理
Additional information: Unable to continue.
其他信息:无法继续。 The container has not been initialized.
容器尚未初始化。
and if I call someone api methods I catch next exception: 如果我调用某人api方法,我会抓住下一个异常:
Unable to continue.
无法继续。 The container has not been initialized.
容器尚未初始化。
Description: An unhandled exception occurred during the execution of the current web request.
描述:执行当前Web请求期间发生未处理的异常。 Please review the stack trace for more information about the error and where it originated in the code.
请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。
Exception Details: Bootstrap.Extensions.Containers.NoContainerException: Unable to continue.
异常详细信息:Bootstrap.Extensions.Containers.NoContainerException:无法继续。 The container has not been initialized.
容器尚未初始化。
Source Error:
来源错误:
Line 21: public Startup() Line 22: { Line 23:
第21行:public Startup()第22行:{第23行:
Bootstrapper.IncludingOnly.Assembly(Assembly.Load("Dashboard.Rest")).With.SimpleInjector().With.ServiceLocator().Start();。Bootstrapper.IncludingOnly.Assembly(Assembly.Load( “Dashboard.Rest”))With.SimpleInjector()With.ServiceLocator()启动(); Line 24: Container container = Bootstrapper.Container as Container;
第24行:容器容器= Bootstrapper.Container作为容器; Line 25:
第25行:
GlobalConfiguration.Configuration.DependencyResolver = new SimpleInjectorWebApiDependencyResolver(container);GlobalConfiguration.Configuration.DependencyResolver = new SimpleInjectorWebApiDependencyResolver(container);
I don't know how to fix it. 我不知道如何解决它。 Help me please.
请帮帮我。 Thanks.
谢谢。
UPDATE I have some SimpleInjectorRegisterTypes class for connect my interfaces and services: 更新我有一些SimpleInjectorRegisterTypes类用于连接我的接口和服务:
public class SimpleInjectorRegisterTypes : ISimpleInjectorRegistration
{
public void Register(Container container)
container.RegisterSingle<IApplication, ApplicationService>();
}
}
And I have service where I write logic for API. 我在为API编写逻辑的地方提供服务。
And in my controllers I create constructor for call my method with help interfaces: 在我的控制器中,我使用帮助接口创建用于调用my方法的构造函数:
public class ApplicationController : ApiController
{
private readonly IApplication _application;
public ApplicationController(IApplication application)
{
_application = application;
}
[HttpGet]
public IHttpActionResult GetAllApps()
{
var apps = _application.GetAllApps();
return apps == null ? (IHttpActionResult)Ok(new Application()) : Ok(apps);
}
....
I fix this problems. 我解决了这个问题。 I just use other IOC container Unity
我只是使用其他IOC容器Unity
Here is an implementation of IDependencyResolver that wraps a Unity container. 以下是包装Unity容器的IDependencyResolver的实现。
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()
{
container.Dispose();
}
}
WebApiConfig.cs WebApiConfig.cs
public static void Register(HttpConfiguration config)
{
var container = new UnityContainer();
container.RegisterType<IProductRepository, ProductRepository>(new HierarchicalLifetimeManager());
config.DependencyResolver = new UnityResolver(container);
// Other Web API configuration not shown.
}
Some controllers with use IoC containers: 一些使用IoC容器的控制器:
public class ProductsController : ApiController
{
private IProductRepository _repository;
public ProductsController(IProductRepository repository)
{
_repository = repository;
}
// Other controller methods not shown.
}
Dependency Injection in ASP.NET Web API 2 ASP.NET Web API 2中的依赖注入
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.