如何强制加载特定的程序集到一个MVC 2应用程序的每个应用程序域? [英] How do I force loading of a specific assembly into every application domain of an MVC 2 application?

查看:136
本文介绍了如何强制加载特定的程序集到一个MVC 2应用程序的每个应用程序域?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些松耦合code,它依赖于一个特定的组件被加载到当前应用程序域:

I have some loosely coupled code that depends on a specific assembly being loaded into the current application domain:

Assembly assembly = AppDomain.CurrentDomain.GetAssemblies().Where(
    candidateAssembly => candidateAssembly.FullName.StartsWith("Microsoft.WindowsAzure.ServiceRuntime")).SingleOrDefault();

和code是MVC 2应用和收益率引用内部执行,因为该程序集未加载到当前应用程序域。我可以打电话从组装约code,但会介绍关于该组件的应用程序code很强的依赖性,我不再有松散耦合。

and that code is executed inside an MVC 2 application and yields null reference because that assembly is not loaded into the current application domain. I could just call some code from that assembly, but that would introduce strong dependency of the application code on that assembly and I no longer have loose coupling.

我需要强制IIS(或者其他)我的code运行前加载该程序集到应用领域。我试着写 IHttpModule的的实现,并列出它在web.config中,但它并不能帮助。

I need to force IIS (or whatever else) to load that assembly into the application domain before my code runs. I tried to write an implementation of IHttpModule and list it in web.config, but it doesn't help.

到目前为止,我想下添加组件<的System.Web><编译><组件> ,看起来像它的工作原理,但我不确定它是否是可靠的。

So far I tried to add the assembly under <system.web><compilation><assemblies> and looks like it works, but I'm not sure whether it's reliable.

如何强制加载特定组件到应用程序域,而无需引入依赖于我的code?

How do I force loading that specific assembly into the application domain without introducing a dependency in my code?

推荐答案

这是错误的方式,它可能会导致严重的定时错误。

This is the wrong way and it can cause serious timed bugs.

的设置影响ASP.NET编译,因此它只力加载组件时ASP.NET次被编译和被编译为至多一次 - 无论是在第一次请求的视图或在就地precompilation过程中,如果是后者的设置。反正会有片刻的时间ofter这是任何ASP.NET视图需要编译。

The setting affects ASP.NET compilation, so it only forces loading the assembly when ASP.NET views are compiled and they are compiled at most once - either on first request to the view or during the in-place precompilation process if the latter is setup. Anyway there will be a moment of time ofter which no ASP.NET view needs to be compiled.

现在有IIS程序池自动回收每29小时。当池回收启动一个新的工作进程,进程开始托管网站的有效载荷。什么是重要的是,ASP.NET的观点在此过程中不会改变,所以他们不需要重新编译,因此编译过程不调用等等组件的中列的System.Web&GT;&LT;编译&GT;&LT;组件&GT; 不会强行加载

Now there's IIS apppool automatic recycle every 29 hours. When the pool recycles it starts a new worker process and that process starts hosting the site payload. What's important is that ASP.NET views don't change during this process and so they need not be recompiled and so the compilation process is not invoked and so the assemblies listed in <system.web><compilation><assemblies> are not forcibly loaded.

所以东西看起来工作,直到29小时通过,那么它只是分崩离析。

So the thing looks working until 29 hours pass and then it just falls apart.

有一个更好的解决方案是必要的。这样的事情在℃的环境;的appSettings&GT; 说的Azure运行时是否可用。如果设置被设置,code,那么可以对内部的Azure运行时组件code第一个呼叫,这将让他们加载。如果没有设置它不会尝试调用code。

A better solution is needed. Something like a setting in <appSettings> saying whether Azure Runtime should be available. If the setting is set, the code then can make a first call to code inside Azure Runtime assemblies and this will make them load. if the setting is not set it doesn't try to call that code.

这篇关于如何强制加载特定的程序集到一个MVC 2应用程序的每个应用程序域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆