Why is ASP.NET failing to recognize the derivation of an Exception?

Viewed 141

We are maintaining a Kentico 11-based site (.NET Framework 4.6.1 running on IIS 10/Windows Server 2019). In testing code that throws an Exception derived directly from System.Exception, the system throws HttpCompileException with this message:

C:\<path>\B2CTokenProcessor.ascx.cs(28): error CS0155: The type caught or thrown must be derived from System.Exception

This is B2CTokenProcessor.ascx.cs line 28:

catch (IdTokenException x)

and this is the definition of IdTokenException:

public class IdTokenException : System.Exception
{
    public IdTokenException(string message) : base(message) { }
}

In a situation this simple, an error like this is entirely baffling to me. If the definition of IdTokenException were missing or unreachable, I'd expect to see a type could not be found message (though, in fact, when I substitute an undefined name in that catch no error is logged by the CMS or in the Event Log). The only thought I have at all is that IdTokenException is defined in a NuGet package that was imported into the project; this Exception is not raised on the development site, but happens on a test site to which the site project and NuGet package DLLs have been deployed.

Why is .NET not seeing this type as being derived from System.Exception?

1 Answers

I still don't understand why this manifested in the test environment and not in the development environment which is running the same .NET environment, but what solved this problem in this case was creating and installing a new version of the NuGet package that targets the same framework version as Kentico (Framework 4.6.1 as noted in the question), rather than targeting .NET Standard 2.0 as it was originally built. I suppose there is something on that dev box that includes the .NET Standard 2.0 framework definitions.

Related