如何强制将特定程序集加载到 MVC 2 应用程序的每个应用程序域中?
我有一些松散耦合的代码,这些代码依赖于加载到当前应用程序域中的特定程序集:
Assembly assembly = AppDomain.CurrentDomain.GetAssemblies().Where(
candidateAssembly => candidateAssembly.FullName.StartsWith("Microsoft.WindowsAzure.ServiceRuntime")).SingleOrDefault();
并且该代码在 MVC 2 应用程序内部执行并产生 null
引用,因为该程序集未加载到当前应用程序域中。应用领域。我可以只从该程序集中调用一些代码,但这会引入应用程序代码对该程序集的强烈依赖性,并且我不再具有松散耦合。
我需要在代码运行之前强制 IIS(或其他任何东西)将该程序集加载到应用程序域中。我尝试编写 IHttpModule
的实现并将其列在 web.config 中,但没有帮助。
到目前为止,我尝试在
下添加程序集,看起来它可以工作,但我不确定它是否可靠。
如何强制将该特定程序集加载到应用程序域中而不在代码中引入依赖项?
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();
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.
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.
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.
How do I force loading that specific assembly into the application domain without introducing a dependency in my code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是错误的方法,可能会导致严重的定时错误。
该设置会影响 ASP.NET 编译,因此仅在编译 ASP.NET 视图时强制加载程序集,并且最多编译一次 - 无论是在第一次请求视图时还是在就地预编译过程中(如果设置了后者) 。不管怎样,之后会有一段时间不需要编译 ASP.NET 视图。
现在,IIS 应用程序池每 29 小时自动回收一次。当池回收时,它会启动一个新的工作进程,并且该进程开始托管站点负载。重要的是 ASP.NET 视图在此过程中不会更改,因此不需要重新编译它们,因此不会调用编译过程,因此
< 中列出的程序集;程序集>
不会被强制加载。所以这个东西看起来可以工作,直到 29 小时过去,然后就崩溃了。
需要更好的解决方案。类似于
中的设置,说明 Azure 运行时是否应该可用。如果设置已设置,则代码可以首次调用 Azure 运行时程序集中的代码,这将使它们加载。如果未设置该设置,则不会尝试调用该代码。This is the wrong way and it can cause serious timed bugs.
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.
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.So the thing looks working until 29 hours pass and then it just falls apart.
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.