Unity错误:在初始化应用程序后无法注册模块

时间:2017-08-02 15:30:11

标签: asp.net-mvc asp.net-mvc-5 unity-container

我有asp.net mvc应用程序,我使用Unity作为IoC容器。 UnityWebActivator类具有PreApplicationStartMethod,它使用Unity容器注册所有类型。

我想注册一个类的单例实例,其属性值来自数据库。

[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(Web.App_Start.UnityWebActivator), "Start")]
[assembly: WebActivatorEx.ApplicationShutdownMethod(typeof(Web.App_Start.UnityWebActivator), "Shutdown")]
namespace Web.App_Start
{

    public static class UnityWebActivator
    {

        public static void Start()
        {
            var container = UnityConfig.GetConfiguredContainer();

            FilterProviders.Providers.Remove(FilterProviders.Providers.OfType<FilterAttributeFilterProvider>().First());
            FilterProviders.Providers.Add(new UnityFilterAttributeFilterProvider(container));

            DependencyResolver.SetResolver(new UnityDependencyResolver(container));

            Microsoft.Web.Infrastructure.DynamicModuleHelper.DynamicModuleUtility.RegisterModule(typeof(UnityPerRequestHttpModule));

            // Web API
            //GlobalConfiguration.Configuration.DependencyResolver = new Unity.WebApi.UnityDependencyResolver(container);
        }
    }
}

和UnityConfig.cs我注册了所有类型

public class UnityConfig
{
    #region Unity Container
    private static Lazy<IUnityContainer> container = new Lazy<IUnityContainer>(() =>
    {
        var container = new UnityContainer();
        RegisterTypes(container);
        return container;
    });

    public static IUnityContainer GetConfiguredContainer()
    {
        return container.Value;
    }

    public static void RegisterTypes(IUnityContainer container)
    {                
        container.RegisterType<DbContext, MyEntities>(new PerRequestLifetimeManager(), new InjectionFactory(x => new MyEntities()));                   

        //register singleton
        container.RegisterInstance<ApplicationSettings>(LoadApplicationSettings(container), new ContainerControlledLifetimeManager());
    }

    private static ApplicationSettings LoadApplicationSettings(IUnityContainer container)
    {
        var appSettings = new ApplicationSettings();
        using (var dbContext = new MyEntities())
        { 
            var settings = dbContext.Settings.ToList();             
            // populate appsettings from settings               
        }
    }
}

上面的LoadApplicationSettings方法需要从数据库中获取数据并创建ApplicationSettings类的实例。

然而,LoadApplicationSettings被调用为PreApplicationStartMethod,这是application_start方法之前,因为DbContext尚未映射但我收到错误

  

InvalidOperationException:无法确定应用程序上下文。   无法解析ASP.NET应用程序路径。

and:   
  

InvalidOperationException:在此期间无法调用此方法   应用程序的启动初始化阶段。

根据建议here,我做了两件事:

1&GT;删除行[assembly:WebActivator.PreApplicationStartMethod]
2 - ;在全局asax的UnityWebActivator.Start()事件中添加对Application_Start()方法的调用。

但是,这样做会在注册UnityPerRequestHttpModule

时导致另一个错误
    public static void Start()
    {
        // removed code for brevity

        // get error at line below
        Microsoft.Web.Infrastructure.DynamicModuleHelper.DynamicModuleUtility.RegisterModule(typeof(UnityPerRequestHttpModule));

        //removed code for brevity
    }
  

&#34;应用程序出现后无法注册模块   。初始化&#34;}

1 个答案:

答案 0 :(得分:0)

您可以使用ApplicationSettings注册InjectionConstructor。这将推迟创建(以及从数据库加载设置),直到第一次从容器中解析它们。据推测,这应该是在应用程序完成引导之后。

以下是新注册:

//register singleton
container.RegisterType<ApplicationSettings>(
    new ContainerControlledLifetimeManager(), 
    new InjectionFactory(c => LoadApplicationSettings(c)));