.NET Framework 4.6.1 WinForms应用访问Azure存储账户遇异常求助
问题描述
我有一个基于.NET Framework 4.6.1的遗留WinForms应用,需添加上传文件至Azure存储账户的功能。尝试引入Azure.Storage.Blobs NuGet包后出现缺失DLL的问题,调用以下代码时抛出异常:
Public Function StreamFileFromAzureStorageAcct(ByVal clientLocalFilePath As String, ByVal containerDestName As String, ByRef strReturn As String) As StreamReader 'Dim strReturn As String = String.Empty Dim textInStream As Stream = Nothing Dim textIn As StreamReader = Nothing Dim connectionString As String = Nothing Try connectionString = String.Format("DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1};EndpointSuffix={2}", modConfigSettings.AzureStorageAcctName, modConfigSettings.AzureStorageAcctKey, modConfigSettings.AzureStorageAcctURL) Dim Container As BlobContainerClient = New BlobContainerClient(connectionString, containerDestName) Dim BlobClient As BlobClient = Container.GetBlobClient(Path.GetFileName(clientLocalFilePath)) Dim readOptions As BlobOpenReadOptions = New BlobOpenReadOptions(False) readOptions.Position = 0 ''Azure Storage Acct Read call returns a Stream textInStream = BlobClient.OpenRead(readOptions) '' Pass the Stream to a StreamReader to stay inline with existing code. textIn = New StreamReader(textInStream) strReturn = "Success" Return textIn Catch er As Exception strReturn = String.Format("Failed to Stream from Storage Account: {0}-File:{1}-connectionString: {2}-{3}", er.Message, Path.GetFileName(clientLocalFilePath), connectionString, er.StackTrace) End Try Return Nothing End Function
异常信息:
Failed to Stream from Storage Account: The type initializer for 'PerTypeValues`1' threw an exception.-
异常堆栈:
at System.SpanHelpers.IsReferenceOrContainsReferencesT at Azure.Storage.Blobs.BlobRestClient.get_ResponseClassifier200() at Azure.Storage.Blobs.BlobRestClient.CreateGetPropertiesRequest(String snapshot, String versionId, Nullable`1 timeout, String leaseId, String encryptionKey, String encryptionKeySha256, String encryptionAlgorithm, String ifTags, RequestConditions requestConditions, RequestContext context) at Azure.Storage.Blobs.BlobRestClient.GetProperties(String snapshot, String versionId, Nullable`1 timeout, String leaseId, String encryptionKey, String encryptionKeySha256, String encryptionAlgorithm, String ifTags, RequestConditions requestConditions, RequestContext context) at Azure.Storage.Blobs.Specialized.BlobBaseClient.d__124.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Azure.Storage.Blobs.Specialized.BlobBaseClient.d__98.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Azure.Storage.Blobs.Specialized.BlobBaseClient.OpenRead(BlobOpenReadOptions options, CancellationToken cancellationToken) at DBAccessObjects.RemFredDataAdmin.StreamFileFromAzureStorageAcct(String clientLocalFilePath, String containerDestName, String& strReturn) in C:\Users\csale\source\Workspaces\Application Server\FredServer\DBAccessObjects\RemFredDataAdmin.vb:line 15904
请问我使用的NuGet包是否正确?该如何解决此异常问题?
解决方案
1. 调整NuGet包版本
你使用的Azure.Storage.Blobs是Azure存储官方SDK,但最新版本已不再支持.NET Framework 4.6.1,仅适配.NET 6+等现代框架。需安装标注支持.NET Framework 4.6.1的旧版本,比如12.14.0或更低版本(可在NuGet包管理器的版本筛选列表中选择对应版本)。
2. 补充缺失的依赖包
.NET Framework 4.6.1原生缺少Azure SDK运行所需的部分API,需手动安装以下兼容的NuGet包:
System.Memory(推荐版本4.5.4)System.Buffers(推荐版本4.5.1)System.Numerics.Vectors(推荐版本4.5.0)
这些包会为.NET Framework 4.6.1补充缺失的类型和方法,解决PerTypeValues1`类型初始化失败的核心问题。
3. 代码细节优化
- 检查连接字符串格式:
EndpointSuffix参数应为core.windows.net(或对应区域的存储后缀),不要直接填写完整的存储账户URL。 - 同步/异步方法适配:旧版本的
Azure.Storage.Blobs提供同步的OpenRead重载,确保你调用的是适配.NET Framework 4.6.1的同步方法;若需切换为异步,需将方法改为Async修饰并使用Await关键字,避免线程阻塞引发异常。
内容的提问来源于stack exchange,提问作者user2413934
相关产品推荐
相关产品推荐

