HttpModule sharing between multiple sub-applications without GAC

I have a website setup in IIS with one web.config installer at the root for many virtual sub-applications (about 35), some of which use their own unique overrides / configurations.

I have two HttpModules that I would like to implement for all sub-applications at once. Without the use of the GAC, is there a way to specify the implementation in web.config so that these modules can be applied to all sub-applications without recompiling the module code into each sub-application? Can I save the HttpModule assembly somewhere in the site structure that it can use with all sub-applications?

+6
source share
3 answers

You can register the http module in the root folder of web.config ( system.webServer/modules ). The module must have a strong name (must be signed):

 <add name="MyModule" preCondition="managedHandler" type="MyModule.Namespace, MyModuleAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=bfd166351ed997df" /> 

IIS now expects the module to be in the bin directory (or in the GAC). The dependentAssembly section tells IIS where the assembly file can be found:

  <runtime> <assemblyBinding appliesTo="v2.0.50727" xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="MyModuleAssembly" publicKeyToken="bfd166351ed997df"/> <codeBase version="1.0.0.0" href="file://c:/SharedLibs/MyModuleAssembly.dll" /> </dependentAssembly> </assemblyBinding> </runtime> 
+4
source

Assuming you added it to the web config and bin folder after the fact does not work for you, there are several possibilities in the current structure:

If you do not mind changing the web configurations, but you need to save the module outside the cell , you can use System.Web.PreApplicationStartMethod to register the AppDomain.AssemblyResolve event handler, then load the event handler and return a type

If you do not want to moderate web.config or you want your events to be on the top of the stack , as you could get GACing and change the web configuration at the machine level, you can use System.Web.PreApplicationStartMethod to get the code to run at startup, just being in the bin directory, and then use Microsoft.Web.Infrastructure.DynamicModuleHelper.DynamicModuleUtility.RegisterModule from the MVC Razor libraries. This gives you the same result as the last module in the machine module list.

If you do not want to change the web configuration and must be earlier in the event stack , you need to do something to reorder the event handlers. I needed to do this once to try to debug something that swallowed the errors. Having plunged a little into the reflector, and I came up with this function to get existing event handlers

 Dim t As Type = target.[GetType]() Public Function GetEventSubscribers(ByVal target As Object, ByVal eventName As String) As [Delegate]() Dim w = CType(t.GetField("_events", BindingFlags.Instance Or BindingFlags.Static Or BindingFlags.NonPublic).GetValue(target), System.ComponentModel.EventHandlerList) Dim k = t.GetFields(BindingFlags.[Static] Or BindingFlags.Instance Or BindingFlags.NonPublic).Where(Function(x) x.Name.StartsWith("Event" & eventName)).Select(Function(x) x.GetValue(target)).ToList() Dim d() As [Delegate] = k.SelectMany(Function(x) If w(x) Is Nothing Then New [Delegate]() {} Else Return w(x).GetInvocationList() End If End Function).ToArray Return d End Function 

If you pass it an HttpApplication instance named EventName, you will get all registered delegates of the handler, which allows you to call RemoveEventHandler () for each of them.

If you do this with an error event, add your own handler, and then add the existing delegates again in the correct order, after which your handler will fire first, before any of the other handlers have a chance to break an even state and the rest of the application does not seem to more wise if you yourself do not change the state of the event.

+2
source

You can add http modules in dll. Put the DLL in the bin folder of your applications and access the http module from web.config

  <add name="MyHttpModule" type="MyDll.MyHttpModule, MyDll" /> 
+1
source

Source: https://habr.com/ru/post/917473/


All Articles