I'm trying to setup StructureMap with NServiceBus. I've downloaded all the packages and NuGet created some files for me:
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:
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));
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.