How can I hook up the ASP.NET compilation process?

ASP.NET Dynamically generates classes and compiles assembly for temporary ASP.NET files.

I would like to be able to receive information when this process occurs. Ultimately, I would like to have an event that will fire the name of the source file and the generated class name + assembly so that I can map between the methods in the original source file and the methods in the generated classes.

I would be grateful for your help.

+8
c # asp.net-mvc iis-7
source share
2 answers

I would recommend using the FileSystemWatcher class to create an observer for the respective directories, and then perform processing based on this.

You can find information about this here: https://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher(v=vs.110).aspx

In essence, the observer will allow you to receive events for changing files in the directory, from which you can then use the Assembly class to load and process through the reflection information of the generated code. I can’t say that it will be easy, but it is very doable. You will also want to have a database for tracking changes, converting the source to compiled code, etc., to make it more reliable.

Hope this indicates that you are in the right direction.

+1
source share

Developing an offer from @Inari

Just make sure you look at the AppDomain.CurrentDomain.DynamicDirectory folder with IncludeSubdirectories set to true. To be sure that you are not too late to track all the compilations, you need to run as the first, so I suggest you use PreApplicationStartMethodAttribute .

This helps in getting information when this process occurs. If you also want to find the source files, it depends on what interests you (only compiled assemblies? => Reflection, also compiled razor pages => by name).

[assembly: PreApplicationStartMethod(typeof(Demo.CompilationWatcher), "Initialize")] namespace Demo { public static class CompilationWatcher { public static void Initialize() { while (true) { FileSystemWatcher watcher = new FileSystemWatcher(); watcher.Path = AppDomain.CurrentDomain.DynamicDirectory; watcher.IncludeSubdirectories = true; watcher.NotifyFilter = NotifyFilters.Attributes | NotifyFilters.CreationTime | NotifyFilters.DirectoryName | NotifyFilters.FileName | NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.Security | NotifyFilters.Size; watcher.Filter = "*.*"; // consider filtering only *.dll, *.compiled etc watcher.Changed += new FileSystemEventHandler(OnChanged); watcher.Created += new FileSystemEventHandler(OnChanged); watcher.EnableRaisingEvents = true; } } public static void OnChanged(object source, FileSystemEventArgs e) { // your thread-safe logic here to log e.Name, e.FullPath, an d get the source file through reflection / name etc. ... } } } 
+1
source share

All Articles