StructureMap:“没有定义插件的默认实例” - 即使它是

时间:2010-04-06 22:27:31

标签: structuremap

我已经使用StructureMap大约6个月了;你会认为它会变得更容易。它似乎没有。

这是我的注册表的第一行:

            For<IDbConnection>()
            .Singleton()
            .Use<SqlConnection>()
            .Ctor<string>(WebConfigurationManager.ConnectionStrings["UnifiedConnectionString"].ConnectionString);

它编译并运行。但是,当我尝试使用该接口时,就像这样:

            return MsSqlConfiguration.MsSql2008.ConnectionString(((DbConnection)ObjectFactory.GetInstance<IDbConnection>()).ConnectionString);

我得到了

StructureMap Exception Code:  202
No Default Instance defined for PluginFamily System.Data.IDbConnection, System.Data,
 Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089

现在我可能会遗漏一些东西(好吧,我很明显),但我不知道我可以更明确地定义IDbConnection的默认实例。我做错了什么?

编辑:按照约书亚的要求,我正在为最小的案例添加完整的代码。

我尝试创建一个可以重现错误的控制台应用。它最终没有复制它,但给出了一个不同的SM异常,我也不明白。但也许当我明白那个时,我会知道生产应用程序中发生了什么。

首先,我的控制台应用程序:

       static void Main(string[] args)
    {
        Debug.Print("Connection String: {0}", ConfigurationManager.ConnectionStrings["UnifiedConnectionString"].ConnectionString);
        (new Bootstrapper()).BootstrapStructureMap();

    }

然后,我的Bootstrapper模块从生产应用程序中取出:

    public class Bootstrapper : IBootstrapper
{
    public  void BootstrapStructureMap()
    {
        ObjectFactory.Initialize(x =>

                x.Scan(s =>
                {
                    s.WithDefaultConventions();
                    s.TheCallingAssembly();
                    s.LookForRegistries();
                }
                    )
        );
        Debug.Print(ObjectFactory.WhatDoIHave());
        try
        {
            ObjectFactory.AssertConfigurationIsValid();
        }
        catch (StructureMapConfigurationException ex)
        {
            string msg = ex.ToString();
            throw;
        }
        catch (Exception ex)
        {
            string msg = ex.ToString();
            throw;
        }
        InitAutoMapper();
    }

最后,DependencyRegistry从生产应用程序中解脱出来,但超出第一个声明的所有内容都被注释掉了。

    public class DependencyRegistry : Registry
{
    public DependencyRegistry()
    {     
        For<IDbConnection>()
            .Singleton()
            .Use<SqlConnection>()
            .Ctor<string>(ConfigurationManager.ConnectionStrings["UnifiedConnectionString"].ConnectionString);
        //For<...
    }
}

现在我运行它时得到的是它通过“WhatDoIHave()”,但它在AssertConfigurationIsValid()上抛出了一个SM 205异常。尝试实例化SqlConnection时出现错误(显然),说“缺少所请求的实例属性\”connectionString \“for InstanceKey”。

以下是信息的文字:

StructureMap.Exceptions.StructureMapConfigurationException:

在实例上构建错误'a54d3ca1-33b5-4100-82d4-13e458f57a3f'(System.Data.SqlClient.SqlConnection的配置实例,System.Data,版本= 2.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089)     for PluginType System.Data.IDbConnection,System.Data,Version = 2.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089

StructureMap.StructureMapException:StructureMap异常代码:205 缺少InstanceKey“a54d3ca1-33b5-4100-82d4-13e458f57a3f”的请求实例属性“connectionString”    在StructureMap.Pipeline.ConstructorInstance。&lt; .ctor&gt; b__0(String key)    在StructureMap.Util.Cache 2.get_Item(KEY key) at StructureMap.Pipeline.ConstructorInstance.Get(String propertyName, Type pluginType, BuildSession session) at StructureMap.Pipeline.ConstructorInstance.Get[T](String propertyName, BuildSession session) at StructureMap.Pipeline.Arguments.Get[T](String propertyName) at lambda_method(ExecutionScope , IArguments ) at StructureMap.Construction.BuilderCompiler.FuncCompiler 1.&lt;&gt; c__DisplayClass2.b__0(IArguments args)

...(希望没有相关内容删除空间)......

StructureMap失败:1个构建/配置失败和0个验证错误

在StructureMap.Container.AssertConfigurationIsValid()    在StructureMap.ObjectFactory.AssertConfigurationIsValid()    在U:\ dave \ VS2008Projects \ FMSWeb \ FMSWeb \ ConsoleIoCTest \ Bootstrapper.cs:第29行的ConsoleIoCTest.Bootstrapper.BootstrapStructureMap()中

知道了吗?我也没有。当我将它作为.ctor参数传递时,它为什么说它缺少构造函数键“connectionString”?

谢谢,抱歉占用了所有空间。

1 个答案:

答案 0 :(得分:4)

我认为你的问题是你错过了Ctor依赖指令的“部分”。

For<IDbConnection>()
  .Singleton()
  .Use<SqlConnection>()
  .Ctor<string>().Is(theConnectionString);
相关问题