Я переношу большую базу кода из Ninject в Autofac и борюсь за одно из привязок (что, по моему мнению, вызывает ошибку активации на основе некоторой моей отладки).
Ninject:
Bind<ISecureDataFormat<AuthenticationTicket>>()
.ToMethod(context =>
{
var owinContext = context.Kernel.Get<IOwinContext>();
return owinContext
.Get<ISecureDataFormat<AuthenticationTicket>>("SecureDataFormat");
});
Autofac (что у меня есть):
builder.Register(
context => context.Resolve<IOwinContext>()
.Get<ISecureDataFormat<AuthenticationTicket>>("SecureDataFormat"))
.As<ISecureDataFormat<AuthenticationTicket>>();
Startup.cs:
var container = RegisterIoC(app, config);
public IContainer RegisterIoC(IAppBuilder app, HttpConfiguration config)
{
var builder = new ContainerBuilder();
builder = RegisterDependencies(builder);
builder = RegisterFilters(builder, config);
/*builder.RegisterModule<DebuggingRequestModule>();*/
var container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
config.DependencyResolver = new AutofacWebApiDependencyResolver(container);
app.UseAutofacMiddleware(container);
app.UseAutofacMvc();
app.UseAutofacWebApi(config);
app.UseWebApi(config);
return container;
}
Больше:
builder.RegisterModule<ApiDependencyModule>().RegisterModule<AutofacWebTypesModule>();
builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
builder.RegisterApiControllers(typeof(AccountController).Assembly);
(по-видимому) связанных конструкторов:
AccountController.cs:
public AccountController(ILoginService loginService,
IBearerTokenStore tokenStore,
IRememberMeCookieService rememberMeCookieService,
IInternalSsoChallenge ssoChallenge)
{
LoginService.cs:
public LoginService(IBearerTokenStore tokenStore,
IGrantTypeProvider grantProvider,
IRememberMeCookieService rememberMeCookieService)
{
BearerTokenCookieStore.cs:
public BearerTokenCookieStore(IOwinContext owinContext, ISecureDataFormat<AuthenticationTicket> secureDataFormat, IAppSettingsReader appSettingsReader, ICookieService cookieService)
{
У меня есть модуль регистрации, который помогает мне отлаживать, и это список сообщений, которые у меня есть.
Resolving _______.Login.Controllers.AccountController
--Resolving _______.ApiGateway.Services.Auth.LoginService
----Resolving _______.ApiGateway.Security.OAuth.BearerTokenCookieStore
------Resolving Microsoft.Owin.Security.DataHandler.SecureDataFormat'1[Microsoft.Owin.Security.AuthenticationTicket]
Exception thrown: 'Autofac.Core.DependencyResolutionException' in Autofac.dll
Exception thrown: 'Autofac.Core.DependencyResolutionException' in Autofac.dll
Exception thrown: 'Autofac.Core.DependencyResolutionException' in Autofac.dll
The thread 0x1014 has exited with code 0 (0x0).
Редактировать:
Исключением я вижу:
An error occurred during the activation of a particular registration.
See the inner exception for details. Registration:
Activator = AccountController (DelegateActivator),
Services = [____.Login.Controllers.AccountController],
Lifetime = Autofac.Core.Lifetime.CurrentScopeLifetime,
Sharing = None,
Ownership = ExternallyOwned --->
An error occurred during the activation of a particular registration.
See the inner exception for details.
Registration:
Activator = AccountController (ReflectionActivator),
Services = [____.Login.Controllers.AccountController],
Lifetime = Autofac.Core.Lifetime.CurrentScopeLifetime,
Sharing = None, Ownership = OwnedByLifetimeScope ---> An error occurred during the activation of a particular registration.
See the inner exception for details.
Registration:
Activator = LoginService (DelegateActivator),
Services = [____.ApiGateway.Services.Auth.ILoginService],
Lifetime = Autofac.Core.Lifetime.CurrentScopeLifetime,
Sharing = None,
Ownership = ExternallyOwned ---> An error occurred during the activation of a particular registration. See the inner exception for details. Registration: Activator = LoginService (ReflectionActivator),
Services = [____.ApiGateway.Services.Auth.ILoginService],
Lifetime = Autofac.Core.Lifetime.CurrentScopeLifetime,
Sharing = None,
Ownership = OwnedByLifetimeScope ---> An error occurred during the activation of a particular registration. See the inner exception for details. Registration: Activator = BearerTokenCookieStore (DelegateActivator),
Services = [____.ApiGateway.Security.OAuth.IBearerTokenStore],
Lifetime = Autofac.Core.Lifetime.CurrentScopeLifetime,
Sharing = None,
Ownership = ExternallyOwned ---> An error occurred during the activation of a particular registration. See the inner exception for details. Registration: Activator = BearerTokenCookieStore (ReflectionActivator),
Services = [____.ApiGateway.Security.OAuth.IBearerTokenStore],
Lifetime = Autofac.Core.Lifetime.CurrentScopeLifetime,
Sharing = Shared,
Ownership = OwnedByLifetimeScope ---> An error occurred during the activation of a particular registration. See the inner exception for details. Registration: Activator = ISecureDataFormat'1 (DelegateActivator),
Services = [Microsoft.Owin.Security.ISecureDataFormat'1[[Microsoft.Owin.Security.AuthenticationTicket,
Microsoft.Owin.Security,
Version=3.0.1.0,
Culture=neutral,
PublicKeyToken=31bf3856ad364e35]]],
Lifetime = Autofac.Core.Lifetime.CurrentScopeLifetime,
Sharing = None,
Ownership = ExternallyOwned ---> An error occurred during the activation of a particular registration. See the inner exception for details. Registration: Activator = ISecureDataFormat'1 (DelegateActivator),
Services = [Microsoft.Owin.Security.ISecureDataFormat'1[[Microsoft.Owin.Security.AuthenticationTicket,
Microsoft.Owin.Security,
Version=3.0.1.0,
Culture=neutral,
PublicKeyToken=31bf3856ad364e35]]],
Lifetime = Autofac.Core.Lifetime.CurrentScopeLifetime,
Sharing = None,
Ownership = OwnedByLifetimeScope ---> A delegate registered to create instances of 'Microsoft.Owin.Security.ISecureDataFormat'1[Microsoft.Owin.Security.AuthenticationTicket]' returned null. (See inner exception for details.) (See inner exception for details.) (See inner exception for details.) (See inner exception for details.) (See inner exception for details.) (See inner exception for details.) (See inner exception for details.) (See inner exception for details.)
Внутреннее большинство:
A delegate registered to create instances of 'Microsoft.Owin.Security.ISecureDataFormat'1[Microsoft.Owin.Security.AuthenticationTicket]' returned null.