我有一个现有的应用程序,使用的是结构映射2.x版本的最后一个构建版本,它工作得很好。StructureMap 3最近刚刚上线,我决定试着更新一下,看看进展如何。
然而,无论我做什么,我似乎都不能让它正确地解析当前用户。我不确定它是否试图在应用程序生命周期的早期建立依赖关系,或者交易可能是什么。因为这个版本是最近发布的,所以我几乎还没有发现任何有用的信息。
注册依赖项的行。
For<HttpContextBase>().Use(() => new HttpContextWrapper(HttpContext.Current));
For<ICurrentUser>().HybridHttpOrThreadLocalScoped().Use(x => GetCurrentUser(x));我的解决依赖关系的方法
private ICurrentUser GetCurrentUser(IContext context)
{
try
{
var httpContext = context.GetInstance<HttpContextBase>();
if (httpContext == null) return null;
if (httpContext.User == null) return null;
var user = httpContext.User;
if (!user.Identity.IsAuthenticated) return null;
var personId = user.GetIdentityId().GetValueOrDefault();
return new CurrentUser(personId, user.Identity.Name);
}
catch (Exception ex)
{
context.GetInstance<ILogger>().Error("Error trying to determine the current user.", ex);
throw new Exception("Error trying to determine the current user.", ex);
}
}我的ICurrentUser界面
public interface ICurrentUser
{
Guid UserId { get; }
string UserName { get; }
}调用GetIdentityId()的代码行基本上只是一个扩展方法,用于包装逻辑以检查存储在Identity上的UserId是否为ClaimTypes.NameIdentifier类型的声明项、处理空值并合并到Guid等。
还有没有其他人尝试过在网页应用中使用StructureMap3来做这样的事情?
发布于 2014-04-16 21:10:22
我自己也遇到过这个问题,似乎每个与StructureMap相关的web都被移到了一个单独的名为StructureMap.Web的Nuget包中,这个包可以是found here。
我猜想这是因为PLC3现在是兼容StructureMap (可移植类库)的,所以把它移到一个单独的包中是有意义的。
一旦你包含了这个包,一切都应该像平常一样工作。
https://stackoverflow.com/questions/22846324
复制相似问题