I have a service that requires a UserName
to be passed in during construction. The UserName
will come from the current web request either cookie or query string.
builder.Register((c,p) => {
var accessService = c.Resolve<IAccessService>();
var access = accessService.GetBySite(Request.QueryString["username"]);
return new JsonWebRequest(access.Site, access.Token);
}).InstancePerRequest();
I've tried to register as above although I receive this error message
Request is not available in this context
You should use
HttpContext.Current
to access the information from the active context.Another way of doing it is by using the
AutofacWebTypesModule
which will import registration forHttpRequestBase
. This module is available using the Autofac MVC5 nuget packageBy the way, for testing purpose and more flexibility I would recommend you to create a
IUserNameProvider
interface that will get you a usernameYou can use it like this :