.NET7中Dataverse ServiceClient执行FetchXML返回空并抛System.Web异常
问题解答
1. FetchXML语法与替代数据获取方式
你的FetchXML语法是正确的,结构完全符合Dataverse的FetchXML规范。但GetEntityDataByFetchSearch属于旧版兼容API,在.NET Core/.NET 5+环境下存在兼容性问题,推荐使用Dataverse Client官方适配现代框架的API:
方式一:使用RetrieveMultipleRequest
using var serviceClient = new ServiceClient(connectionString); if (serviceClient.IsReady) { var fetchXml = @"<fetch top='5'><entity name='account'><attribute name='name' /></entity></fetch>"; var fetchExpression = new FetchExpression(fetchXml); var retrieveRequest = new RetrieveMultipleRequest { Query = fetchExpression }; var retrieveResponse = (RetrieveMultipleResponse)serviceClient.Execute(retrieveRequest); var accounts = retrieveResponse.EntityCollection.Entities; // 处理查询结果 foreach (var account in accounts) { Console.WriteLine(account.GetAttributeValue<string>("name")); } }
方式二:使用LINQ查询
using var serviceClient = new ServiceClient(connectionString); if (serviceClient.IsReady) { using var context = new OrganizationServiceContext(serviceClient); var accounts = context.CreateQuery("account") .Select(a => new { Name = a.GetAttributeValue<string>("name") }) .Take(5) .ToList(); // 处理查询结果 foreach (var account in accounts) { Console.WriteLine(account.Name); } }
2. 兼容性错误修复
你遇到的System.Web.PreApplicationStartMethodAttribute找不到的错误,本质是GetEntityDataByFetchSearch方法依赖.NET Framework专属的System.Web程序集,而.NET7是跨平台框架,不包含该程序集,无法通过添加引用解决。修复方案如下:
- 替换API:彻底弃用
GetEntityDataByFetchSearch,改用上面推荐的RetrieveMultipleRequest或LINQ查询方式,这些API专为.NET Core/.NET 5+设计,无兼容性问题。 - 确认NuGet包版本:1.1.32版本的
Microsoft.PowerPlatform.Dataverse.Client支持.NET7,但建议升级到最新稳定版,规避已知的兼容性bug。 - 停止无效操作:不要尝试添加
System.Web引用,.NET7中不存在该程序集,强行添加只会引发更多冲突。
内容的提问来源于stack exchange,提问作者ScottSto
相关产品推荐
相关产品推荐

