初始化Azure LogicManagementClient报数组类型不兼容错误如何解决
初始化LogicManagementClient触发ArrayTypeMismatchException异常解决
问题描述
使用LogicManagementClient检索Azure逻辑应用操作执行历史时,执行客户端初始化代码触发报错:Attempted to access an element as a type incompatible with the array.
触发报错的代码:
LogicManagementClient client = new LogicManagementClient(credentials) { SubscriptionId = "****" };
凭据对象可正常创建,调试时可正常读取配置值,凭据创建代码如下:
var credentials = SdkContext.AzureCredentialsFactory .FromServicePrincipal("clientid", "clientsecret", "tenantid", AzureEnvironment.AzureGlobalCloud);
报错堆栈信息:
System.ArrayTypeMismatchException HResult=0x80131503 Message=Attempted to access an element as a type incompatible with the array. Source=mscorlib StackTrace: at System.Collections.Generic.List`1.Add(T item) at Microsoft.Azure.Management.Logic.LogicManagementClient.Initialize() at Microsoft.Azure.Management.Logic.LogicManagementClient..ctor(ServiceClientCredentials credentials, DelegatingHandler[] handlers)
根因
该异常由NuGet包版本不兼容触发:当前项目引用的Microsoft.Azure.Management.Logic包,和项目内已有的Microsoft.Rest.ClientRuntime、Azure Fluent管理类依赖包版本不匹配。LogicManagementClient初始化阶段会向内置HTTP处理链列表添加DelegatingHandler实例,版本不匹配时会出现类型定义不一致,最终触发数组类型不匹配异常。
解决步骤
- 卸载项目内所有相关旧版NuGet包,包括
Microsoft.Azure.Management.Logic、Microsoft.Azure.Management.ResourceManager.Fluent、Microsoft.Rest.ClientRuntime、Microsoft.Rest.ClientRuntime.Azure,避免旧版本程序集残留引用。 - 重新安装官方验证过的兼容版本包组合:
- 安装
Microsoft.Azure.Management.Logic4.1.0及以上稳定版 - 匹配安装
Microsoft.Rest.ClientRuntime2.3.21及以上版本、Microsoft.Rest.ClientRuntime.Azure3.3.19及以上版本 - 若继续使用Fluent SDK创建凭据,
Microsoft.Azure.Management.ResourceManager.Fluent请安装1.38.0及以上稳定版,禁止和新版Logic管理包混用1.30以下的旧版Fluent包
- 安装
- 清理解决方案、删除bin/obj目录下的生成文件后重新生成项目,再执行客户端初始化代码。
可选优化方案:如果不需要依赖Fluent SDK的其他能力,可以移除Fluent包引用,直接使用
Microsoft.Rest.ClientRuntime.Azure.Authentication包提供的方法创建服务主体凭据,从根源上规避Fluent包和Logic管理包的版本冲突,凭据创建示例代码:var credentials = ApplicationTokenProvider.LoginSilentAsync( "tenantid", new ClientCredential("clientid", "clientsecret"), AzureEnvironment.AzureGlobalCloud).Result;
- 验证:客户端初始化无异常后,即可正常调用
WorkflowRuns.List、WorkflowRunActions.List等接口查询逻辑应用执行历史。
内容的提问来源于stack exchange,提问作者mitesh patel
相关产品推荐
相关产品推荐

