2015-07-22 28 views
5

Uso nel mio progetto Owin e Katana per l'autorizzazione OAuth. E tutto il lavoro buono, ma nel file di global.asax.cs ho un po 'di codice per il contenitore del CIO:OWIN e Global.asax.cs file

Bootstrapper.IncludingOnly.Assembly(Assembly.Load("Dashboard.Rest")).With.SimpleInjector().With.ServiceLocator().Start(); 
      Container container = Bootstrapper.Container as Container; 
      GlobalConfiguration.Configuration.DependencyResolver = new SimpleInjectorWebApiDependencyResolver(container); 

ho aggiunto questo codice in Startup.cs file, ma dopo che mi cattura eccezione successiva:

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.

e se chiamo metodi API qualcuno che cattura eccezione successiva:

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

non so come risolvere il problema. Aiutami per favore. Grazie.

UPDATE ho alcuni SimpleInjectorRegisterTypes classe per collegare i miei interfacce e servizi:

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

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

E devo servizio dove scrivo la logica per l'API.

E nei miei controllori creo costruttore per chiamare il mio metodo con le interfacce di aiuto:

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

Che tipo di contenitore CIO stai usando? – Kamo

+0

@Kamo Ho aggiornato la mia domanda –

+0

Non è la riga 'Bootstrapper.Container as Container' che ti dà' null'? – Kamo

risposta

1

riparo questo problema. Uso solo un altro contenitore IOC Unity

Ecco un'implementazione di IDependencyResolver che include un contenitore Unity.

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

Alcuni controller con l'utilizzo contenitori 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

+0

Quindi in pratica la soluzione è smettere di usare la libreria Bootstrapper? – Steven

+0

@Steven proprio ora - sì. :) –