Skip to content

ANCM In process: Managed exceptions before full initialization are too hard to look at #5153

@davidfowl

Description

@davidfowl

Epic #8833

While testing out the generic host with the new ValidateOnBuild feature (which fails to build the host if there's an issue with services), I noticed that its really hard (even in development) to just get access to the managed exception when using ANCM in proc. If there's an exception in Program.cs, this is what you see:

image

Then the event log shows this:

image

This is a pretty bad experience for looking at a simple managed exception. Here are the steps we advertise to troubleshoot:

Troubleshooting steps:
- Check the system event log for error messages
- Enable logging the application process' stdout messages
- Attach a debugger to the application process and inspect
  • The event log tells me there's a manged exception, but nothing more.
  • Enabling stdout logging means I suddenly need a web.config file. Do we have a tooling gesture to turn this on? When I add a web.config it gives me this:
    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
    
      <!-- To customize the asp.net core module uncomment and edit the following section. 
      For more info see https://go.microsoft.com/fwlink/?linkid=838655 -->
      <!--
      <system.webServer>
        <handlers>
          <remove name="aspNetCore"/>
          <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified"/>
        </handlers>
        <aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" />
      </system.webServer>
      -->
    
    </configuration>
    Which I am assuming isn't correct for inproc hosting.
  • The default debugger settings don't break when this exception occurs. The output window does show this obscure message though:
    Exception thrown: 'System.InvalidOperationException' in Microsoft.Extensions.DependencyInjection.dll
    Exception thrown: 'System.InvalidOperationException' in Microsoft.Extensions.DependencyInjection.dll
    Exception thrown: 'System.AggregateException' in Microsoft.Extensions.DependencyInjection.dll
    
  • Changing the debugger settings to break on all exceptions does work.

image

We need to improve this. Here are some things I can think of:

  • We should re-think replacing the built in exception handler for .NET or finding a way to call into it when an unhandled exception happens. It logs the exception information to the event log with all of the details. Here's an example:
    image
  • We should show an error page with the exception in development.
  • We could potentially make our own managed entry point that we call into first in order to set things up such that we can get unhandled exceptions and handle them appropriately. Something like the following:
public static void HookExceptionHandler()
{
    AppDomain.CurrentDomain.UnhandledException += (sender, e) =>
    {
        if (e.IsTerminating)
        {
            // Log to the event log here
        }
    };
}

We would call this delegate first (it would exist in the IIS integration assembly) so that before we called into main, we'd have logic in managed code to hook up the unhandled exception event handler.

Metadata

Metadata

Assignees

Labels

DoneThis issue has been fixedarea-networkingIncludes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractionscost: LWill take from 5 - 10 days to completefeature-iisIncludes: IIS, ANCM

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions