[英]“No parameterless constructor defined for this object.” for IBus in controller using StructureMap
I'm trying to setup StructureMap with NServiceBus. 我正在尝试使用NServiceBus设置StructureMap。 I've downloaded all the packages and NuGet created some files for me: 我已经下载了所有软件包,并且NuGet为我创建了一些文件:
Here is the code in those files 这是这些文件中的代码
IoC.cs: 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: 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. 如果我要注入IBus,则应排除此问题。
What have I already tried: 我已经尝试了什么:
Added parameterless constructor to the controller with following in it's body: 在控制器中添加了无参数构造函数,其主体如下:
_bus = _bus = new Container().GetInstance< IBus>(); _bus = _bus = new Container()。GetInstance <IBus>();
but _bus was still null and I got an exception connected to that. 但是_bus仍然为空,并且我有一个与此相关的异常。
Please assist. 请协助。
The same container for your own code and NServiceBus should be configured. 您应为自己的代码和NServiceBus配置相同的容器。 Below code shows this configuration for StructureMap. 下面的代码显示了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));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.