Azure functions Azure函数-为响应消息设置MediaTypeFormatter?

Azure functions Azure函数-为响应消息设置MediaTypeFormatter?,azure-functions,mediatypeformatter,Azure Functions,Mediatypeformatter,我正在尝试为Azure函数的响应设置媒体类型格式化程序以返回Excel文件。我不想要外部文件绑定,因为我想将输出流式传输到浏览器进行下载,而不是将文件写入blob或googlesheet或其他内容。在此之前,我曾在其他上下文(例如WebApi)中使用过EPPlus库和WebApiContrib.Formatting.Xlsx。我能够导入包并在函数中引用它们,但我无法成功添加格式化程序并将其应用于响应 是否可以为添加格式化程序的函数获取对HttpConfiguration对象的引用?我已尝试将Xl

我正在尝试为Azure函数的响应设置媒体类型格式化程序以返回Excel文件。我不想要外部文件绑定,因为我想将输出流式传输到浏览器进行下载,而不是将文件写入blob或googlesheet或其他内容。在此之前,我曾在其他上下文(例如WebApi)中使用过EPPlus库和WebApiContrib.Formatting.Xlsx。我能够导入包并在函数中引用它们,但我无法成功添加格式化程序并将其应用于响应

是否可以为添加格式化程序的函数获取对HttpConfiguration对象的引用?我已尝试将XlsxMediaTypeFormatter添加到CreateResponse调用,但由于某些原因,无法将其强制转换到MediaTypeFormatter。如果我只是尝试设置MIME类型,它会说没有可用的格式化程序

下面是我的测试函数:

using System.Net;
using System.Net.Http.Headers;
using System.Net.Http.Formatting;
using WebApiContrib.Formatting.Xlsx;
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{

    var testData = new List<TestItemDto>
    {
        new TestItemDto
        {
            Id = System.Guid.NewGuid().ToString(), 
            Name = "Test Item 1" 
        }, 
        new TestItemDto
        {
            Id = System.Guid.NewGuid().ToString(), 
            Name = "Test Item 2" 
        }
    };

    //var excelMediaType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
    var formatter = new XlsxMediaTypeFormatter(headerHeight: 25f, freezeHeader: true);

    var response = req.CreateResponse<List<TestItemDto>>(HttpStatusCode.OK, testData, (MediaTypeFormatter)formatter);
    response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
    {
        FileName = "TestData.xlsx"
    };
    return response;
}

public class TestItemDto
{
    public string Id { get; set; }
    public string Name { get; set; }
}
My project.json文件:

{
  "frameworks": {
    "net46":{
      "dependencies": {
        "EPPlus": "3.1.3.3", 
        "WebApiContrib.Formatting.Xlsx" : "1.0.1"
      }
    }
   }
}

这不是最好的答案,因为它绕过MediaTypeFormatter,直接创建响应内容。但是,我可以通过放弃XlsxMediaTypeFormatter,直接使用EPPlus库创建excel工作簿,并将其流式传输到浏览器(使用与上述相同的DTO和测试集合)使其正常工作:


如果有人能回答实际问题(例如,如何使用MediaTypeFormatter),我将保留该问题。

听起来像是程序集版本不匹配,但我不知道在哪里。您能否尝试使用
csproj
precompiled函数在本地运行相同的代码?似乎对我有用…我在本地尝试了它,但在实例化XlsxMediaTypeFormatter时出现运行时错误(空引用)。显然,该库在函数中不起作用(它是为在WebApi中使用而设计的)。仍在寻找其他解决方案,用于设置媒体类型和从函数将文件返回到浏览器。我想我会尝试使用EPPlus库生成文件,然后将其直接流式传输到响应内容中?
{
  "frameworks": {
    "net46":{
      "dependencies": {
        "EPPlus": "3.1.3.3", 
        "WebApiContrib.Formatting.Xlsx" : "1.0.1"
      }
    }
   }
}
// try to create an excel file by hand with EPPlus
ExcelPackage pck = new ExcelPackage();
var wsTest = pck.Workbook.Worksheets.Add("TestSheet");
//Load the collection starting from cell A1...
wsTest.Cells["A1"].LoadFromCollection(testData, true);
wsTest.Cells[wsTest.Dimension.Address].AutoFitColumns();

var excelMediaType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";

var response = req.CreateResponse(HttpStatusCode.OK);
response.Content = new StreamContent(new MemoryStream(pck.GetAsByteArray()));
response.Content.Headers.ContentType = new MediaTypeHeaderValue(excelMediaType);
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
      FileName = "TestData.xlsx"
};
return response;