0

I'm trying to send a command from a filter in my MVC4 project to my command processor.

The problem:
I can't get an NServiceBus instance in the filter to fill.

The components:

  • ASP.NET MVC 4
  • NServiceBus version 3
  • StructureMap

The Attribute/Filter:

namespace AMS.WebApp.Filters
{
    public class AMSAuthorizeAttribute : AuthorizeAttribute
    {
        public IBus Bus { get; set; }
        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            bool accessGranted = false;

            accessGranted = base.AuthorizeCore(httpContext);


            // arbitrary command, don't worry about it
            // Why is Bus still null?
            var requestAccess = new RequestingAccess();
            Bus.Send("AMS.AccessControl.CommandProcessor", requestAccess);

            //if(isAdmin)
            //  accessGranted = true;

            #if DEBUG
                accessGranted = true;
            #endif

            return accessGranted;
        }
    }
}

The IOC Code:

using AMS.WebApp.Filters;
using NServiceBus;
using StructureMap;
namespace AMS.WebApp.DependencyResolution {
    public static class IoC {
        public static IContainer Initialize() {
            ObjectFactory.Initialize(x =>
                        {
                            x.Scan(scan =>
                                    {
                                        scan.AssembliesFromApplicationBaseDirectory();
                                        scan.WithDefaultConventions();
                                    });

                            //This doesn't work
                            //x.SetAllProperties(y => y.OfType<IBus>());  

                            //Neither does this                            
                            //x.ForConcreteType<AMSAuthorizeAttribute>()
                            // .Configure
                            // .Setter<IBus>(a => a.Bus)
                            // .IsTheDefault();
                        });
            return ObjectFactory.Container;
        }
    }
}

Also, my attempt to bypass structuremap completely by passing in the bus instance from the controller resulted in:

An object reference is required for the non-static field, method, or property

At this point I'm pretty sure its something awkward with attributes/filters and structuremap, but I'm not really sure what that is.

WARNING: the accepted answer does not fix the actual problem of getting nservicebus in an action filter, but it does address how to get DI in an action filter. See ASP.NET MVC4 NServiceBus Attribute/Filter StructureMap for the Nservicebus specific question

Community
  • 1
  • 1
ton.yeung
  • 4,793
  • 6
  • 41
  • 72

1 Answers1

0

Take a look at this post. I think this is what you're looking for.

http://lostechies.com/jimmybogard/2010/05/03/dependency-injection-in-asp-net-mvc-filters/

Edit:

I think you have two different issues here.

  1. Using DI on a filter
  2. Configuring DI on NServiceBus

Can you please post your code which initializes NServiceBus for StructureMap?

You are looking for somthing like this:

Configure.With().StructureMapBuilder()

  • Interesting, I think this does it. I'll try it tomorrow at work and mark the answer if it works. Thanks! – ton.yeung Apr 24 '13 at 03:59
  • This works on a sample project I created. I removed the stuff in the article and the setter injection doesn't work, add it, and the setter injection works. Unfortunately, NServiceBus throws a fit with the extra configuration: StructureMap Exception Code: 202 No Default Instance defined for PluginFamily NServiceBus.Unicast.Subscriptions.ISubscriptionStorage, NServiceBus.Core, Version=3.3.0.0, Culture=neutral, PublicKeyToken=9fc386479f8a226c – ton.yeung Apr 24 '13 at 14:44
  • As John states below, make sure you explicitly tell NServiceBus to use StructureMap. From your code, I don't see this step being performed. If you skip this step, NSB will use it's default which I think is AutoFac. This would explain why StructureMap don't find the NServiceBus interfaces. – Corey Behrends Apr 24 '13 at 23:21