Search code examples
c#azure.net-4.8system.diagnosticsazure-form-recognizer

Could not load file or assembly 'System.Diagnostics.DiagnosticSource, Version=6.0.0.0 for Azure.AI.FormRecognizer


The project framework is .NET 4.8. I installed Azure.AI.FormRecognizer 4.1.0, which requires minimum Azure.Core 1.34.0 which requires System.Diagnostic.DiagnosticSource 6.0.1. But for some reason the FormRecognizer asks for 6.0.0.0. Am I missing something obvious?

This is the output:

Exception thrown: 'System.IO.FileNotFoundException' in Azure.AI.FormRecognizer.dll
An exception of type 'System.IO.FileNotFoundException' occurred in Azure.AI.FormRecognizer.dll but was not handled in user code
Could not load file or assembly 'System.Diagnostics.DiagnosticSource, Version=6.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The system cannot find the file specified.

In the app.config it gets generated as

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

And I tried to change it to 6.0.1.0 but that didn't do anything.

This is what the error looks like during runtime:

     private Uri _uri;
     private AzureKeyCredential _credential;
     private DocumentAnalysisClient _analysisClient;
     private DocumentModelAdministrationClient _modelClient;

     public AzureOcr()
     {
         _uri = new Uri(_diEndpoint);
         _credential = new AzureKeyCredential(_diKey);
         _analysisClient = new DocumentAnalysisClient(_uri, _credential); // here it throws "System.IO.FileNotFoundException: 'Could not load file or assembly 'System.Diagnostics.DiagnosticSource, Version=6.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The system cannot find the file specified.'"
         _modelClient = new DocumentModelAdministrationClient(_uri, _credential);
     }

Solution

  • In the main class library there was an older version of DiagnosticSource 5.0.0.0, I changed that to 6.0.0.1 and it was resolved. Unfortunately very project specific.

    I just added a bindingredirect of the NuGet Package in the main class library's app.config (without installing the package directly in the class library).

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