简体   繁体   English

“没有为此对象定义无参数的构造函数。”对于使用结构映射的控制器中的IBus

[英]“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为我创建了一些文件:

NuGet为StructureMap创建的新文件

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: 我已经尝试了什么:

  • Removing [assembly: ...] from StructuremapMvc.cs and calling StructuremapMvc.Start() from Global.asax. 从StructuremapMvc.cs中删除[assembly:...],然后从Global.asax中调用StructuremapMvc.Start()。 Same result. 结果相同。
  • 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));

More information 更多信息

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Unity:没有为此对象定义无参数构造函数。 - Unity : No parameterless constructor defined for this object. 没有为此对象定义无参数构造函数。 MVC 5 - No parameterless constructor defined for this object. MVC 5 ASP Net Core MVC创建实例控制器System.MissingMethodException:&#39;为此对象未定义无参数构造函数。 - asp net core mvc create instance controller System.MissingMethodException: 'No parameterless constructor defined for this object.' 没有为此对象定义无参数构造函数。 当我尝试使用 Unity 依赖时 - No parameterless constructor defined for this object. while i try for Unity Dependency CBO.FillCollection抛出“没有为此对象定义的无参数构造函数。”错误 - CBO.FillCollection throwing “No parameterless constructor defined for this object.” error 获取“未为此对象定义无参数的构造函数。”发布视图模型时出错 - Getting “No parameterless constructor defined for this object.” Error while posting a viewmodel 错误“未为此对象定义无参数的构造函数。” ViewModel中的SelectList错误 - Error “No parameterless constructor defined for this object.” with SelectList from ViewModel System.MissingMethodException:没有为此对象定义的无参数构造函数。 MVC4 - System.MissingMethodException: No parameterless constructor defined for this object. MVC4 没有为此对象定义无参数构造函数 - No parameterless constructor defined for this object 错误:未为此对象定义无参数构造函数 - Error: No parameterless constructor defined for this object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM