c# - 创建 BlobClient 会导致发布配置中的 System.Diagnostics.DiagnosticSource Version=4.0.4.0 FileNotFoundException

标签 c# .net azure azure-blob-storage

我有一个使用最新 Visual Studio 2022 的混合解决方案( native C++、C++/CLI、c#、.NET)。我的解决方案的目标框架版本为 4.7.2。我有两个 C# 类库项目,每个项目都具有从 Azure Blob 存储上传或下载文件的功能。因此,这两个项目都使用 Azure.Storage.Blobs NuGet 包最新版本 12.16.0。

当我在 Debug模式下运行程序时,一切正常。但在发布配置中,当其中一个函数想要创建用于上传/下载的 BlobClient 时,我收到以下异常:

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.

根据 NuGet.org,Azure.Storage.Blobs->Azure.Storage.Common 12.15.0->Azure.Core (>= 1.31.0)->System.Diagnostics.DiagnosticSource (>= 的依赖关系树4.6.0) 系统内存 (>= 4.5.5)-> 系统缓冲区 (>= 4.5.1)。 我为这两个项目安装了所有软件包的最新稳定版本,包括 System.Diagnostics.DiagnosticsSource NuGet 软件包(7.0.2)。 7.0.2 System.Diagnostics.DiagnosticsSource 包包含运行时版本 4.0.30319。

导致此异常的原因以及为什么要查找 System.Diagnostics.DiagnosticSource Version=4.0.4.0?

代码如下:

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

最佳答案

谢谢,阿达利亚特。事实证明,即使设置了“复制本地”,有问题的 DLL (System.Diagnostics.DiagnosticSource) 也没有被复制到输出文件夹。我应该立即意识到这一点,但对系统一直在寻找 v4.0.4.0 而不是两个项目中引用的 v7.0.0.2(包括 app.config、packages.config、等)事实证明,这是使用 VS 或 MSBuild 构建时的常见问题(根据许多帖子。) 发生的情况是,有一个主要的启动项目 A,以及 A 所依赖的另外两个项目 B 和 C。 B和C都调用X.dll。 X.dll 依赖于 Y.dll,但是,尽管 B 和 C 项目中都专门引用了 Y.dll,但构建过程为了保持整洁,不会将 Y.dll 复制到输出文件夹。解决此问题的一种方法(我就是这样做的)是添加对项目 A 的引用。然后它肯定会被复制到输出文件夹。

这是一个相当常见的问题,解决方案也更复杂。例如,参见c# - Dependent DLL is not getting copied to the build output folder in Visual Studio - Stack Overflow .

关于c# - 创建 BlobClient 会导致发布配置中的 System.Diagnostics.DiagnosticSource Version=4.0.4.0 FileNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76317490/

相关文章:

c# - Release模式符号文件 .dll.mdb 中的 Fody 异常与程序集 .dll 不匹配

c# - 如何在本地测试 Azure 队列触发器函数?

c# - 如何从服务访问当前登录用户的 HKCU 注册表?

c# - .NET 字节而不是字符的正则表达式

Azure 应用服务 : Accessing logs from all scaled machines

azure - Powershell (7.2.7) Foreach-Object -Parallel - 多线程问题

azure - 使用电子邮件搜索现有用户时,AAD 请求 https ://graph. windows.net 方法 GET 因为请求正文格式错误

c# - TryUpdateModelAsync() 失败但没有说明原因

c# - WPF 数据网格 : Clear column sorting

.net - Azure 中的 session 超时