📄 rshelpers.cs
字号:
using System;
using System.IO;
using System.IO.IsolatedStorage;
using System.Web.Services.Protocols;
using System.Net;
using System.Web;
using System.Configuration;
using System.Text;
using System.Xml;
using AWC.Reporter.Web.RS;
using AWC.Reporter.Web.Entities;
namespace AWC.Reporter.Web
{
internal enum PermissionType
{
Item,
System
}
/// <summary>
/// Some Reporting Services helper methods
/// </summary>
internal class RsHelpers
{
static RsHelpers()
{
}
/// <summary>
/// Gets the Reporting Services proxy
/// </summary>
internal static ReportingService Proxy
{
get
{
ReportingService rsProxy = new ReportingService();
rsProxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
// or use this to pass the credentials explicitly
// rsProxy.Credentials = new System.Net.NetworkCredential("username", "password");
rsProxy.Url = ConfigurationSettings.AppSettings[Constants.CONFIG_RS_URL] + "/ReportService.asmx";
return rsProxy;
}
}
/// <summary>
/// Sets the stream root for the Image Urls
/// </summary>
/// <param name="streamRootPath"></param>
/// <param name="devInfo"></param>
/// <returns></returns>
internal static string SetStreamRoot (string devInfo)
{
if (devInfo == null) devInfo = "<DeviceInfo/>";
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml (devInfo);
XmlNode streamRoot = xmlDoc.CreateNode(XmlNodeType.Element,"StreamRoot", String.Empty );
streamRoot.InnerText = String.Format("http://{0}{1}/{2}?ID=", HttpContext.Current.Request.ServerVariables["HTTP_HOST"],
HttpContext.Current.Request.ApplicationPath, "ImageHandler.aspx");
xmlDoc.ImportNode(streamRoot, false);
xmlDoc.DocumentElement.AppendChild(streamRoot);
return xmlDoc.InnerXml;
}
/// <summary>
/// Returns the location of the image store where the images will be downloaded
/// for HTML reports
/// </summary>
/// <returns></returns>
internal static string GetStreamRootPath()
{
return HttpContext.Current.Request.ApplicationPath + ConfigurationSettings.AppSettings[Constants.CONFIG_STREAM_ROOT];
}
/// <summary>
/// Renders a report via SOAP
/// </summary>
/// <param name="reportRequest">An instance of the ReportRequest entity</param>
/// <param name="SessionID">The report database session identifier</param>
/// <returns>The report payload as a byte array</returns>
internal static byte[] RenderReport (ReportRequest reportRequest, out string SessionID)
{
ReportingService rs = RsHelpers.Proxy;
// Render arguments
byte[] result = null;
string reportPath = reportRequest.Report[0].Path;
string historyID = null;
string format = reportRequest.Report[0].ExportFormat;
string devInfo = reportRequest.Report[0].DeviceSettings;
DataSourceCredentials[] credentials = null;
string showHideToggle = null;
string encoding;
string mimeType;
Warning[] warnings = null;
ParameterValue[] reportHistoryParameters = null;
string[] streamIDs = null;
ParameterValue[] reportParameters = null;
string optionalString = null;
string streamRootPath = GetStreamRootPath();
SessionHeader sh = new SessionHeader();
if (reportRequest.Report[0].ExecutionOption==(short)ExecutionSettingEnum.Snapshot)
{
// this is a snapshot report
if (!reportRequest.Report[0].IsHistoryIDNull())
{
historyID = reportRequest.Report[0].HistoryID;
}
}
else
{
reportParameters = new ParameterValue[reportRequest.Parameters.Count];
// populate the parameters collection
for (int i=0; i<reportRequest.Parameters.Count; i++)
{
reportParameters[i] = new ParameterValue();
reportParameters[i].Name = reportRequest.Parameters[i].Name;
reportParameters[i].Value = reportRequest.Parameters[i].Value;
}
}
try
{
// set the streamrootpath so the Report Server can save the images to this folder
// refer to Chapter 11 for more details
if (IsHtmlReport(format)) devInfo = SetStreamRoot(devInfo);
if (reportRequest.Report[0].SessionID!=null)
{
RS.SessionHeader sessionHeader = new RS.SessionHeader();
sessionHeader.SessionId = reportRequest.Report[0].SessionID;
// request the report to be rendered from the same db session
rs.SessionHeaderValue = sessionHeader;
}
result = rs.Render(reportPath, format, historyID, devInfo, reportParameters, credentials,
showHideToggle, out encoding, out mimeType, out reportHistoryParameters, out warnings,
out streamIDs);
// for multi-stream renders, such as HTML, need to render the report images as streams
// so they show in the report
// alternatively, if HTTP GET is allowed on the Report Server,
// the HTMLFragment device info setting could be used
if (IsHtmlReport(format))
{
string physicalStreamPath = HttpContext.Current.Server.MapPath(streamRootPath);
foreach (string streamID in streamIDs)
{
byte [] image = rs.RenderStream(reportPath, format, streamID, null, null, reportParameters, out optionalString, out optionalString);
string imageFileName = physicalStreamPath + streamID;
FileStream stream = File.OpenWrite(imageFileName);
stream.Write(image, 0, image.Length);
stream.Close();
}
}
}
catch (SoapException ex)
{
System.Diagnostics.Trace.WriteLine(ex.Detail.OuterXml);
throw ex;
}
SessionID = rs.SessionHeaderValue.SessionId;
return result;
}
private static bool IsHtmlReport(string reportFormat)
{
return ("ht" == reportFormat.Substring(0, 2).ToLower());
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -