Inject dependency of current user in MVC application using StructureMap3

I have an existing application using the latest build from version 2.x of Structuremap and it works fine. StructureMap 3 recently went live, and I decided to try updating and see how it goes.

However, no matter what I do, I cannot get it to correctly allow the current user. I'm not sure if he is trying to build dependencies too early during the life of the application or that there may be a deal. Since the release is so recent, there is almost no information that I thought would come in handy.

The string that records the dependency.

For<HttpContextBase>().Use(() => new HttpContextWrapper(HttpContext.Current)); For<ICurrentUser>().HybridHttpOrThreadLocalScoped().Use(x => GetCurrentUser(x)); 

My method for resolving dependencies

  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); } } 

My ICurrentUser Interface

 public interface ICurrentUser { Guid UserId { get; } string UserName { get; } } 

The line that calls GetIdentityId() is basically just an extension method that wraps the logic to verify that the UserId stored in Identity, as a claim item of type ClaimTypes.NameIdentifier , processes zeros and combines with Guid, etc.

Has anyone else tried using StructureMap3 in a webapp for something like this?

+6
source share
1 answer

Faced with this problem initially, all the websites related to StructureMap were moved to a separate Nuget package called StructureMap.Web , which can be found here .

I assume that this is due to the fact that StructureMap 3 is now compatible with PLC (Portalble Class Library), so its meaning in a separate package makes sense.

After including this package, everything should continue as usual.

+1
source

All Articles