You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ASP.NET Core 3.1中使用C#调用SharePoint时遇System.Web.Services程序集找不到错误求助

解决ASP.NET Core 3.1中SharePoint CSOM加载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.CSOM NuGet包:
    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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 23:08:10