开发者

How do I force loading of a specific assembly into every application domain of an MVC 2 application?

开发者 https://www.devze.com 2023-04-12 18:27 出处:网络
I have some loosely coupled code that depends on a specific assembly being loaded into the current application domain:

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 => candida开发者_StackOverflow中文版teAssembly.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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消