使用StructureMap

时间:2016-06-24 06:49:29

标签: c# structuremap

我的任务是使用IoC容器显式解析SqlConnection实例,并在运行时传入connectionString参数(不是注册时间)。我想出了以下测试类:

[TestFixture]
public class StructureMapTests
{
    public IContainer Container { get; set; }
    [SetUp]
    public void SetUp()
    {
        Container = new Container(_ =>
        {
            _.For<IDbConnection>().Add<SqlConnection>().Named("MyDefault").Ctor<string>("connectionString");
        });
    }

    [Test]
    public void RegistrationUsingSM()
    {
        const string connectionString =
                "Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;";
        var connection = Container.With(x =>
        {
            x.With("connectionString").EqualTo(connectionString);

        }).GetInstance<IDbConnection>("MyDefault");
        Assert.IsInstanceOf<SqlConnection>(connection);
        Assert.AreEqual(connectionString, connection.ConnectionString);
    }
}

调用GetInstance时,测试会抛出StructureMapBuildPlanException。消息是:

无法为具体类型System.Data.SqlClient.SqlCredential创建构建计划

我不知道为什么在此异常中引用了SqlCredential。我在解析IDbConnection时明确指的是命名实例“MyDefault”。我已注册IDbConnection以显式使用仅包含connectionString参数的SqlConnection构造函数。 SqlCredential与此有什么关系?为什么不解决?下面是堆栈跟踪:

  

在C:\ BuildAgent \ work \ a395dbde6b793293 \ src \ StructureMap.Shared \ Building \ BuildPlan.cs中的StructureMap.Building.BuildPlan.Build(IBuildSession会话,IContext上下文):第152行      在C:\ BuildAgent \ work \ a395dbde6b793293 \ src \ StructureMap.Shared \ BuildSession.cs中的StructureMap.BuildSession.BuildNewInSession(输入pluginType,Instance实例):第113行      在C:\ BuildAgent \ work \ a395dbde6b793293 \ src \ StructureMap.Shared \ Pipeline \ NulloTransientCache.cs中的StructureMap.Pipeline.NulloTransientCache.Get(输入pluginType,Instance instance,IBuildSession session):第29行      在StructureMap.BuildSession.ResolveFromLifecycle(Type pluginType,Instance instance)中的C:\ BuildAgent \ work \ a395dbde6b793293 \ src \ StructureMap.Shared \ BuildSession.cs:第102行      在StructureMap.SessionCache.GetObject(类型为pluginType,Instance instance,ILifecycle lifecycle)中的C:\ BuildAgent \ work \ a395dbde6b793293 \ src \ StructureMap.Shared \ SessionCache.cs:第93行      在C:\ BuildAgent \ work \ a395dbde6b793293 \ src \ StructureMap.Shared \ BuildSession.cs中的StructureMap.BuildSession.FindObject(Type pluginType,Instance instance):第223行      在StructureMap.Container.BuildInstanceWithArgs(Type pluginType,Instance defaultInstance,ExplicitArguments args,String requestedName)在C:\ BuildAgent \ work \ a395dbde6b793293 \ src \ StructureMap.Shared \ Container.cs:第1162行      在StructureMap.Container.DoGetInstance(Type pluginType,ExplicitArguments args,String instanceKey)中的C:\ BuildAgent \ work \ a395dbde6b793293 \ src \ StructureMap.Shared \ Container.cs:第278行      在C:\ BuildAgent \ work \ a395dbde6b793293 \ src \ StructureMap.Shared \ Container.cs中的StructureMap.Container.GetInstance [T](ExplicitArguments args,String instanceKey):第204行      在C:\ BuildAgent \ work \ a395dbde6b793293 \ src \ StructureMap.Shared \ ExplicitArgsExpression.cs中的StructureMap.ExplicitArgsExpression.GetInstance [T](字符串名称):第118行      at Scaffolding.Test.StructureMapTests.RegistrationUsingSM()在C:\ Users \ garre \ documents \ visual studio 2015 \ Projects \ Scaffolding.Test \ Scaffolding.Test \ StructureMapTests.cs:第29行

编辑:我能够通过在GetInstance方法中传递null SqlCredential对象来解决此问题:

var connection = Container.With<string>(connectionString).With<SqlCredential>(null).GetInstance<IDbConnection>();

显然,默认构造函数是ctor(string,SqlCredential),但我仍然无法将ctor(string)指定为要使用的构造函数。

0 个答案:

没有答案