Commit c0dad86
authored
Fix TypeLoadException exception error (#8157)
## Summary of changes
Fixes `TypeLoadException`[ being logged as an
error](https://app.datadoghq.com/error-tracking?query=service%3Ainstrumentation-telemetry-data%20%40lib_language%3Adotnet%20version%3A3.3%2A&et-side=activity&fromUser=false&issue_states=open&order=total_count&refresh_mode=sliding&source=all&sp=%5B%7B%22p%22%3A%7B%22issueId%22%3A%224cdd44b0-aea9-11f0-940c-da7ad0900002%22%7D%2C%22i%22%3A%22error-tracking-issue%22%7D%5D&from_ts=1769594245972&to_ts=1770199045972&live=true)
during tracer initialization on .NET Framework when
`System.Web.Hosting.HostingEnvironment` is unavailable.
## Reason for change
On .NET Framework 4.x, users were seeing this error during startup:
```
Error: Unable to get application name through ASP.NET settings
System.TypeLoadException
at Datadog.Trace.PlatformHelpers.ApplicationNameHelpers.TryLoadAspNetSiteName(String& siteName)
at Datadog.Trace.PlatformHelpers.ApplicationNameHelpers.GetApplicationName(TracerSettings settings)
```
## Root Cause
The existing code had a `try-catch` block inside `TryLoadAspNetSiteName`
to catch `TypeLoadException`, but the exception was thrown **during JIT
compilation** of the method, not during execution. When the JIT compiler
encountered `System.Web.Hosting.HostingEnvironment` references, it threw
`TypeLoadException` before any code in the method could execute - so the
inner catch block was never reached.
## Implementation details
Extracted the `System.Web`-dependent code into a separate method marked
with `[MethodImpl(MethodImplOptions.NoInlining)]`. This:
1. Defers JIT compilation of the System.Web-dependent code until the
method is actually called
2. Allows the `TypeLoadException` to be caught at the call site
3. Logs a **Warning** (expected behavior) instead of an **Error**
A similar pattern is already used in
`SecurityCoordinator.Framework.cs:243-244`.
## Test coverage
## Other details
<!-- Fixes #{issue} -->
<!-- 1 parent 57f90c1 commit c0dad86
1 file changed
Lines changed: 29 additions & 8 deletions
Lines changed: 29 additions & 8 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| 11 | + | |
11 | 12 | | |
12 | 13 | | |
13 | 14 | | |
| |||
70 | 71 | | |
71 | 72 | | |
72 | 73 | | |
73 | | - | |
74 | | - | |
75 | | - | |
76 | | - | |
77 | | - | |
78 | | - | |
79 | | - | |
80 | | - | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
81 | 78 | | |
82 | 79 | | |
83 | 80 | | |
| |||
87 | 84 | | |
88 | 85 | | |
89 | 86 | | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
90 | 111 | | |
0 commit comments