【问题标题】:Why does my Asp.Net API not like my ReportViewer为什么我的 Asp.Net API 不像我的 ReportViewer
【发布时间】: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


    【解决方案1】:

    又经历了几个小时的挫折……

    我改为使用 SOAP WebService 访问 ReportServer

    [HttpGet]
    [Route("invoicereport/{report}/{invoice}")]
    public async Task<IHttpActionResult> GetInvoiceReport([FromUri] string report, [FromUri] string invoice)
    {
        try
        {
            Debug.WriteLine("Test");
    
            ReportExecutionService rs = new ReportExecutionService();
    
            rs.Credentials = CredentialCache.DefaultCredentials;
    
            rs.Url = "http://localhost/reportserver/reportexecution2005.asmx";
    
            rs.ExecutionHeaderValue = new ExecutionHeader();
            var executionInfo = new ExecutionInfo();
            executionInfo = rs.LoadReport($"{ReportsDir}/Invoice/{report}", null);
            
    
            List<ParameterValue> parameters = new List<ParameterValue>();
    
            parameters.Add(new ParameterValue { Name = "SOPNUMBER", Value = invoice });
            parameters.Add(new ParameterValue { Name = "SOPTypeString", Value = "Invoice" });
    
            rs.SetExecutionParameters(parameters.ToArray(), "en-US");
    
            string deviceInfo = "<DeviceInfo><Toolbar>False</Toolbar></DeviceInfo>";
            string mimeType;
            string encoding;
            string[] streamId;
            Warning[] warning;
    
            var result = rs.Render("PDF", deviceInfo, out mimeType, out encoding, out encoding, out warning, out streamId);
    
            FileStream stream = File.Create(System.Web.HttpRuntime.CodegenDir + $"/{invoice}.pdf", result.Length);
    
            //write file with rendered result
    
            stream.Write(result, 0, result.Length);
    
            //close stream
    
            stream.Close();
    
            StorageFile file = StorageFile.GetFileFromPathAsync(System.Web.HttpRuntime.CodegenDir + $"/{invoice}.pdf").GetResults();
    
            var pdf = PdfDocument.LoadFromFileAsync(file).GetResults();
    
            return Ok(pdf);
    
        }
        catch (Exception ex)
        {
            Log.Logger.Error(ex, "Reporting Error");
        }
    
        return Ok();
    }
    

    这解决了我的问题。

    【讨论】:

      猜你喜欢
      • 2012-02-14
      • 2017-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多