OWIN 和 Global.asax.cs 文件

OWIN and Global.asax.cs file

我在我的项目中使用 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 文件中添加了这段代码,但在它之后我捕获了下一个异常:

An exception of type 'Bootstrap.Extensions.Containers.NoContainerException' occurred in Bootstrapper.dll but was not handled in user code

Additional information: Unable to continue. The container has not been initialized.

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

Unable to continue. The container has not been initialized.

Description: An unhandled exception occurred during the execution of the current web request. 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. The container has not been initialized.

Source Error:

Line 21: public Startup() Line 22: { Line 23:
Bootstrapper.IncludingOnly.Assembly(Assembly.Load("Dashboard.Rest")).With.SimpleInjector().With.ServiceLocator().Start(); Line 24: Container container = Bootstrapper.Container as Container; Line 25:
GlobalConfiguration.Configuration.DependencyResolver = new SimpleInjectorWebApiDependencyResolver(container);

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

更新 我有一些 SimpleInjectorRegisterTypes class 用于连接我的接口和服务:

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

我解决了这个问题。我只是使用其他 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.
} 

Dependency Injection in ASP.NET Web API 2