Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OWIN and Global.asax.cs file

I use in my project Owin and Katana for OAuth authorization. And all work good but in global.asax.cs file I have some code for IOC container:

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:

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.

and if I call someone api methods I catch next exception:

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

I don't know how to fix it. Help me please. Thanks.

UPDATE I have some SimpleInjectorRegisterTypes class for connect my interfaces and services:

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

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

And I have service where I write logic for API.

And in my controllers I create constructor for call my method with help interfaces:

 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);
        }
....
like image 508
Taras Kovalenko Avatar asked Jul 22 '15 06:07

Taras Kovalenko


1 Answers

I fix this problems. I just use other IOC container Unity

Here is an implementation of IDependencyResolver that wraps a Unity container.

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

Some controllers with use IoC containers:

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

like image 74
Taras Kovalenko Avatar answered Sep 28 '22 04:09

Taras Kovalenko