【发布时间】:2018-05-26 18:27:01
【问题描述】:
所以我最近开始使用结构图。我有一个使用身份的 web api。我的目标是每个请求都有一个 dbcontext。目前我遇到了上下文问题,通过结构映射注入的上下文与用户管理器使用的上下文不同。我环顾四周并找不到解决方案,尽管我确信它很明显。
所以首先我有我的结构图设置。
public class DefaultRegistry : Registry {
#region Constructors and Destructors
public DefaultRegistry() {
Scan(
scan => {
scan.TheCallingAssembly();
scan.AddAllTypesOf<IImporterService>().NameBy(type => type.Name);
scan.WithDefaultConventions();
});
For<IHttpContextBaseWrapper>().Use<HttpContextBaseWrapper>();
For<ApplicationDbContext>().Use(() => new ApplicationDbContext());
For<HttpContextBase>().Use(() => new HttpContextWrapper(HttpContext.Current));
}
#endregion
}
我暂时不需要注入用户管理器,因为它是通过基础服务中的 httpcontext 访问的,我知道这可能需要更改以进行未来的单元测试。
接下来我们进行启动验证。
private void ConfigureOAuthTokenGeneration(IAppBuilder app)
{
app.CreatePerOwinContext(ApplicationDbContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
app.CreatePerOwinContext<ApplicationRoleManager>(ApplicationRoleManager.Create);
//Token Auth
var OAuthOptions = new OAuthAuthorizationServerOptions
{
TokenEndpointPath = new PathString("/api/account/login"),
Provider = new ApplicationOAuthProvider("self"),
AuthorizeEndpointPath = new PathString("/api/AccountApi/ExternalLogin"),
AccessTokenExpireTimeSpan = TimeSpan.FromDays(121),
RefreshTokenProvider = new ApplicationRefreshTokenProvider(),
//#if DEBUG
AllowInsecureHttp = true,
//#endif
};
// Enable the application to use bearer tokens to authenticate users
app.UseOAuthBearerTokens(OAuthOptions);
}
我已经减少了代码,但我有典型的服务和存储层结构。所有注入都使用结构映射,服务和存储库具有正确的上下文,唯一没有的是用户管理器。我知道它下面的行导致了这个问题:
app.CreatePerOwinContext(ApplicationDbContext.Create);
但是,就像我说的,我仍然习惯于构建映射,我之前使用了 unity,并且只会从当前容器中解析我需要的实例。非常感谢任何帮助或指导。
【问题讨论】:
-
但为什么要投反对票?
标签: c# asp.net asp.net-identity structuremap