Autofac.Core.DependencyResolutionException was unhandled by user code
Message=No scope with a Tag matching 'AutofacWebRequest' is visible from the scope in which the instance was requested. This generally indicates that a component registered as per-HTTP request is being requested by a SingleInstance() component (or a similar scenario.) Under the web integration always request dependencies from the DependencyResolver.Current or ILifetimeScopeProvider.RequestLifetime, never from the container itself.
Source=Autofac
StackTrace:
at Autofac.Core.Lifetime.MatchingScopeLifetime.FindScope(ISharingLifetimeScope mostNestedVisibleScope)
at Autofac.Core.Resolving.InstanceLookup..ctor(IComponentRegistration registration, IResolveOperation context, ISharingLifetimeScope mostNestedVisibleScope, IEnumerable`1 parameters)
at Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, IComponentRegistration registration, IEnumerable`1 parameters)
at Autofac.ResolutionExtensions.ResolveService(IComponentContext context, Service service, IEnumerable`1 parameters)
at Autofac.Core.Activators.Reflection.AutowiringPropertyInjector.InjectProperties(IComponentContext context, Object instance, Boolean overrideSetValues)
at Autofac.Core.Registration.ComponentRegistration.RaiseActivating(IComponentContext context, IEnumerable`1 parameters, Object& instance)
at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable`1 parameters)
at Autofac.Core.Resolving.InstanceLookup.Execute()
at Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, IComponentRegistration registration, IEnumerable`1 parameters)
at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
at Autofac.Features.Collections.CollectionRegistrationSource.<>c__DisplayClass4.<RegistrationsFor>b__0(IComponentContext c, IEnumerable`1 p)
at Autofac.Core.Activators.Delegate.DelegateActivator.ActivateInstance(IComponentContext context, IEnumerable`1 parameters)
at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable`1 parameters)
at Autofac.Core.Resolving.InstanceLookup.Execute()
at Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, IComponentRegistration registration, IEnumerable`1 parameters)
at Autofac.Core.Resolving.ResolveOperation.Execute(IComponentRegistration registration, IEnumerable`1 parameters)
at Autofac.ResolutionExtensions.ResolveService(IComponentContext context, Service service, IEnumerable`1 parameters)
at NServiceBus.ObjectBuilder.Autofac.AutofacObjectBuilder.ResolveAll(IComponentContext container, Type componentType) in y:\BuildAgent\work\31f8c64a6e8a2d7c\src\NServiceBus.Core\ObjectBuilder\Autofac\AutofacObjectBuilder.cs:line 192
at NServiceBus.ObjectBuilder.Common.CommonObjectBuilder.BuildAll[T]() in y:\BuildAgent\work\31f8c64a6e8a2d7c\src\NServiceBus.Core\ObjectBuilder\Common\CommonObjectBuilder.cs:line 179
at NServiceBus.MessageMutator.MutateOutgoingMessageBehavior.Invoke(SendLogicalMessageContext context, Action next) in y:\BuildAgent\work\31f8c64a6e8a2d7c\src\NServiceBus.Core\MessageMutator\MutateOutgoingMessageBehavior.cs:line 11
at NServiceBus.Sagas.SagaSendBehavior.Invoke(SendLogicalMessageContext context, Action next) in y:\BuildAgent\work\31f8c64a6e8a2d7c\src\NServiceBus.Core\Sagas\SagaSendBehavior.cs:line 41
at NServiceBus.Pipeline.PipelineFactory.Execute[T](BehaviorChain`1 pipelineAction, T context) in y:\BuildAgent\work\31f8c64a6e8a2d7c\src\NServiceBus.Core\Pipeline\PipelineFactory.cs:line 164
at NServiceBus.Pipeline.PipelineFactory.InvokeSendPipeline(SendOptions sendOptions, LogicalMessage message) in y:\BuildAgent\work\31f8c64a6e8a2d7c\src\NServiceBus.Core\Pipeline\PipelineFactory.cs:line 117
at NServiceBus.Unicast.Behaviors.MultiMessageBehavior.Invoke(SendLogicalMessagesContext context, Action next) in y:\BuildAgent\work\31f8c64a6e8a2d7c\src\NServiceBus.Core\Unicast\Behaviors\MultiMessageBehavior.cs:line 15
at NServiceBus.Unicast.Behaviors.MultiSendValidatorBehavior.Invoke(SendLogicalMessagesContext context, Action next) in y:\BuildAgent\work\31f8c64a6e8a2d7c\src\NServiceBus.Core\Unicast\Behaviors\MultiSendValidatorBehavior.cs:line 33
at NServiceBus.Pipeline.PipelineFactory.Execute[T](BehaviorChain`1 pipelineAction, T context) in y:\BuildAgent\work\31f8c64a6e8a2d7c\src\NServiceBus.Core\Pipeline\PipelineFactory.cs:line 164
at NServiceBus.Pipeline.PipelineFactory.InvokeSendPipeline(SendOptions sendOptions, IEnumerable`1 messages) in y:\BuildAgent\work\31f8c64a6e8a2d7c\src\NServiceBus.Core\Pipeline\PipelineFactory.cs:line 99
at NServiceBus.Unicast.UnicastBus.SendMessages(SendOptions sendOptions, List`1 messages) in y:\BuildAgent\work\31f8c64a6e8a2d7c\src\NServiceBus.Core\Unicast\UnicastBus.cs:line 704
at Mitek.XIP.Platform.Services.Security.UserAuthService.AuthenticateUser(String emailAddress, String password, String tenantReferenceId) in c:\git\Mitek.XIP.Platform\src\Domain\Mitek.XIP.Platform\Services\Security\UserAuthService.cs:line 39
at Mitek.XIP.Platform.UI.MVC.Controllers.AccountController.Login(LoginModel model) in c:\git\Mitek.XIP.Platform\src\UI\Mitek.XIP.Platform.UI.MVC\Controllers\AccountController.cs:line 179
at lambda_method(Closure , ControllerBase , Object[] )
at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<BeginInvokeSynchronousActionMethod>b__36(IAsyncResult asyncResult, ActionInvocation innerInvokeState)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3c()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass45.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3e()
InnerException:
Code:
### Global.asax.cs ###
public class MvcApplication : System.Web.HttpApplication
{
private static IContainer _Container;
private static IBus _Bus;
protected void Application_Start()
{
// create a model binder that will trim all string type input models
ModelBinders.Binders.Add(typeof(string), new TrimStringModelBinder());
_Container = AutofacConfig.Configure();
AutoMapperConfig.CreateMappers();
// Removes the X-AspNetMvc-Version Header
MvcHandler.DisableMvcResponseHeader = true;
// Configure NServiceBus
Configure.Serialization.Xml();
_Bus = Configure.With()
.AutofacBuilder(_Container)
.UseTransport<Msmq>()
.UnicastBus()
.CreateBus()
.Start(() => Configure.Instance.ForInstallationOn<NServiceBus.Installation.Environments.Windows>().Install());
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
}
### AutofacConfig.cs ###
public static class AutofacConfig
{
public static IContainer Configure()
{
var builder = new ContainerBuilder();
var assemblyScanner = new AssemblyScanner();
// register connection string provider
builder.RegisterType<XIPConnectionStringProvider>()
.As<IConnectionStringProvider>();
// register custom serializer for logging
builder.RegisterType<CustomSerializer>()
.As<ICustomSerializer>()
.PropertiesAutowired(PropertyWiringOptions.AllowCircularDependencies);
// register all Services
builder.RegisterAssemblyTypes(assemblyScanner.GetScannableAssemblies().Assemblies.ToArray())
.Where(t => t.Name.EndsWith("Service"))
.PropertiesAutowired(PropertyWiringOptions.AllowCircularDependencies)
.AsImplementedInterfaces().InstancePerHttpRequest();
// register Controllers
builder.RegisterControllers(typeof(MvcApplication).Assembly)
.PropertiesAutowired(PropertyWiringOptions.AllowCircularDependencies)
.InstancePerHttpRequest();
// register filter provider
builder.RegisterFilterProvider();
// register XIPContextWrapper
builder.Register<IXIPContextWrapper>(context =>
{
if (HttpContext.Current != null)
{
return new HttpContextXIPContextWrapper();
}
// TODO: Figure out what to do with non web/wcf applications
return null;
});
// build the container
var container = builder.Build();
// set mvc dependency resolver to use Autofac Dependecy Resolver
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
// return the container
return container;
}
}
### AccountController.cs ###
public class AccountController : Controller
{
// Bus instance will be injected
public IBus Bus { get; set; }
[HttpPost]
[AllowAnonymous]
public ActionResult Login(LoginModel model)
{
ViewBag.Title = "Login";
if (ModelState.IsValid)
{
var request = new AuthenticateUserRequestMessage
{
EmailAddress = model.EmailAddress,
EncryptedPassword = model.Password,
TenantReferenceId = null
};
// ### This is the line that throws the exception detailed above... ###
Bus.Send(request);
... other code here
}
return View(model);
}
}