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;
}