使用C#与VS2022获取SSRS报表服务器报表列表求助
解决VS2022中C#连接SSRS获取报表列表的问题
代码问题修正说明
你提供的代码存在两处核心问题:
- 错误定义了
ReportingService2010Soap属性,应直接使用服务引用自动生成的EndpointConfiguration枚举值 - 未调用SSRS Web Service的
ListChildren方法来获取报表/文件夹资源
完整可行代码示例
using System; using System.Net; using YourServiceReferenceNamespace; // 替换为你添加服务引用时的实际命名空间 namespace SSRSReportFetcher { class Program { static void Main(string[] args) { // 正确的SSRS Web Service地址格式:ReportServer虚拟目录下的ReportService2010.asmx var reportServerUrl = "http://<服务器名>:<端口>/ReportServer/ReportService2010.asmx"; var credentials = new NetworkCredential("<域用户名>", "<密码>", "<域名>"); // 使用服务引用自动生成的EndpointConfiguration枚举(默认名称为ReportingService2010Soap) var endpointConfig = ReportingService2010SoapClient.EndpointConfiguration.ReportingService2010Soap; using (var client = new ReportingService2010SoapClient(endpointConfig, new EndpointAddress(reportServerUrl))) { try { // 设置Windows身份验证凭据 client.ClientCredentials.Windows.ClientCredential = credentials; client.Open(); // 遍历根目录下所有资源,第二个参数true表示递归获取子目录内容 CatalogItem[] allItems = client.ListChildren("/", true); Console.WriteLine("所有报表:"); foreach (var item in allItems) { // 仅筛选类型为"Report"的资源 if (item.TypeName.Equals("Report", StringComparison.OrdinalIgnoreCase)) { Console.WriteLine($"名称:{item.Name} | 路径:{item.Path}"); } } } catch (Exception ex) { Console.WriteLine($"获取失败:{ex.Message}"); ex.InnerException?.Message?.Let(m => Console.WriteLine($"内部错误:{m}")); } finally { // 安全关闭客户端连接 if (client.State == System.ServiceModel.CommunicationState.Opened) client.Close(); else client.Abort(); } } Console.ReadLine(); } } }
关键注意事项
- URL正确性:SSRS Web Service的标准路径是
http://<服务器>/ReportServer/ReportService2010.asmx,不要包含报表文件夹路径,遍历目录通过ListChildren的路径参数指定 - 命名空间替换:将代码中的
YourServiceReferenceNamespace替换为你添加服务引用时设置的命名空间 - 权限验证:确保使用的域账号在SSRS服务器上拥有「浏览报表」权限,可通过报表管理器确认
- 配置匹配:添加服务引用后,app.config会自动生成endpoint配置,代码中使用的
EndpointConfiguration名称需与app.config内的endpoint name一致
内容的提问来源于stack exchange,提问作者Krish Lalwani
相关产品推荐
相关产品推荐

