使用ObjectFactory中的Configuration Manager构建地图

时间:2014-01-10 09:43:33

标签: c# asp.net-mvc-4 inversion-of-control

我在MVC4项目中使用结构图,我正在尝试在ObjectFactory中使用Configuration Manager:

public static IContainer Initialize() {
        ObjectFactory.Initialize(x =>
                    {

                        x.Scan(scan =>
                                {
                                    scan.TheCallingAssembly();
                                    scan.WithDefaultConventions();
                                });

                        var someConfiguration = ConfigurationManager.GetSection("someconfiguration") as SomeConfigurationSection;


                    });
        return ObjectFactory.Container;
    }

我有错误:

Description: An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify your configuration file appropriately. Parser Error Message: An error occurred creating the configuration section handler for someconfiguration: This method cannot be called during the application's pre-start initialization phase.

我是否可能无法在对象工厂中使用Configuration Manager?如果没有,有什么方法可以读取依赖注入purpouses的web.config?

P.S。 如果不使用ObjcetFactory,ConfigurationManager就能完美运行。

1 个答案:

答案 0 :(得分:1)

我只是自己找到了问题的答案。

结构图正在App_Start中创建文件,正在初始化Factory

的特殊类
 [assembly: WebActivator.PreApplicationStartMethod(typeof(Web.UI.App_Start.StructuremapMvc), "Start")]
namespace Web.UI.App_Start

如果只是将Start方法中的所有内容复制/粘贴到Global.asax并且问题不再是问题