简体   繁体   中英

“No parameterless constructor defined for this object.” for IBus in controller using StructureMap

I'm trying to setup StructureMap with NServiceBus. I've downloaded all the packages and NuGet created some files for me:

NuGet为StructureMap创建的新文件

Here is the code in those files

IoC.cs:

public static class IoC {
    public static IContainer Initialize() {
        var cont = new Container();
        cont.Configure(x =>
                    {
                        x.Scan(scan =>
                                {
                                    scan.TheCallingAssembly();
                                    scan.WithDefaultConventions();
                                });
                        //x.For<IExample>().Use<Example>();
                    });
        return cont;
    }
}

StructureMap.cs:

[assembly: WebActivator.PreApplicationStartMethod(typeof(MyProjectName.App_Start.StructuremapMvc), "Start")]

namespace MyProjectName.App_Start {
    public static class StructuremapMvc {
        public static void Start() {
            IContainer container = IoC.Initialize();
            DependencyResolver.SetResolver(new StructureMapDependencyResolver(container));
            GlobalConfiguration.Configuration.DependencyResolver = new StructureMapDependencyResolver(container);
        }
    }
}

Didn't change anything, so those files are the way, they were created. Then I added a constructor in one of my controllers:

public class ProductsController : Controller
{
    private readonly IBus _bus;

    public ProductsController(IBus bus)
    {
        _bus = bus;
    }

    public ActionResult Index()
    {
        ViewBag.Title = "Product list";

        var message = new ProductMessage();
        _bus.Send(message);

        return View();
    }
}

That's when I got the error

No parameterless constructor defined for this object.

which is weird, since this line

scan.WithDefaultConventions();

should exclude this issue if I'm trying to inject IBus.

What have I already tried:

  • Removing [assembly: ...] from StructuremapMvc.cs and calling StructuremapMvc.Start() from Global.asax. Same result.
  • Added parameterless constructor to the controller with following in it's body:

    _bus = _bus = new Container().GetInstance< IBus>();

    but _bus was still null and I got an exception connected to that.

Please assist.

The same container for your own code and NServiceBus should be configured. Below code shows this configuration for StructureMap.

BusConfiguration busConfiguration = new BusConfiguration();

//Configure the container and use the same one for MVC and NServiceBus    
Container container = new Container();

busConfiguration.UseContainer<StructureMapBuilder>(c => c.ExistingContainer(container));

More information

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM