OWIN and Global.asax.cs file

本文关键字:cs file asax Global and OWIN | 更新日期: 2023-09-27 18:04:01

我在我的项目中使用Owin和Katana进行OAuth授权。所有的工作都很好,但在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);

我在Startup.cs文件中添加了这段代码,但在它之后,我捕获了下一个异常:

类型的异常"Bootstrap.Extensions.Containers。NoContainerException'发生在Bootstrapper.dll,但未在用户代码中处理

附加信息:无法继续。集装箱还没有初始化。

,如果我调用某人的api方法,我捕获下一个异常:

无法继续。容器未初始化

描述:在执行过程中发生未处理的异常当前的web请求。请查看堆栈跟踪了解更多信息有关错误及其在代码中的起源的信息。

异常细节:Bootstrap.Extensions.Containers.NoContainerException: cannot to继续下去。容器未初始化

源错误:

第21行:public Startup()第22行:{第23行:
Bootstrapper.IncludingOnly.Assembly (Assembly.Load("Dashboard.Rest")).With.SimpleInjector () .With.ServiceLocator () .Start ();第24行:容器容器=引导程序。容器容器;25行:
GlobalConfiguration.Configuration.DependencyResolver = new SimpleInjectorWebApiDependencyResolver(容器);

我不知道如何修理它。请帮帮我。谢谢。

我有一些SimpleInjectorRegisterTypes类连接我的接口和服务:

 public class SimpleInjectorRegisterTypes : ISimpleInjectorRegistration
    {
        public void Register(Container container)
            container.RegisterSingle<IApplication, ApplicationService>();      
        }
    }

我有一个服务,我为API编写逻辑。

在我的控制器中,我用帮助接口创建构造函数来调用我的方法:

 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);
        }
....

OWIN and Global.asax.cs file

我解决了这个问题。我只是使用了其他的IOC容器Unity

这是一个包装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

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.
}

一些使用IoC容器的控制器:

public class ProductsController : ApiController
{
    private IProductRepository _repository;
    public ProductsController(IProductRepository repository)  
    {
        _repository = repository;
    }
    // Other controller methods not shown.
} 

ASP中的依赖注入. NET Web API 2