Skip to content

Instantly share code, notes, and snippets.

@paulodiogo
Last active July 21, 2020 20:06
Show Gist options
  • Save paulodiogo/ca9f9af2d309f3ce07ae74baa3d3b79d to your computer and use it in GitHub Desktop.
Save paulodiogo/ca9f9af2d309f3ce07ae74baa3d3b79d to your computer and use it in GitHub Desktop.
Using SSRS 2016
[Serializable]
public enum Format
{
XML,
CSV,
PDF,
EXCELOPENXML,
WORDOPENXML
}
public class Report
{
public Report(string extension, string mimeType, byte[] results)
{
this.Extension = extension;
this.MimeType = mimeType;
this.Results = results;
}
public string Extension { get; private set; }
public string MimeType { get; private set; }
public byte[] Results { get; private set; }
}
//Project > References > Add Service Reference > http://<URL>/<APP_SSRS>/ReportExecution2005.asmx
//Add the SOAP Execution client
public class GenerateReportService
{
private readonly SSRSEX.ReportExecutionServiceSoapClient clientEX;
//NetworkCredential => CredentialCache.DefaultNetworkCredentials or new System.Net.NetworkCredential("user", "pass", "domain")
public GenerateReportService(System.Net.NetworkCredential clientCredentials)
{
clientEX = new SSRSEX.ReportExecutionServiceSoapClient();
clientEX.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;
clientEX.ClientCredentials.Windows.ClientCredential = clientCredentials;
}
public Report Render(string reportName, IDictionary<string, string> parametros, Format format)
{
string mimeType = string.Empty;
string extension = string.Empty;
string historyID = null;
string deviceInfo = null;
byte[] results;
string encoding = string.Empty;
SSRSEX.Warning[] warnings = null;
string[] streamIDs = null;
clientEX.Open();
SSRSEX.ServerInfoHeader serverInfoHeader = null;
SSRSEX.ExecutionInfo executionInfo = null;
SSRSEX.TrustedUserHeader trustedUserHeader = new SSRSEX.TrustedUserHeader();
clientEX.LoadReport(trustedUserHeader, reportName, historyID, out serverInfoHeader, out executionInfo);
SSRSEX.ParameterValue[] parameters = new SSRSEX.ParameterValue[1];
List<ParameterValue> reportParameters = new List<ParameterValue>();
foreach (var item in parameters)
{
reportParameters.Add(new ParameterValue
{
Name = item.Key,
Value = item.Value
});
}
SSRSEX.ExecutionHeader execHeader = new SSRSEX.ExecutionHeader();
execHeader.ExecutionID = executionInfo.ExecutionID;
clientEX.SetExecutionParameters(execHeader, trustedUserHeader, reportParameters.ToArray(), "pt-BR", out executionInfo);
clientEX.Render(execHeader, trustedUserHeader, format.ToString(), deviceInfo,
out results, out extension, out mimeType, out encoding, out warnings, out streamIDs);
return new Report(extension, mimeType, results);
}
public async System.Threading.Tasks.Task<Report> RenderAsync(string reportName, IDictionary<string, string> parameters, Format format)
{
string historyID = null;
string deviceInfo = null;
string encoding = string.Empty;
clientEX.Open();
SSRSEX.ServerInfoHeader serverInfoHeader = null;
SSRSEX.ExecutionInfo executionInfo = null;
SSRSEX.TrustedUserHeader trustedUserHeader = new SSRSEX.TrustedUserHeader();
clientEX.LoadReport(trustedUserHeader, reportName, historyID, out serverInfoHeader, out executionInfo);
SSRSEX.ParameterValue[] parameters = new SSRSEX.ParameterValue[1];
List<ParameterValue> reportParameters = new List<ParameterValue>();
foreach (var item in parameters)
{
reportParameters.Add(new ParameterValue
{
Name = item.Key,
Value = item.Value
});
}
SSRSEX.ExecutionHeader execHeader = new SSRSEX.ExecutionHeader();
execHeader.ExecutionID = executionInfo.ExecutionID;
clientEX.SetExecutionParameters(execHeader, trustedUserHeader, reportParameters.ToArray(), "pt-BR", out executionInfo);
SSRSEX.RenderRequest renderReq = new SSRSEX.RenderRequest(execHeader, trustedUserHeader, format.ToString(), deviceInfo);
SSRSEX.RenderResponse taskRender = await clientEX.RenderAsync(renderReq);
return new Report(taskRender.Extension, taskRender.MimeType, taskRender.Result);
}
}
public class UsingController
{
private readonly GenerateReportService executer;
public UsingController(GenerateReportService executer)
{
this.executer = executer;
}
public async System.Threading.Tasks.Task<ActionResult> IndexOne(int year, string format)
{
var parameters = new Dictionary<string, string>();
parameters.Add("Year", year.ToString());
var reportFormat = (Format)Enum.Parse(typeof(Format), format);
var relatorio = await this.executer.RenderAsync("reportName", parameters, format);
return File(relatorio.Results, relatorio.MimeType, string.Format("relatorio.{0}", relatorio.Extension));
}
public ActionResult IndexTwo(string format)
{
var parameters = new Dictionary<string, string>();
parameters.Add("Year", year.ToString());
var reportFormat = (Format)Enum.Parse(typeof(Format), format);
var relatorio = this.executer.Render("reportName", parameters, format);
return File(relatorio.Results, relatorio.MimeType, string.Format("relatorio.{0}", relatorio.Extension));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment