Autofac.Core.DependencyResolutionException - Using Bus.Send...

1,046 views
Skip to first unread message

Brian Stanforth

unread,
Dec 30, 2013, 5:19:54 PM12/30/13
to particula...@googlegroups.com
Using latest of NServiceBus (4.3.x)

I am getting an error "Autofac.Core.DependencyResolutionException" when trying to Bus.Send from my MVC Controller.  Not sure why.  

Full error, stacktrace and basic code outlined below.  One thing to note is that I am building my own Autofac container and leveraging the AutofacBuilder in fluent configuration of Bus (see Global.asax.cs code)

Exception Details:

Autofac.Core.DependencyResolutionException was unhandled by user code
  HResult=-2146233088
  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);
}
}

John Simons

unread,
Dec 30, 2013, 6:41:28 PM12/30/13
to particula...@googlegroups.com
Hi Brian,

Can you create an issue in github, https://github.com/Particular/NServiceBus/issues/new
And provide us with a small repro (a github repo is fine)

Cheers
John



--
You received this message because you are subscribed to the Google Groups "Particular Software" group.
To unsubscribe from this group and stop receiving emails from it, send an email to particularsoftw...@googlegroups.com.
To post to this group, send email to particula...@googlegroups.com.
Visit this group at http://groups.google.com/group/particularsoftware.
To view this discussion on the web visit https://groups.google.com/d/msgid/particularsoftware/38d2022f-6ef5-4bcc-8b4e-3b4fdf381114%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Brian Stanforth

unread,
Jan 1, 2014, 8:14:48 PM1/1/14
to particula...@googlegroups.com
To unsubscribe from this group and stop receiving emails from it, send an email to particularsoftware+unsub...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages