c# - OWIN 和 Global.asax.cs 文件

标签 c# asp.net owin global-asax simple-injector

我在我的项目中使用 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 类用于连接我的接口(interface)和服务:

 public class SimpleInjectorRegisterTypes : ISimpleInjectorRegistration
    {
        public void Register(Container container)

            container.RegisterSingle<IApplication, ApplicationService>();      
        }
    }

我有为 API 编写逻辑的服务。

在我的 Controller 中,我创建构造函数以使用帮助接口(interface)调用我的方法:

 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 容器的 Controller :

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

关于c# - OWIN 和 Global.asax.cs 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31555701/

相关文章:

c# - RegisterStartupScript 和 RegisterClientScriptBlock 哪个更快?

c# - 不使用 HttpContext 获取当前 OwinContext

c# - Startup.cs 中的 OpenIdConnectAuthenticationOptions 问题(AuthenticationFailed 属性)

c# - 绑定(bind)两个不同类但具有相似属性的对象

c# - 在 C# 中的 BouncyCaSTLe 内存中 PGP 加密

c# - 超链接可见性的 ASP.NET 声明性绑定(bind)不起作用

C# 和 JavaScript - 转义单引号字符

security - Microsoft.Owin.Security.ActiveDirectory 是用于制作使用 AD 的 Owin 身份验证中间件吗?

c# - 在异步方法中使用 await 来防止下一行代码运行

c# - 具有两种方式数据绑定(bind)的 Winforms 文本框无法正常工作