SimpleInjector 不起作用 - OWIN 上的 Web API
SimpleInjector doesn't work - Web API on OWIN
OwinStartup.cs
public class OwinStartup
{
internal static IDataProtectionProvider DataProtectionProvider { get; private set; }
public void Configuration(IAppBuilder app)
{
DataProtectionProvider = app.GetDataProtectionProvider();
var config = new HttpConfiguration();
SimpleInjectorConfig.Configure(app);
ConfigureOAuth(app);
WebApiConfig.Register(config);
app.UseCors(CorsOptions.AllowAll);
app.UseWebApi(config);
}
private static void ConfigureOAuth(IAppBuilder app)
{
app.CreatePerOwinContext(
() => (IDisposable)GlobalConfiguration.Configuration.DependencyResolver.GetService(
typeof(AppUserManager)));
var options = new OAuthAuthorizationServerOptions
{
TokenEndpointPath = new PathString("/Token"),
Provider = new AppAuthProvider(),
AllowInsecureHttp = true,
};
app.UseOAuthAuthorizationServer(options);
app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());
}
}
SimpleInjectorConfig.cs
public static class SimpleInjectorConfig
{
public static void Configure(IAppBuilder app)
{
var container = new Container();
container.Options.DefaultScopedLifestyle = new AsyncScopedLifestyle();
//allows scoped instances to be resolved during OWIN request
app.Use(async (context, next) =>
{
using (AsyncScopedLifestyle.BeginScope(container))
{
await next();
}
});
container.Register<AppIdentityDbContext>(Lifestyle.Scoped);
container.Register<AppUserManager>(Lifestyle.Scoped);
container.Register(
() =>
container.IsVerifying
? new OwinContext().Authentication
: HttpContext.Current.GetOwinContext().Authentication, Lifestyle.Scoped);
container.Register<AppSignInManager>(Lifestyle.Scoped);
container.Verify();
GlobalConfiguration.Configuration.DependencyResolver =
new SimpleInjectorWebApiDependencyResolver(container);
}
}
所以在我的 OAuthAuthorizationServerProvider
实现中调用 AppAuthProvider
我试图使用此代码获取 AppUserManager
的实例(我需要找到用户):
var manager = context.OwinContext.Get<AppUserManager>();
但不知道为什么我还是得到 null
。我真的不知道该怎么做,因为一切似乎都配置正确。有任何想法吗 ?谢谢!
我找到了解决办法。更新了以下代码:
OwinStartup.cs
public class OwinStartup
{
internal static IDataProtectionProvider DataProtectionProvider { get; private set; }
public void Configuration(IAppBuilder app)
{
DataProtectionProvider = app.GetDataProtectionProvider();
var container = SimpleInjectorConfig.Configure();
//allows scoped instances to be resolved during OWIN request
app.Use(async (context, next) =>
{
using (AsyncScopedLifestyle.BeginScope(container))
{
await next();
}
});
var config = new HttpConfiguration
{
DependencyResolver = new SimpleInjectorWebApiDependencyResolver(container)
};
ConfigureOAuth(app, config);
WebApiConfig.Register(config);
app.UseCors(CorsOptions.AllowAll);
app.UseWebApi(config);
}
private static void ConfigureOAuth(IAppBuilder app, HttpConfiguration config)
{
app.CreatePerOwinContext(
() => (AppUserManager)config.DependencyResolver.GetService(
typeof(AppUserManager)));
var options = new OAuthAuthorizationServerOptions
{
TokenEndpointPath = new PathString("/Token"),
Provider = new AppAuthProvider(),
//TODO: Change in production.
AllowInsecureHttp = true,
};
app.UseOAuthAuthorizationServer(options);
app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());
}
}
SimpleInjectorConfig.cs
public static class SimpleInjectorConfig
{
public static Container Configure()
{
var container = new Container();
container.Options.DefaultScopedLifestyle = new AsyncScopedLifestyle();
container.Register<AppIdentityDbContext>(Lifestyle.Scoped);
container.Register<AppUserManager>(Lifestyle.Scoped);
container.Register(
() =>
container.IsVerifying
? new OwinContext().Authentication
: HttpContext.Current.GetOwinContext().Authentication, Lifestyle.Scoped);
container.Register<AppSignInManager>(Lifestyle.Scoped);
container.Verify();
return container;
}
}
也许有人会用到。
OwinStartup.cs
public class OwinStartup
{
internal static IDataProtectionProvider DataProtectionProvider { get; private set; }
public void Configuration(IAppBuilder app)
{
DataProtectionProvider = app.GetDataProtectionProvider();
var config = new HttpConfiguration();
SimpleInjectorConfig.Configure(app);
ConfigureOAuth(app);
WebApiConfig.Register(config);
app.UseCors(CorsOptions.AllowAll);
app.UseWebApi(config);
}
private static void ConfigureOAuth(IAppBuilder app)
{
app.CreatePerOwinContext(
() => (IDisposable)GlobalConfiguration.Configuration.DependencyResolver.GetService(
typeof(AppUserManager)));
var options = new OAuthAuthorizationServerOptions
{
TokenEndpointPath = new PathString("/Token"),
Provider = new AppAuthProvider(),
AllowInsecureHttp = true,
};
app.UseOAuthAuthorizationServer(options);
app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());
}
}
SimpleInjectorConfig.cs
public static class SimpleInjectorConfig
{
public static void Configure(IAppBuilder app)
{
var container = new Container();
container.Options.DefaultScopedLifestyle = new AsyncScopedLifestyle();
//allows scoped instances to be resolved during OWIN request
app.Use(async (context, next) =>
{
using (AsyncScopedLifestyle.BeginScope(container))
{
await next();
}
});
container.Register<AppIdentityDbContext>(Lifestyle.Scoped);
container.Register<AppUserManager>(Lifestyle.Scoped);
container.Register(
() =>
container.IsVerifying
? new OwinContext().Authentication
: HttpContext.Current.GetOwinContext().Authentication, Lifestyle.Scoped);
container.Register<AppSignInManager>(Lifestyle.Scoped);
container.Verify();
GlobalConfiguration.Configuration.DependencyResolver =
new SimpleInjectorWebApiDependencyResolver(container);
}
}
所以在我的 OAuthAuthorizationServerProvider
实现中调用 AppAuthProvider
我试图使用此代码获取 AppUserManager
的实例(我需要找到用户):
var manager = context.OwinContext.Get<AppUserManager>();
但不知道为什么我还是得到 null
。我真的不知道该怎么做,因为一切似乎都配置正确。有任何想法吗 ?谢谢!
我找到了解决办法。更新了以下代码:
OwinStartup.cs
public class OwinStartup
{
internal static IDataProtectionProvider DataProtectionProvider { get; private set; }
public void Configuration(IAppBuilder app)
{
DataProtectionProvider = app.GetDataProtectionProvider();
var container = SimpleInjectorConfig.Configure();
//allows scoped instances to be resolved during OWIN request
app.Use(async (context, next) =>
{
using (AsyncScopedLifestyle.BeginScope(container))
{
await next();
}
});
var config = new HttpConfiguration
{
DependencyResolver = new SimpleInjectorWebApiDependencyResolver(container)
};
ConfigureOAuth(app, config);
WebApiConfig.Register(config);
app.UseCors(CorsOptions.AllowAll);
app.UseWebApi(config);
}
private static void ConfigureOAuth(IAppBuilder app, HttpConfiguration config)
{
app.CreatePerOwinContext(
() => (AppUserManager)config.DependencyResolver.GetService(
typeof(AppUserManager)));
var options = new OAuthAuthorizationServerOptions
{
TokenEndpointPath = new PathString("/Token"),
Provider = new AppAuthProvider(),
//TODO: Change in production.
AllowInsecureHttp = true,
};
app.UseOAuthAuthorizationServer(options);
app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());
}
}
SimpleInjectorConfig.cs
public static class SimpleInjectorConfig
{
public static Container Configure()
{
var container = new Container();
container.Options.DefaultScopedLifestyle = new AsyncScopedLifestyle();
container.Register<AppIdentityDbContext>(Lifestyle.Scoped);
container.Register<AppUserManager>(Lifestyle.Scoped);
container.Register(
() =>
container.IsVerifying
? new OwinContext().Authentication
: HttpContext.Current.GetOwinContext().Authentication, Lifestyle.Scoped);
container.Register<AppSignInManager>(Lifestyle.Scoped);
container.Verify();
return container;
}
}
也许有人会用到。