如何为 SSRS 报表服务编写 C# 包装器类






4.33/5 (5投票s)
介绍如何为 SSRS 报表服务构建 C# 包装器类
引言
我最近有一个需求,需要从 MVC 6 应用程序生成 SSRS 报告。这看起来像是一个微不足道,很容易完成的任务,但最终变成了从初级开发人员传递到初级开发人员,没有任何真正成功或进展的工作。最初的代码审查显示,尝试通过执行 HTTP POST 并将所需参数传递给报告服务器来请求 SSRS 报告,但结果是 401 HTTP 响应。
请注意:这种方法可能适用于某些报告服务器配置。
然而,Microsoft 确实提供了一个生成 SSRS 报告的端点,使用此端点应被视为从 SSRS 外部的应用程序生成报告的正确方法。
为什么没有使用 SSRS 报告服务?
当我进行自己的研究时,我发现有关此主题的可用文章很容易让初级开发人员感到困惑,原因如下:
- SSRS 提供了两个服务端点,每个端点都有自己的用途。使用这两个端点中的哪一个来生成报告?
- 年轻一代的开发人员习惯于 RESTFUL Web 服务,很少有人了解较旧的 Web 服务技术(如 SOAP)。
SSRS 报告服务端点
生成 SSRS 报告的端点是
[Your report server host name]/ReportServer/ReportExecution2005.asmx
另请注意,SSRS 提供了第二个端点来执行其他与报告服务器相关的任务
[Your report server host name]/ReportServer/ReportExecution2010.asmx
构建 SSRS 报告服务包装类
为了使用 SSRS 提供的报告服务端点,我们必须添加一个 Web 引用。
非常重要:新的 ASP.NET 5/Core 项目类型不提供添加 Web 引用的功能。唯一的解决办法是创建一个 .NET 4.5.1 类库项目,该项目将编译成一个 .dll 文件,仍然可以被你的 ASP.NET 5/Core 应用程序导入。
点击“文件”>“新建”>“项目”菜单
选择“类库”项目模板,并将您的项目命名为“SsrsWrapper”
右键单击“引用”,然后单击“添加服务引用”菜单项
非常重要:我最初添加了一个服务引用,但它不起作用,请确保按照我下面概述的步骤添加 Web 引用。
单击“高级...”按钮
单击“添加 Web 引用...”按钮
输入 SSRS 报告服务端点,然后单击箭头按钮
输入报告服务器的用户名和密码,然后单击“确定”按钮
将 Web 引用命名为“SsrsReportService”,然后单击“添加引用”按钮
请注意:身份验证模式弹出了多次,提示我输入用户名和密码,只需输入一次凭据,然后单击“取消”按钮即可。
在“解决方案资源管理器”中,您将注意到一个新的“Web 引用”文件夹
编写 ReportManager 类
我们现在有一个可用于生成报告的端点。让我们编写一个 ReportManager
类
将 Class1.cs 重命名为 ReportManager.cs
将以下代码粘贴到 ReportManager
类中,以创建一个带有两个 Render
方法的管理器类,一个将报告作为字节数组返回,另一个将物理文件保存到磁盘。
using System.IO; using System.Net; using SsrsWrapper.SsrsReportService; namespace SsrsWrapper { public class ReportManager { private readonly ReportExecutionService _reportServerExecutionService; public ReportManager ( string reportServerWsdlUrl, string username, string password, string domain) { _reportServerExecutionService = new ReportExecutionService { Url = reportServerWsdlUrl, Credentials = new NetworkCredential(username, password, domain) }; } public byte[] Render ( string reportDirectory, string reportName, string reportFormat, ParameterValue[] parameters ) { _reportServerExecutionService.ExecutionHeaderValue = new ExecutionHeader(); _reportServerExecutionService.SetExecutionParameters(parameters, "en-us"); string encoding; string mimeType; string extension; Warning[] warnings; string[] streamIds; var result = _reportServerExecutionService.Render(reportFormat, @"<DeviceInfo><Toolbar>False</Toolbar></DeviceInfo>", out extension, out encoding, out mimeType, out warnings, out streamIds); return result; } public void Render ( string reportDirectory, string reportName, string reportFormat, ParameterValue[] parameters, string destinationPath ) { var result = Render(reportDirectory, reportName, reportFormat, parameters); var stream = File.Create(destinationPath, result.Length); stream.Write(result, 0, result.Length); stream.Close(); } } }
如何使用 ReportManager 类
现在我们有了一个为提供的 SSRS 报告服务端点提供的包装类,我们可以使用它从各种 C# 应用程序生成 SSRS 报告,例如:
- 桌面应用程序
- 控制台应用程序
- Windows 服务应用程序
- Web 应用程序,例如经典 ASP、MVC 和 Web API
创建 ReportManager
类的新实例,并调用其中一个 Render
方法
var reportManager = new ReportManager ( [The report server host name], [The report server username], [The report server password], [The domain] ); reportManager.Render ( [The directory the report was published to on the server], "My Report", "PDF", [The report parameters], "c:\Test\MyReport.pdf" );
如何在用户应用程序中配置添加的 Web 引用
请记住,如果不提供作为 Web 引用添加的 SSRS 报告服务端点的 XML 配置,则无法引用和使用包含 ReportManager 的 .dll 文件。对于非 ASP.NET 5/Core 应用程序,将 SsrsWrapper.Properties.Settings
配置添加到 app\web.config 文件。
对于 ASP.NET 5/Core 应用程序,在项目的根文件夹中创建一个 ReportManagerConfig.xml 文件,其中包含与上图中看到的完全相同的 XML 配置,并通过添加以下代码行在 Startup.cs 类的 Configure
方法中导入该文件
XmlConfigurator.Configure(new FileInfo(Path.Combine(env.ApplicationBasePath, "ReportManagerConfig.xml")));