microsoft / ApplicationInsights-dotnet

ApplicationInsights-dotnet
MIT License
565 stars 287 forks source link

System.IO.FileLoadException: Could not load file or assembly 'System.Diagnostics.DiagnosticSource Version=4.0.4.0 #2550

Open TimothyMothra opened 2 years ago

TimothyMothra commented 2 years ago

This is a conflict between Microsoft.ApplicationInsights and Microsoft.AspNet.TelemetryCorrelation. These packages have different versions of System.Diagnostics.DiagnosticSource:

This causes the exception:

Microsoft.ApplicationInsights.Extensibility.TraceSource Error: 18 : 
TelemetryInitializer Microsoft.ApplicationInsights.Web.UserTelemetryInitializer failed to initialize telemetry item System.IO.FileLoadException: 
Could not load file or assembly 'System.Diagnostics.DiagnosticSource, Version=4.0.4.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. 
The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
File name: 'System.Diagnostics.DiagnosticSource, Version=4.0.4.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'
   at Microsoft.ApplicationInsights.Web.Implementation.RequestTrackingExtensions.CreateRequestTelemetryPrivate(HttpContext platformContext)
   at Microsoft.ApplicationInsights.Web.Implementation.RequestTrackingExtensions.ReadOrCreateRequestTelemetryPrivate(HttpContext platformContext) in /_/WEB/Src/Web/Web/Implementation/RequestTrackingExtensions.cs:line 142
   at Microsoft.ApplicationInsights.Web.Implementation.WebTelemetryInitializerBase.Initialize(ITelemetry telemetry) in /_/WEB/Src/Web/Web/Implementation/WebTelemetryInitializerBase.cs:line 36

Workaround

The workaround is to use a binding redirect:

<runtime>
  <dependentAssembly>
    <assemblyIdentity name="System.Diagnostics.DiagnosticSource" publicKeyToken="cc7b13ffcd2ddd51"  culture="neutral"/>
    <bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="5.0.0.0"/>
  </dependentAssembly>
</runtime>
daniel-sousa-lobo commented 2 years ago

The redirect is not working.

github-actions[bot] commented 1 year ago

This issue is stale because it has been open 300 days with no activity. Remove stale label or this will be closed in 7 days. Commenting will instruct the bot to automatically remove the label.

MagnusEikemo commented 1 year ago

I also have the same issue, working on a legacy .NET 4.X MVC Projectm the error suddenly just started to pop up. I've tried adding the Assembly manually as instructed, but the redirect doesn't seem to work. However when I upgrade the version of the Diagnostics to 7.0.2 it stops complaining about the versions but still says it can't load the assembly. Seems like a bug.... I've fixed other broken assemblies the same way but System.Diagnostics is rather stubborn.

MagnusEikemo commented 1 year ago

The error is fixable, however this was due to adding Azure.Core library into this old legacy project, which is not entirely compatible. Thus we will have to move away from the library itself and the error is no longer an issue.

I understand this answer does not suffice for everyone, so what I did to force the redirect through was to actually install the NuGetPackage onto the MVC Project itself. So both manually adding the assembly to Web.Config as well as installing the correct versions from Nuget.

For my use case 5.0.0 and 6.0.0 seemed to be functional versions of the System.Diagnostics.DiagnosticSource library.

nelson9494 commented 1 year ago

I'm encountering a similar problem, but with a more recent version. I receive an error stating "System.Diagnostics.DiagnosticSource, Version=6.0.0.0," although Azure.Core utilizes version 6.0.0.1. When I downgrade my NuGet package from 6.0.0.1 to 6.0.0.0, I encounter a different error: "Could not load file or assembly 'System.Diagnostics.DiagnosticSource, Version=4.0.4.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'." Attempting to switch from version 6.0.0.0 to 4.0.4.0 results in a compatibility issue with "Azure.Core" and "Azure.Storage.Blobs," which do not support System.Diagnostics.DiagnosticSource, Version=4.0.4.0. Despite having consistent references to version 6.0.0.1 in both my web.config and the app.config of my new DLL, the error persists.

=== Pre-bind state information === LOG: DisplayName = System.Diagnostics.DiagnosticSource, Version=6.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51 (Fully-specified) LOG: Appbase = file:///D:/System/testsystem/ LOG: Initial PrivatePath = D:\System\testsystem\bin Calling assembly : Azure.Storage.Blobs, Version=12.19.1.0, Culture=neutral, PublicKeyToken=92742159e12e44c8. LOG: This bind starts in default load context. LOG: Using application configuration file: D:\System\testsystem\web.config LOG: Using host configuration file: C:\Windows\Microsoft.NET\Framework64\v4.0.30319\aspnet.config LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework64\v4.0.30319\config\machine.config. LOG: Post-policy reference: System.Diagnostics.DiagnosticSource, Version=6.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51 LOG: Attempting download of new URL file:///C:/Windows/Microsoft.NET/Framework64/v4.0.30319/Temporary ASP.NET Files/root/4a4892e4/1a9eb44/System.Diagnostics.DiagnosticSource.DLL. WRN: Comparing the assembly name resulted in the mismatch: Revision Number ERR: Failed to complete setup of assembly (hr = 0x80131040). Probing terminated. DATE/TIME: 11/21/2023 10:53:57 PM

nelson9494 commented 12 months ago

I discovered the solution: The issue was that the <dependentAssembly> tags in the web.config were not being recognized, preventing the new dll from functioning correctly. This was due to the <configuration> tag containing namespace attributes, which resulted in the <assemblyBinding> tag being overlooked.

Incorrect configuration: <configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">

Correct configuration: <configuration>

After correcting this, the assembly classes were read properly, allowing me to direct the system to the correct dlls.

tiaan-lg commented 1 month ago

I was faced with the same issue on a .net 4.8 project and updating the binding redirect manually to this

<dependentAssembly> <assemblyIdentity name="System.Diagnostics.DiagnosticSource" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-8.0.0.0" newVersion="8.0.0.0" /> </dependentAssembly>

Targeting 8.0.0.0 directly solved the issue for me