Nic*_*cht 6 structuremap asp.net structuremap3
我有一个现有的应用程序使用2.x版本的Structuremap中的最后一个版本,它工作正常.StructureMap 3刚刚上线,我决定尝试更新它,看看它是怎么回事.
但无论我做什么,我似乎无法正确解决当前用户.我不确定它是否试图在应用程序的生命周期中过早地构建依赖项或者交易可能是什么.因为最近的发布,几乎没有任何信息,我发现它还没有任何用处.
注册依赖项的行.
For<HttpContextBase>().Use(() => new HttpContextWrapper(HttpContext.Current));
For<ICurrentUser>().HybridHttpOrThreadLocalScoped().Use(x => GetCurrentUser(x));
Run Code Online (Sandbox Code Playgroud)
我解决依赖关系的方法
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);
}
}
Run Code Online (Sandbox Code Playgroud)
我的ICurrentUser接口
public interface ICurrentUser
{
Guid UserId { get; }
string UserName { get; }
}
Run Code Online (Sandbox Code Playgroud)
行调用GetIdentityId()基本上只是一个扩展方法,它包含逻辑以检查存储在Identity上的UserId作为类型的声明项ClaimTypes.NameIdentifier,处理空值和合并到Guid等.
有没有其他人尝试在webapp中使用StructureMap3来做这样的事情呢?
Having run into this problem myself, it appears that every web related within StructureMap has been moved into a separate Nuget package called StructureMap.Web that can be found here.
我认为这是因为 StructureMap 3 现在符合 PLC(Portalble Class Library)标准,因此将其移动到单独的包中是有意义的。
一旦包含该包,一切都应该继续正常工作。
| 归档时间: |
|
| 查看次数: |
834 次 |
| 最近记录: |