ASP.NET Core 3.1中使用C#调用SharePoint时遇System.Web.Services程序集找不到错误求助
这个错误的核心原因很明确:System.Web.Services是.NET Framework专属的传统组件,而ASP.NET Core 3.1是跨平台框架,并不包含这类仅适用于旧版.NET的程序集,所以你的代码在Core环境下找不到它就报错了。下面是具体的解决步骤和修正方案:
1. 确保使用兼容.NET Core的SharePoint CSOM包
首先要卸载你当前可能引用的旧版.NET Framework CSOM包,安装针对.NET Core优化的版本:
- 如果是连接SharePoint Online,安装
Microsoft.SharePointOnline.CSOMNuGet包:Install-Package Microsoft.SharePointOnline.CSOM -Version 16.0.24103.12000 - 如果是连接本地SharePoint Server(On-Premises),安装
Microsoft.SharePoint.CSOM包(注意选择支持.NET Core的版本,一般16.x及以上版本都兼容)。
2. 修正你的CSOM代码
虽然你原来的代码逻辑没问题,但可以做一些优化,同时确保在Core环境下能正常运行:
using Microsoft.SharePoint.Client; using System; namespace YourAspNetCoreProject { public class SharePointService { public void GetSiteDetails() { string siteUrl = "your-site-url-here"; string username = "your-username"; string password = "your-password"; using (ClientContext spContext = new ClientContext(siteUrl)) { // 针对Forms认证的设置(适用于本地SharePoint环境) spContext.AuthenticationMode = ClientAuthenticationMode.FormsAuthentication; spContext.FormsAuthenticationLoginInfo = new FormsAuthenticationLoginInfo(username, password); // 选择性加载需要的属性,减少不必要的数据请求 spContext.Load(spContext.Web, web => web.Title, web => web.ServerRelativeUrl, web => web.Lists.Include(list => list.Title)); try { spContext.ExecuteQuery(); Console.WriteLine($"站点相对URL: {spContext.Web.ServerRelativeUrl}"); Console.WriteLine($"站点标题: {spContext.Web.Title}"); } catch (Exception ex) { Console.WriteLine($"请求失败: {ex.Message}"); if (ex.InnerException != null) { Console.WriteLine($"内部错误: {ex.InnerException.Message}"); } } } } } }
3. 额外注意事项
- 如果你是连接SharePoint Online,更推荐使用OAuth认证(比如应用权限或用户的Microsoft 365凭证),Forms认证通常只用于本地SharePoint环境。
- 检查你的项目依赖,确保没有误引用任何.NET Framework专属的库(比如System.Web相关的其他组件),ASP.NET Core环境下要尽量使用Core兼容的NuGet包。
- 如果问题仍存在,可以尝试清理项目的NuGet缓存,重新安装所有依赖包。
内容的提问来源于stack exchange,提问作者RKh
相关产品推荐
相关产品推荐

