Type resolution error during ASP.NET precompilatio

2019-05-28 17:27发布

问题:

During ASP.NET precompilation of our .NET 3.5 web application, various initialization is performed in type initializers. One of the type initializers throws a custom exception when the environment is incorrectly configured. However, when our custom exception is thrown, here is what the aspnet_compiler.exe tells us:

[exec] error ASPRUNTIME: Type is not resolved for member 'App.Project.CustomException,App.Project, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.
[exec]
[exec] [SerializationException]: Type is not resolved for member 'App.Project.CustomException,App.Project, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.
[exec]    at System.Web.Compilation.BuildManagerHost.PrecompileApp(ClientBuildManagerCallback callback)
[exec]    at System.Web.Compilation.ClientBuildManager.PrecompileApplication(ClientBuildManagerCallback callback, Boolean forceCleanBuild)
[exec]    at System.Web.Compilation.ClientBuildManager.PrecompileApplication(ClientBuildManagerCallback callback)
[exec]    at System.Web.Compilation.Precompiler.Main(String[] args)

Notice that no "real" stack trace information is included. However, when I change our code to throw an InvalidOperationException (instead of our custom exception), the stack trace is included correctly. (As an aside, our CustomException type is annotated with [Serializable]. When we remove the [Serializable] annotation, the aspnet_compiler.exe complains differently -- that our custom exception is not marked as such.)

Does anyone know why throwing a custom exception during ASP.NET precompilation is causing the secondary SerializationException? Why is it trying to serialize the exception? Similarly, why does using a BCL InvalidOperationException not cause the secondary SerializationException?

Could this be because the aspnet_compiler.exe is trying to do some sort of reflection on unexpected exceptions? (And therefore since it does not have our App.Project assembly loaded, it is unable to resolve the type?)

回答1:

It appears that the aspnet_compiler.exe program uses .NET Remoting to communicate with the ASP.NET runtime. When the ASP.NET runtime throws my custom exception, the aspnet_compiler.exe is unable to deserialize it because it cannot resolve the type of the custom exception because my App.Project assembly is not in the GAC nor available via path search to aspnet_compiler.exe.

To test this hypothesis, I copied the App.Project assembly into the same directory as aspnet_compiler.exe (C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727) and performed the precompilation. When the custom exception is thrown, it comes out with the normal stack trace I would expect and the SerializationException does not occur.