Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# &引用;没有为此对象定义无参数构造函数。”;对于控制器中使用StructureMap的IBU_C#_Asp.net_Dependency Injection_Nservicebus_Structuremap - Fatal编程技术网

C# &引用;没有为此对象定义无参数构造函数。”;对于控制器中使用StructureMap的IBU

C# &引用;没有为此对象定义无参数构造函数。”;对于控制器中使用StructureMap的IBU,c#,asp.net,dependency-injection,nservicebus,structuremap,C#,Asp.net,Dependency Injection,Nservicebus,Structuremap,我正在尝试使用NServiceBus设置StructureMap。 我已经下载了所有软件包,NuGet为我创建了一些文件: 下面是这些文件中的代码 IoC.cs: public static class IoC { public static IContainer Initialize() { var cont = new Container(); cont.Configure(x => {

我正在尝试使用NServiceBus设置StructureMap。 我已经下载了所有软件包,NuGet为我创建了一些文件:

下面是这些文件中的代码

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;
    }
}
没有更改任何内容,所以这些文件就是这样创建的。 然后我在我的一个控制器中添加了一个构造函数:

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();
    }
}
那就是我犯错误的时候

没有为此对象定义无参数构造函数

这很奇怪,因为这条线

scan.WithDefaultConventions();
如果我尝试注入IBus,应该排除此问题

我已经试过了:

  • 从StructuremapMvc.cs中删除[assembly:…]并调用 来自Global.asax的StructuremapMvc.Start()。同样的结果
  • 在控制器中添加了无参数构造函数,如下所示 它的身体:

    _总线=_总线=新容器()

    但是_-bus仍然是空的,我得到了一个与此相关的异常


请协助。

应为您自己的代码和NServiceBus配置相同的容器。下面的代码显示了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));
总线配置总线配置=新总线配置();
//配置容器,并对MVC和NServiceBus使用相同的容器
容器=新容器();
UseContainer(c=>c.ExistingContainer(container));

非常直截了当。IBus实现中需要一个无参数构造函数。如果您显示该实现的代码,我可以专门向您显示需要执行的操作。@pquest yes,如果我在控制器中放置了无prameterless构造函数,错误就会消失。但我需要控制器中的IBus实例,无论我将其作为参数还是尝试单独解析,IBus仍然无法解析。您可以看到上面的实现:ProductsController您使用的是什么版本的
StructureMap
?您安装了哪些NuGet软件包和版本?@XpyM不在您的控制器中。它在IBus中implementation@NightOwl888以下是列表:NServiceBus 5.2.14 NServiceBus.StructureMap 5.0.1 StructureMap 3.0.5.130 StructureMap.MVC4 3.0.2.115
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));