0

I have a hybrid solution (Native C++, C++/CLI, c#, .NET) using up-to-date Visual Studio 2022. My solution targets framework version 4.7.2. I have two c# class library projects each of which has functions that upload or download files from Azure blob storage. As such, both projects use the Azure.Storage.Blobs NuGet package latest version 12.16.0.

When I run the program in debug mode everything works fine. But in Release config, when one of the functions that want to create a BlobClient for upload/download, I get the following exception:

System.IO.FileNotFoundException: 'Could not load file or assembly 'System.Diagnostics.DiagnosticSource, Version=4.0.4.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The system cannot find the file specified.

According to NuGet.org, the dependency tree for Azure.Storage.Blobs->Azure.Storage.Common 12.15.0->Azure.Core (>= 1.31.0)->System.Diagnostics.DiagnosticSource (>= 4.6.0)System.Memory (>= 4.5.5)->System.Buffers (>= 4.5.1). I have the latest stable version of all packages installed for both projects including System.Diagnostics.DiagnosticsSource NuGet package (7.0.2). The 7.0.2 System.Diagnostics.DiagnosticsSource package contains runtime version 4.0.30319.

What is causing this exception and why would it be looking for System.Diagnostics.DiagnosticSource Version=4.0.4.0?

Here's the code:

    public bool UploadFileToAzureBlobStorage(string Path, string containerName, string BlobName)
    {
        string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
        EnvironmentVariableTarget target;
        if (userName == @"NT AUTHORITY\SYSTEM")
            target = EnvironmentVariableTarget.Machine;
        else
            target = EnvironmentVariableTarget.User;
        string connectionString = Environment.GetEnvironmentVariable("HOROSCOPE_STORAGE_ACCT_CONNECTION_STRING", target);
        //string containerName = "chart-reports";
        string blobName = BlobName;
        string filePath = Path;
    
        BlobContainerClient container = null;
        BlobClient blob = null;
        try
        {
            container = new BlobContainerClient(connectionString, containerName);
            blob = container.GetBlobClient(blobName);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
            return false;
        }
        // Upload local file
        try
        {
            // set content-type from https://github.com/Azure/azure-sdk-for-net/issues/9823
            blob.Upload(filePath, new BlobHttpHeaders {ContentType = "text/html"},
                conditions: null); // "conditions: null" means overwrite
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
            return false;
        }
        return true;
    }
Adalyat Nazirov
  • 1,611
  • 2
  • 14
  • 28
Paul.B
  • 31
  • 6
  • can you check your other dependencies and projects in solutions and their references to `System.Diagnostics.DiagnosticSource`. It's possible that output of your other project just overrides the right one. In this case you just need to consolidate dependencies – Adalyat Nazirov May 23 '23 at 18:51

1 Answers1

0

Thanks, Adalyat. Turns out that the DLL in question (System.Diagnostics.DiagnosticSource) was not being copied to the output folder even though Copy Local was set. I should have realized this right away but was confused by the fact that the system kept looking for v4.0.4.0 instead of the v7.0.0.2 that was referenced in the two projects (including the app.config, packages.config, etc.) This turns out to be a common problem when building with VS or MSBuild (according to many posts.) What happens is that there is a main, startup project, A, and two other projects, B and C, on which A depends. Both B and C call X.dll. X.dll depends on Y.dll but, even though Y.dll is specifically refenced in both B and C projects, the build process, in trying to be tidy, does not copy Y.dll to the output folder. One way to fix this (the way I did it) is to add the reference to project A. Then it for sure gets copied to the output folder.

This is a fairly common problem with more complex solutions. See, for example, c# - Dependent DLL is not getting copied to the build output folder in Visual Studio - Stack Overflow.

Paul.B
  • 31
  • 6