生成 .rdlc 报告在本地有效,但在部署到 azure 后无效
Posted
技术标签:
【中文标题】生成 .rdlc 报告在本地有效,但在部署到 azure 后无效【英文标题】:Generating .rdlc report works on local but not after deploying to azure 【发布时间】:2016-11-06 07:58:43 【问题描述】:在我的 ASP.NET MVC 5 应用程序中,我有以下方法:
public ActionResult SendMailAsAStudent(string studentId, string companyId, int applicationId, string companyCVR, string studentCPR)
var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
var student = manager.FindById(studentId);
var company = manager.FindById(companyId);
var application = db.Applications.Find(applicationId);
Project projectobj = db.Projects.Find(application.ProjectId);
var myMessage = new SendGridMessage();
myMessage.From = new MailAddress("info@leepio.dk");
myMessage.AddTo(student.Email);
myMessage.AddTo(company.Email);
myMessage.Subject ="The contract for " + projectobj.Title + " is signed!";
myMessage.html = "Here is the final contract";
LocalReport localReport = new LocalReport();
localReport.ReportPath = Server.MapPath("~/Contracts/Contract.rdlc");
ReportParameter cvrParameter = new ReportParameter("cvrParameter", companyCVR);
ReportParameter companyNameParameter = new ReportParameter("companyNameParameter", company.CompanyName);
ReportParameter companyAddressParameter = new ReportParameter("companyAddressParameter", company.Address);
ReportParameter studentNameParameter = new ReportParameter("studentNameParameter", student.FirstName+" "+student.LastName);
ReportParameter studentAddressParameter = new ReportParameter("studentAddressParameter", student.Address);
ReportParameter studentZipCodeParameter = new ReportParameter("studentZipCodeParameter", student.ZipCode);
ReportParameter jobStartDateParameter = new ReportParameter("jobStartDateParameter", projectobj.StartDate);
ReportParameter jobEndDateParameter = new ReportParameter("jobEndDateParameter", projectobj.EndDate);
ReportParameter jobDescriptionParameter = new ReportParameter("jobDescriptionParameter", projectobj.Description);
ReportParameter jobHoursPerWeekParameter = new ReportParameter("jobHoursPerWeekParameter", projectobj.HoursPerWeek.ToString());
ReportParameter jobHourlyRateParameter = new ReportParameter("jobHourlyRateParameter", projectobj.HourlyRate.ToString());
ReportParameter cprParameter = new ReportParameter("cprParameter", studentCPR);
localReport.SetParameters(new ReportParameter[] cprParameter );
localReport.SetParameters(new ReportParameter[] cvrParameter );
localReport.SetParameters(new ReportParameter[] companyNameParameter );
localReport.SetParameters(new ReportParameter[] companyAddressParameter );
localReport.SetParameters(new ReportParameter[] studentNameParameter );
localReport.SetParameters(new ReportParameter[] studentAddressParameter );
localReport.SetParameters(new ReportParameter[] studentZipCodeParameter );
localReport.SetParameters(new ReportParameter[] jobStartDateParameter );
localReport.SetParameters(new ReportParameter[] jobEndDateParameter );
localReport.SetParameters(new ReportParameter[] jobDescriptionParameter );
localReport.SetParameters(new ReportParameter[] jobHoursPerWeekParameter );
localReport.SetParameters(new ReportParameter[] jobHourlyRateParameter );
string reportType = "PDF";
string mimeType;
string encoding;
string fileNameExtension = "pdf";
Warning[] warnings;
string[] streams;
var renderedBytes = localReport.Render(reportType, "", out mimeType, out encoding, out fileNameExtension, out streams, out warnings);
Response.AddHeader("content-disposition", "attatchment; filename=Contract." + fileNameExtension);
MemoryStream stream = new MemoryStream(renderedBytes);
myMessage.AddAttachment(stream, "Contract.pdf");
var apiKey = "key removed for this post";
var transportWeb = new Web(apiKey);
transportWeb.DeliverAsync(myMessage);
Contract contract = new Contract();
contract.StudentId = studentId;
contract.CompanyId = companyId;
contract.CVR = companyCVR;
contract.ApplicationId = applicationId;
contract.IsSigned = true;
db.Contracts.Add(contract);
db.SaveChanges();
return RedirectToAction("Index", "Projects");
如果我在本地主机上运行它,它会成功发送带有报告文件的邮件。部署到 Azure 时,它不再起作用。注释掉报告生成并将其添加为附件使其正常工作,因此我将问题确定为报告生成。
什么可能导致它不再在网站上运行?我检查了参数不为空。
【问题讨论】:
您需要比“不起作用”更具体。有没有抛出异常?我最初的怀疑是您的 azure 实例没有安装报告运行时。 没有异常或类似的东西,试图找出F12的问题,但没有。可能就像您描述的那样,但是我的参考文献中有 Microsoft.ReportViewer.Common 和 Microsoft.ReportViewer.WebForms。当我点击发布时它们没有安装吗? 两个 CopyLocal 都设置为 TRUE 肯定有异常,你知道怎么在Azure上远程调试吗? 在快速谷歌之后,我发现了如何做到这一点。我的另一个问题是在 Visual Studio 中选择“发布”而不是“调试”会改变结果吗?编辑:即使选择了“发布”,仍然可以在我的本地机器上工作 【参考方案1】:我打开了 web.config 中显示的错误,如下所示:
<customErrors mode="Off">
然后我得到了错误:
Could not load file or assembly 'Microsoft.ReportViewer.ProcessingObjectModel, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
感谢 Crowcoder,我终于找到了问题所在,我不得不从 NuGet 包管理器安装 Microsoft.Report.Viewer 10.0.0!
构建、发布,现在可以使用了!
【讨论】:
以上是关于生成 .rdlc 报告在本地有效,但在部署到 azure 后无效的主要内容,如果未能解决你的问题,请参考以下文章
使用 s-s-rS 2017 RDLC 文件创建本地 PDF 报告