结构图使用配置文件(版本2.6)

时间:2013-10-16 14:36:42

标签: structuremap

            ObjectFactory.Initialize(x =>
            {
                x.Scan(scan =>
                {
                    scan.Assembly("CloudAssembly");  // defines profile "Cloud"
                    scan.LookForRegistries();
                });

                x.Profile("Local", cfg =>
                {
                    cfg.For<ICloudStorage>().
                        Use(() =>
                            new LocalStorage(HttpContext.Current.Server.MapPath("~")));

                });
           });

然后我尝试将其设置为“Local”配置文件,以便ICloudStorage解析为LocalStorage。

        ObjectFactory.Container.SetDefaultsToProfile("Local");
        ObjectFactory.Profile = "Local";

然后在激活依赖于ICloudStorage的对象时会出现此异常:

StructureMap Exception Code:  202
No Default Instance defined for PluginFamily ICloudStorage, AssemblyName, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null

我是否对配置文件做错了什么?我查看了whatDoIHave的输出,看起来它配置正确。

以下是“whatDoIHave”的相关部分:

ICloudStorage (MyLibrary.ICloudStorage)                         Default Instance for Profile Local          Instance is created by Func<object> function:  System.Func`2[StructureMap.IContext,MyLibrary.ICloudStorage]                                                                                                                         
Scoped as:  Transient

                                                                                  Default Instance for Profile Local          Instance is created by Func<object> function:  System.Func`2[StructureMap.IContext,MyLibrary.ICloudStorage]                                                                                                                         
                                                                                  Default Instance for Profile Cloud     Configured Instance of CloudProviders.CloudStorage, CloudProviders, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null  

1 个答案:

答案 0 :(得分:0)

我只是删除了ObjectFactory.Container.SetDefaultsToProfile(“Local”),我在Initialize方法之后直接调用它而不是使用配置文件......

  <StructureMap DefaultProfile="Local">

  </StructureMap>

它现在正常运作......