gpt4 book ai didi

azure - Azure 上的自定义 FileResult : Browser Waits forever

转载 作者:行者123 更新时间:2023-12-03 00:16:55 26 4
gpt4 key购买 nike

我有一个操作将 Excel 作为自定义 FileResult 返回。我的解决方案基于 ClosedXml库(内部使用 OpenXml)。我的 XlsxResult 类使用服务器上的只读 .xlsx 文件作为模板。然后,它将模板传递到内存流中,并使用 ClosedXml 对其进行操作和保存。最后,内存流被写入响应。

这在 Cassini 和 IIS Express 上都可以正常工作,但在部署在 azure 上时会失败,并且没有任何错误。我遇到的唯一影响是发送到服务器的请求永远不会得到任何响应。我仍在等待 60 分钟左右的事情发生......

我的行动:

[OutputCache(Location= System.Web.UI.OutputCacheLocation.None, Duration=0)]
public FileResult Export(int year, int month, int day) {
var date = new DateTime(year, month, day);
var filename = string.Format("MyTemplate_{0:yyyyMMdd}.xlsx", date);

//return new FilePathResult("~/Content/templates/MyTemplate.xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");

var result = new XlsxExportTemplatedResult("MyTemplate.xlsx", filename, (workbook) => {
var ws = workbook.Worksheets.Worksheet("My Export Sheet");
ws.Cell("B3").Value = date;
// Using a OpenXML's predefined formats (15 stands for date)
ws.Cell("B3").Style.NumberFormat.NumberFormatId = 15;
ws.Columns().AdjustToContents(); // You can also specify the range of columns to adjust, e.g.
return workbook;
});
return result;
}

我的文件结果

public class XlsxExportTemplatedResult : FileResult
{
// default buffer size as defined in BufferedStream type
private const int BufferSize = 0x1000;

public static readonly string TEMPLATE_FOLDER_LOCATION = @"~\Content\templates";

public XlsxExportTemplatedResult(string templateName, string fileDownloadName, Func<XLWorkbook, XLWorkbook> generate)
: base("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") {
this.TempalteName = templateName;
this.FileDownloadName = fileDownloadName;
this.Generate = generate;
}

public string TempalteName { get; protected set; }
public Func<XLWorkbook, XLWorkbook> Generate { get; protected set; }

protected string templatePath = string.Empty;
public override void ExecuteResult(ControllerContext context) {
templatePath = context.HttpContext.Server.MapPath(System.IO.Path.Combine(TEMPLATE_FOLDER_LOCATION, this.TempalteName));
base.ExecuteResult(context);
}

//http://msdn.microsoft.com/en-us/library/office/ee945362(v=office.11).aspx
protected override void WriteFile(System.Web.HttpResponseBase response) {
FileStream fileStream = new FileStream(templatePath, FileMode.Open, FileAccess.Read);
using (MemoryStream memoryStream = new MemoryStream()) {
CopyStream(fileStream, memoryStream);
using (var workbook = new XLWorkbook(memoryStream)) {
Generate(workbook);
workbook.Save();
}
// At this point, the memory stream contains the modified document.
// grab chunks of data and write to the output stream
Stream outputStream = response.OutputStream;
byte[] buffer = new byte[BufferSize];
while (true) {
int bytesRead = memoryStream.Read(buffer, 0, BufferSize);
if (bytesRead == 0) {
// no more data
break;
}
outputStream.Write(buffer, 0, bytesRead);
}
}
fileStream.Dispose();
}

static private void CopyStream(Stream source, Stream destination) {
byte[] buffer = new byte[BufferSize];
int bytesRead;
do {
bytesRead = source.Read(buffer, 0, buffer.Length);
destination.Write(buffer, 0, bytesRead);
} while (bytesRead != 0);
}

}

那么我是否错过了一些东西(显然我错过了)。

请注意:

  1. Azure 中没有丢失任何 dll,因为我使用 Windows Azure Tools 1.7 的 RemoteAccess 功能进行了检查
  2. 我的导出不是一项繁重的长时间运行任务。
  3. 当我将操作更改为仅返回带有模板 xlsx 的 FilePathResult 时,它可以在 azure 上运行。但我需要在返回文件之前对其进行处理,因为您可能会怀疑:-)

坦克。

UPDATE:After I logged extensively in my code the execution hangs with no error at the ClosedXml "Save" method call. But still no error. Abstract from the WADLogsTable:

  • Opening template file from path:E:\sitesroot\0\Content\templates\MyTemplate.xlsx
  • Opened template from path:E:\sitesroot\0\Content\templates\MyTemplate.xlsx just
  • copied template to editable memory stream. Bytes copied: 15955,Position: 15955
  • modified the excel document in memory.
  • here it hangs when a it calls to workbook.Save(); This is a ClosedXml method call.

最佳答案

我遇到了和你完全相同的错误情况。我无法针对您的具体情况提供解决方案,并且我知道您改变了方向,但是在经历了您所面临的相同令人沮丧的步骤之后,我想为您(或其他人)的答案“铺平道路” .

进入 Visual Studio 中的包管理器控制台并使用 MVC 功能(路由)安装 Elmah:

    Install-Package elmah.MVC

现在,在您的根 web.config 中,更新您的 Elmah 条目。它可能位于文件末尾,如下所示:

    <elmah></elmah>

更新那个坏男孩以允许远程访问并设置您的日志路径:

    <elmah>
<security allowRemoteAccess="1" />
<errorLog type="Elmah.XmlFileErrorLog, Elmah" logPath="~/app_data/elmah" />
</elmah>

现在,将其推送到 Azure。

最后,访问您的网站,强制出现错误,然后导航至 http://your-site-here.azurewebsites.net/elmah您将看到错误的确切原因。

Elmah 太棒了。

胆怯的坦白:我的错误不在第三方代码中,结果是在我的连接字符串中,我没有为其设置MultipleActiveResultsSets 为真。我必须做的另一个修复是在调用 ToList() 到该库上的一个内部方法之后将我的实体传递给该库,将其保留为 IQueryable 中断该方法。

关于azure - Azure 上的自定义 FileResult : Browser Waits forever,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12094389/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com