【发布时间】:2022-01-17 05:00:39
【问题描述】:
我有一个调用 Asp.net API 的 UWP 应用。我正在尝试获取 API 以从 SQL ReportServer 获取 PDF 并将其返回到 UWP 应用程序。
我的 API 控制器方法:
[HttpGet]
[Route("invoicereport/{report}/{invoice}")]
public IHttpActionResult GetInvoiceReport([FromUri] string report, [FromUri] string invoice)
{
try
{
Debug.WriteLine("Test");
ReportViewer ReportViewer1 = new ReportViewer();
ReportViewer1.ProcessingMode = ProcessingMode.Remote;
ReportViewer1.ServerReport.ReportServerUrl = new Uri("http://localhost/ReportServer");
ReportViewer1.ServerReport.ReportPath = $"/{ReportsDir}/Invoice/{report}";
ReportParameter[] rptParams = new ReportParameter[2];
rptParams[0] = new ReportParameter("SOPNUMBER", invoice);
rptParams[1] = new ReportParameter("SOPTypeString", "Invoice");
ReportViewer1.ServerReport.SetParameters(rptParams);
string format = "PDF", devInfo = @"<DeviceInfo><Toolbar>True</Toolbar></DeviceInfo>";
string mimeType = "",
encoding = "",
fileNameExtn = "";
string[] stearms = null;
Warning[] warnings = null;
byte[] result = null;
//render report, it will returns bite array
result = ReportViewer1.ServerReport.Render(format,
devInfo, out mimeType, out encoding,
out fileNameExtn, out stearms, out warnings);
//create file with require legth
//var pdf = PdfDocument.LoadFromStreamAsync(result);
FileStream stream = File.Create(Path.GetTempPath() + $"/Reports/{invoice}.pdf", result.Length);
//write file with rendered result
stream.Write(result, 0, result.Length);
//close stream
stream.Close();
var file = StorageFile.GetFileFromPathAsync(Path.GetTempPath() + $"/Reports/{invoice}.pdf");
var pdf = PdfDocument.LoadFromFileAsync(file.GetResults());
return Ok(pdf.GetResults());
}
catch (Exception ex)
{
Log.Logger.Error(ex,"Reporting Error");
}
return Ok();
}
当方法被调用时,它会立即出错并返回。我在它永远无法到达的调试行上有一个breakpoint。 Debug 行或 try-catch 不记录任何内容。
调试窗口输出:
...
Exception thrown: 'System.Web.HttpException' in System.Web.Mvc.dll
Exception thrown: 'System.Web.HttpException' in System.Web.dll
Exception thrown: 'System.Net.WebException' in System.Net.Requests.dll
Exception thrown: 'System.Net.WebException' in System.Private.CoreLib.dll
Exception thrown: 'System.Net.WebException' in System.Net.Requests.dll
Exception thrown: 'System.Xml.XmlException' in System.Private.Xml.dll
...
据我所知,它不喜欢Microsoft.Reporting.WebForms.ReportViewer。此控制器中的其他操作/方法运行良好。我知道这在技术上不是一个 WebForm,但我被困在下一步该做什么上。我们将不胜感激任何提示或只是向正确方向推进。
【问题讨论】:
标签: c# asp.net-web-api .net-framework-4.8