I have trouble using MembershipReboot
with the new ASP MVC5 template and Autofac
. I have used the default MVC5 template to set up the site and then tried to wire up the MembershipReboot
framework as a replacement for the ASP Identity framework that ships with the template.
This issue I am having is trying to resolve an IOwinContext
from the Autofac
container. Here is my wiring in the Startup class (cut down to basics). This is the wiring used in the samples for the MembershipReboot Owin
application (except there he uses Nancy).
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
var builder = new ContainerBuilder();
builder.RegisterControllers(Assembly.GetExecutingAssembly());
builder.Register(c => new DefaultUserAccountRepository())
.As<IUserAccountRepository>()
.As<IUserAccountQuery>()
.InstancePerLifetimeScope();
builder.RegisterType<UserAccountService>()
.AsSelf()
.InstancePerLifetimeScope();
builder.Register(ctx =>
{
**var owin = ctx.Resolve<IOwinContext>();** //fails here
return new OwinAuthenticationService(
MembershipRebootOwinConstants.AuthenticationType,
ctx.Resolve<UserAccountService>(),
owin.Environment);
})
.As<AuthenticationService>()
.InstancePerLifetimeScope();
var container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
ConfigureAuth(app);
app.Use(async (ctx, next) =>
{
using (var scope = container.BeginLifetimeScope(b =>
{
b.RegisterInstance(ctx).As<IOwinContext>();
}))
{
ctx.Environment.SetUserAccountService(() => scope.Resolve<UserAccountService>());
ctx.Environment.SetAuthenticationService(() => scope.Resolve<AuthenticationService>());
await next();
}
});
}
And here is my controller with the dependency specified in the controller constructor.
public class HomeController : Controller
{
private readonly AuthenticationService service;
public HomeController(AuthenticationService service)
{
this.service = service;
}
public ActionResult Index()
{
return View();
}
public ActionResult About()
{
ViewBag.Message = "Your application description page.";
return View();
}
public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";
return View();
}
}
It seems I need to wrap my Autofac
container in an AutofacDependencyResolver
in order for the MVC framework to use the container to resolve components. This is the only major difference from the Nancy Owin
sample and my use in MVC5.
When I do this, it then seems (from my Tracing) as if the dependency is being resolved without first going through the OWIN middleware
stack so the IOwinContext
is never registered.
What am I doing wrong here?
UPDATE:
Brock, your new sample works perfectly when I migrate the configuration over to my project. Just for my understanding, it seems that this line in your new sample registers the current OwinContext with the container and that is what was missing previously.
builder.Register(ctx=>HttpContext.Current.GetOwinContext()).As<IOwinContext>();
Is that