StructureMap配置无插件定义的异常代码202

时间:2009-12-28 16:47:00

标签: structuremap

我在测试时经常遇到这个错误。我甚至无法创建一个应该返回RoleRepository的IRoleRepository实例。

这是错误:

TestCase'EStudy.Repository.Tests.when_fetching_instance_of_role_repository_using_structuremap.should_fetch_successfully' 失败:StructureMap.StructureMapException:StructureMap异常代码:202 没有为PluginFamily定义的默认实例EStudy.Domain.Model.Model.EStudyModelDataContext,EStudy.Domain.Model,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null     在StructureMap.BuildSession。&lt; .ctor&gt; b__0(类型t)     在StructureMap.Util.Cache 2.get_Item(KEY key) at StructureMap.BuildSession.CreateInstance(Type pluginType) at StructureMap.Pipeline.DefaultInstance.build(Type pluginType, BuildSession session) at StructureMap.Pipeline.Instance.createRawObject(Type pluginType, BuildSession session) at StructureMap.Pipeline.Instance.Build(Type pluginType, BuildSession session) at StructureMap.Pipeline.ConfiguredInstanceBase 1.getChild(String propertyName,Type pluginType,BuildSession buildSession)     在StructureMap.Pipeline.ConfiguredInstanceBase 1.StructureMap.Pipeline.IConfiguredInstance.GetChild(String propertyName, Type pluginType, BuildSession buildSession) at EStudyDomainModelRepositoriesRoleRepositoryInstanceBuilder4c713d65fe464f4792cd90352224a54b.BuildInstance(IConfiguredInstance , BuildSession ) at StructureMap.Pipeline.ConfiguredInstanceBase 1.StructureMap.Pipeline.IConfiguredInstance.Build(类型pluginType,BuildSession会话,InstanceBuilder构建器)     在StructureMap.Pipeline.ConfiguredInstanceBase 1.build(Type pluginType, BuildSession session) at StructureMap.Pipeline.Instance.createRawObject(Type pluginType, BuildSession session) at StructureMap.Pipeline.Instance.Build(Type pluginType, BuildSession session) at StructureMap.Pipeline.BuildPolicy.Build(BuildSession buildSession, Type pluginType, Instance instance) at StructureMap.InstanceFactory.Build(BuildSession session, Instance instance) at StructureMap.BuildSession.CreateInstance(Type pluginType, Instance instance) at StructureMap.BuildSession.<.ctor>b__0(Type t) at StructureMap.Util.Cache 2.get_Item(KEY key)     在StructureMap.BuildSession.CreateInstance(Type pluginType)     在StructureMap.Container.GetInstance(Type pluginType)     在StructureMap.Container.GetInstanceT     在StructureMap.ObjectFactory.GetInstancePLUGINTYPE     C:\ Projects \ EStudyVS2008 \ EStudySoltution \ EStudy.Repository.Tests \ Test_StructureMapConfiguration.cs(41,0):at EStudy.Repository.Tests.when_fetching_instance_of_role_repository_using_structuremap.should_fetch_successfully()

 [TestFixture]
    public class when_fetching_instance_of_estudy_data_context_using_structuremap : BaseTest
    {

        [Test]
        public void should_fetch_successfully()
        {
           Assert.IsNotNull(ObjectFactory.GetInstance<EStudyModelDataContext>());
        }
    }

BaseTest: 

 public class BaseTest
    {
        private TransactionScope _scope;

        static BaseTest()
        {
            EStudy.Domain.Model.Configuration.StructureMapConfiguration.Configure(); 
        }

And here is the StructureMapConfiguration.Configure method; 

 public class StructureMapConfiguration
    {
        public static void Configure()
        { 
            ObjectFactory.Initialize(InitializeStructureMap);

            ObjectFactory.Initialize(j =>
            {
                j.ForRequestedType<EStudyModelDataContext>().TheDefault.Is.OfConcreteType<EStudyModelDataContext>().WithCtorArg("connection")
                    .EqualTo(ConfigurationManager.ConnectionStrings["EStudyTestDatabaseEntities"].ConnectionString);

            });
        }

        public static void InitializeStructureMap(IInitializationExpression x)
        {
            x.Scan(y =>
                       {
                           y.Assembly("EStudySoltution.EStudy.Domain.Model");
                           y.With<DefaultConventionScanner>();
                       }

                );





        }

1 个答案:

答案 0 :(得分:2)

你不应该多次调用Initialize()。来自文档:

“对Initialize()的任何连续调用都将有效地清除任何现有配置并有效地重启Container。”

如果要向现有容器添加其他配置,请使用Objectfactory.Configure()。

我不确定这会解释为什么你会收到你为EStudyModelDataContext看到的错误,但它肯定会看到你发布的内容至少有一个问题。

您还可以通过调用container.WhatDoIHave()(它输出可以发送到控制台的字符串)来查看您的操作和配置是否正确。