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);
}
....
Che tipo di contenitore CIO stai usando? – Kamo
@Kamo Ho aggiornato la mia domanda –
Non è la riga 'Bootstrapper.Container as Container' che ti dà' null'? – Kamo