注入带有StructureMap的连接字符串不起作用

本文关键字:连接 字符串 不起作用 StructureMap 注入 | 更新日期: 2023-09-27 18:14:50

我仍然是非常新的结构图,所以我不明白为什么这是不工作。我正在向存储库注入"连接字符串",并且我一直从结构映射中获得以下错误:

StructureMap Exception Code: 205缺少InstanceKey "a04b4f71-4171-4e9f-b98d-170fc9ee005f"的请求实例属性"purchaseOrdersFilePath"

附带说明,连接字符串在引号中,因为我正在使用linq to xml,所以"连接字符串"实际上是一个文件的路径。我添加这个只是为了以防它可能与这个问题有关。

我的代码如下:
public class PurchaseOrderRepository : IPurchaseOrderRepository
{
    private readonly string PurchaseOrdersFilePath;
    public PurchaseOrderRepository(string purchaseOrdersFilePath)
    {
        if (string.IsNullOrWhiteSpace(purchaseOrdersFilePath)) throw new ArgumentNullException("purchaseOrdersFilePath");
        PurchaseOrdersFilePath = purchaseOrdersFilePath;
    }
 }

在我的全局。我有以下配置语句:

private void RegisterControllerFactory()
{
    var ioc = new Container();
    var controllerFactory = new IocControllerFactory(ioc);
    ControllerBuilder.Current.SetControllerFactory(controllerFactory);
    ioc.Configure(r => r.Scan(x =>
    {
        x.AssemblyContainingType<HomeController>();
        x.AddAllTypesOf<IController>();
        x.Include(t => typeof(IController).IsAssignableFrom(t));
    }));
    ioc.Configure(r => r
        .ForConcreteType<PurchaseOrderRepository>()
        .Configure.Ctor<string>().Is(@"C:'Users'sromero'Documents'Visual Studio 2010'Projects'DIDemo'SupportFiles'POS.xml"));
}

我做错了什么?

谢谢你的帮助。

注入带有StructureMap的连接字符串不起作用

事实证明,我配置了相同的组件两次(我没有反映在问题中的示例代码),所以我所做的是:

ioc.Configure(r => r
                .For<IPurchaseOrderRepository>()
                .Use<PurchaseOrderRepository>());
    ioc.Configure(r => r
            .ForConcreteType<PurchaseOrderRepository>()
            .Configure.Ctor<string>().Is(@"C:'Users'sromero'Documents'Visual Studio 2010'Projects'DIDemo'SupportFiles'POS.xml"));

我应该做的是:

ioc.Configure(r => r
                .For<IPurchaseOrderRepository>()
                .Use<PurchaseOrderRepository>()
                .Ctor<string>().Is(@"C:'Users'sromero'Documents'Visual Studio 2010'Projects'DIDemo'SupportFiles'POS.xml"));

尝试:

.Ctor<string>("purchaseOrdersFilePath").Is(@"C:'Users'sromero'Documents'Visual Studio 2010'Projects'DIDemo'SupportFiles'POS.xml"));