在C#控制台应用中渲染SSRS;在哪里可以找到SSRS ReportExecutionService?

时间:2018-10-09 16:16:09

标签: c# reporting-services service render

在C#控制台应用中,我试图在报告服务器上呈现SSRS报告,然后将其流式传输为PDF文件。 我找到了很多文档,但是遇到了一个障碍:如何添加对报表执行服务的引用?

我能够按照代码示例中的第一步进行操作……我添加了一个服务引用/单击了http://ServerName/ReportServer_XXX/reportservice2010.asmx中的“添加Web引用”, (通过远程处理到服务器并运行RS Config Manager,我得到了“ ReportServer_XXX”)。 然后,我可以执行以下操作: var rs = new ReportingService.ReportingService2010(); rs.Credentials = System.Net.CredentialCache.DefaultCredentials; 等等

下一步是类似地向ReportExecutionService添加Web引用,该引用具有.Render()方法,可将PDF实际上输出为字节数组。 但是,当我输入网址时: http://ServerName/ReportServer_XXX/reportexecutionservice2010.asmx
(或…2005.asmx”),它的标题为 ServerName / ReportServer_XXX-/ 然后在我们的SSRS网站中的文件夹(章节,财务,市场营销等)中。 “添加引用”按钮显示为灰色。

我在做什么笨蛋? 谢谢!

2 个答案:

答案 0 :(得分:2)

另一种方法是创建 WebRequest。我已经创建了这个函数:

  public static byte[] GenerateReport(string ReportServer, string Username, string Password, string Domain, string ReportPath, string Format, string Parameters)
  {

      ReportPath = ReportPath.Replace("/", "%2f").Replace(" ", "+");
      string URL = ReportServer + "?" + ReportPath + $"&rs:Command=Render&rs:Format=" + Format + "&rs:ParameterLanguage=en-GB" + "&" + Parameters;
      WebRequest Req = WebRequest.Create(URL);
      Req.Credentials = new NetworkCredential(Username, Password, Domain);
      Req.Timeout = Convert.ToInt32(ConfigurationManager.AppSettings["SSRSTimeout"]);
      Stream WebStream = Req.GetResponse().GetResponseStream();
      MemoryStream MemStream = new MemoryStream();
      WebStream.CopyTo(MemStream);
      long Len = MemStream.Length;
      byte[] ReportReturn = new byte[Len];
      MemStream.Seek(0, SeekOrigin.Begin);
      MemStream.Read(ReportReturn, 0, (int)Len);
      WebStream.Close();
      MemStream.Close();
      MemStream.Dispose();
      return ReportReturn;
  }

使用:

  string Domain = ConfigurationManager.AppSettings["SSRSDomain"];
  string Username = ConfigurationManager.AppSettings["SSRSUser"];
  string Password = ConfigurationManager.AppSettings["SSRSPass"];
  string ReportServer = ConfigurationManager.AppSettings["SSRSServer"];

  string ReportPath = "/Report Path/ReportName";
  string Params = "ParamName=ParamValue";

  byte[] ReportData = GenerateReport(ReportServer, Username, Password, Domain, ReportPath, "PDF", Params);

  File.WriteAllBytes(AppDomain.CurrentDomain.BaseDirectory + @"\" + "savedfilename.pdf", ReportData);

答案 1 :(得分:1)

您根本没有做任何愚蠢的事情。但是,您可能需要将该URL更改为类似的内容。

ReportExecutionService rexec = new ReportExecutionService();
rexec.Credentials = System.Net.CredentialCache.DefaultCredentials;
rexec.Url = "http://<your report `enter code here`
    server>/reportserver/ReportExecution2005.asmx?";

我知道这是违反直觉的。我正在使用SSRS 2017和VS 2017,但仍为“ 2005”。

    // The following is SSRS boiler-plate from Microsoft
string encoding = null;
Warning[] warnings = null;
string[] streamIDs = null;
ExecutionInfo execInfo = new ExecutionInfo();
ExecutionHeader execHeader = new ExecutionHeader();
rexec.ExecutionHeaderValue = execHeader;
execInfo = rexec.LoadReport(reportPath, historyID);
rexec.SetExecutionParameters(parameters, "en-us");
Console.WriteLine("SessionID: {0}", rexec.ExecutionHeaderValue.ExecutionID);
try
{
     Console.WriteLine(reportName.ToString());
     result = rexec.Render(format, devInfo, out string extension, out string 
        mimeType, out encoding, out warnings, out streamIDs);
execInfo = rexec.GetExecutionInfo(); Console.WriteLine("Execution date and time: 
        {0}", execInfo.ExecutionDateTime);
}

catch (SoapException e)
{
    Console.WriteLine(e.Detail.OuterXml);
    Dts.Events.FireError(0, "Error ",e.Message+ "\r" + e.StackTrace, String.Empty,0);
    Dts.TaskResult = (int)ScriptResults.Failure;
 }