具有 HTTP 触发器和 blob 输出绑定的 Azure 函数失败,返回 500,没有详细信息
Azure function with HTTP trigger and blob output binding failed with 500 and no details
我正在尝试创建一个简单的 Azure 函数应用程序,它从 HTTP 请求接收图像二进制文件并使用 C# 和无服务器框架写入 blob 存储。
C#函数代码如下:
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Microsoft.Azure.WebJobs.Extensions.Storage;
namespace ThumbnailGenerator.Azure
{
public static class ThumbnailGenerator
{
[FunctionName("upload")]
[StorageAccount("AzureWebJobsStorage")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
[Blob("input/{name}", FileAccess.Write)] Stream inputImage,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
string name = req.Query["name"];
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
byte[] data = System.Convert.FromBase64String(requestBody);
await inputImage.WriteAsync(data);
return new OkObjectResult("Succeed:" + name);
}
}
}
我的 host.json
看起来如下:
{
"version": "2.0",
"logging": {
"applicationInsights": {
"samplingSettings": {
"isEnabled": true,
"excludedTypes": "Request"
}
}
},
"extensions": {
"blobs": {
"maxDegreeOfParallelism": "4"
}
}
}
应用程序是使用无服务器框架部署的
service: thumbnail-dotnet
frameworkVersion: '2'
provider:
name: azure
runtime: dotnet3.1
region: East US
# List of plugins used by service. Must always include: - serverless-azure-functions
plugins:
- serverless-azure-functions
functions:
upload:
handler: src/handlers/upload.main
events:
- http: true
methods:
- GET
authLevel: anonymous
然而,当我测试它时,Azure 总是给出 500 Internal Server Error
没有任何细节。没有应用程序洞察跟踪,没有日志,执行计数也为零,这让我很困惑。
这是在 Azure
上生成的 function.json
{
"generatedBy": "Microsoft.NET.Sdk.Functions-3.0.11",
"configurationSource": "attributes",
"bindings": [
{
"type": "httpTrigger",
"methods": [
"get",
"post"
],
"authLevel": "anonymous",
"name": "req"
}
],
"disabled": false,
"scriptFile": "../bin/ThumbnailGenerator.dll",
"entryPoint": "ThumbnailGenerator.Azure.ThumbnailGenerator.Run"
}
我用不同的方法调试了一个星期都没有找到原因。
谁能给点提示?
如果我在本地 运行 您的代码,将显示以下异常:
Microsoft.Azure.WebJobs.Host: Error indexing method 'upload'. Microsoft.Azure.WebJobs.Host: Unable to resolve binding parameter 'name'. Binding expressions must map to either a value provided by the trigger or a property of the value the trigger is bound to, or must be a system binding expression (e.g. sys.randguid, sys.utcnow, etc.).
如错误消息中所述,您必须在触发器中指定变量。我想,在 Azure Functions 中仍然无法绑定到查询参数。
所以你必须在路由中指定它:
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = "{name}")] HttpRequest req,
运行 本地:
upload: [GET,POST] http://localhost:7071/api/{name}
完整代码:
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Microsoft.Azure.WebJobs.Extensions.Storage;
namespace ThumbnailGenerator.Azure
{
public static class ThumbnailGenerator
{
[FunctionName("upload")]
[StorageAccount("AzureWebJobsStorage")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = "{name}")] HttpRequest req,
[Blob("input/{name}", FileAccess.Write)] Stream inputImage,
string name,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
byte[] data = System.Convert.FromBase64String(requestBody);
await inputImage.WriteAsync(data);
return new OkObjectResult("Succeed:" + name);
}
}
}
请在GitHub
上找到工作代码
我正在尝试创建一个简单的 Azure 函数应用程序,它从 HTTP 请求接收图像二进制文件并使用 C# 和无服务器框架写入 blob 存储。
C#函数代码如下:
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Microsoft.Azure.WebJobs.Extensions.Storage;
namespace ThumbnailGenerator.Azure
{
public static class ThumbnailGenerator
{
[FunctionName("upload")]
[StorageAccount("AzureWebJobsStorage")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
[Blob("input/{name}", FileAccess.Write)] Stream inputImage,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
string name = req.Query["name"];
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
byte[] data = System.Convert.FromBase64String(requestBody);
await inputImage.WriteAsync(data);
return new OkObjectResult("Succeed:" + name);
}
}
}
我的 host.json
看起来如下:
{
"version": "2.0",
"logging": {
"applicationInsights": {
"samplingSettings": {
"isEnabled": true,
"excludedTypes": "Request"
}
}
},
"extensions": {
"blobs": {
"maxDegreeOfParallelism": "4"
}
}
}
应用程序是使用无服务器框架部署的
service: thumbnail-dotnet
frameworkVersion: '2'
provider:
name: azure
runtime: dotnet3.1
region: East US
# List of plugins used by service. Must always include: - serverless-azure-functions
plugins:
- serverless-azure-functions
functions:
upload:
handler: src/handlers/upload.main
events:
- http: true
methods:
- GET
authLevel: anonymous
然而,当我测试它时,Azure 总是给出 500 Internal Server Error
没有任何细节。没有应用程序洞察跟踪,没有日志,执行计数也为零,这让我很困惑。
这是在 Azure
上生成的 function.json{
"generatedBy": "Microsoft.NET.Sdk.Functions-3.0.11",
"configurationSource": "attributes",
"bindings": [
{
"type": "httpTrigger",
"methods": [
"get",
"post"
],
"authLevel": "anonymous",
"name": "req"
}
],
"disabled": false,
"scriptFile": "../bin/ThumbnailGenerator.dll",
"entryPoint": "ThumbnailGenerator.Azure.ThumbnailGenerator.Run"
}
我用不同的方法调试了一个星期都没有找到原因。
谁能给点提示?
如果我在本地 运行 您的代码,将显示以下异常:
Microsoft.Azure.WebJobs.Host: Error indexing method 'upload'. Microsoft.Azure.WebJobs.Host: Unable to resolve binding parameter 'name'. Binding expressions must map to either a value provided by the trigger or a property of the value the trigger is bound to, or must be a system binding expression (e.g. sys.randguid, sys.utcnow, etc.).
如错误消息中所述,您必须在触发器中指定变量。我想,在 Azure Functions 中仍然无法绑定到查询参数。
所以你必须在路由中指定它:
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = "{name}")] HttpRequest req,
运行 本地:
upload: [GET,POST] http://localhost:7071/api/{name}
完整代码:
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Microsoft.Azure.WebJobs.Extensions.Storage;
namespace ThumbnailGenerator.Azure
{
public static class ThumbnailGenerator
{
[FunctionName("upload")]
[StorageAccount("AzureWebJobsStorage")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = "{name}")] HttpRequest req,
[Blob("input/{name}", FileAccess.Write)] Stream inputImage,
string name,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
byte[] data = System.Convert.FromBase64String(requestBody);
await inputImage.WriteAsync(data);
return new OkObjectResult("Succeed:" + name);
}
}
}
请在GitHub
上找到工作代码