【发布时间】:2021-02-25 19:04:34
【问题描述】:
我有一个获取 HTTP 请求的 Azure 函数,这个 azure 函数调用更新数据库的存储过程,
存储过程运行良好并更新数据库中的表。
但是当我想用 postMan 模拟这个请求时,我遇到了错误(500 :Internal Serveur Erro)。
这是我的 Azure 函数:
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System;
using Core.Handlers;
using System.Text.Json;
using Newtonsoft.Json.Linq;
namespace RealTimeTriggerFunctions
{
public static class SendToAzureSql
{
private static readonly string AZURE_TABLE1=
Environment.GetEnvironmentVariable("AZURE_TABLE1");
// Handler
private static AzureSqlHandler azSqlHandler;
[FunctionName("SendToAzureSql")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
ILogger log)
{
string procedureName = "";
try
{
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
if(string.IsNullOrEmpty(requestBody))
{
return new BadRequestResult();
}
dynamic message = JsonConvert.DeserializeObject(requestBody);
if (message == null)
{
return new BadRequestResult();
}
log.LogInformation((string)message.type.ToString()+ " progress...");
switch (message.type.ToString())
{
case "xx.yy.zz.event.table1.table1":
procedureName = "stored_table1";
InitHandlers(log, AZURE_TABLE1);
break;
default:
return new BadRequestObjectResult("Wrong Request!");
}
var dataJson = JsonDocument.Parse(requestBody);
string actionType = message.type.ToString().Contains("deleted") ? "Deleted": "Default";
await azSqlHandler.UpsertItemAsync(procedureName, actionType, payload:
dataJson.RootElement);
return new OkObjectResult(message.type.ToString() + " Processed");
}
catch (Exception e)
{
log.LogError($"An error occurred while processing request : '{e.Message}'");
throw e;
}
}
/// <summary>
/// Init connexions
/// </summary>
private static void InitHandlers(ILogger log, string connectionString)
{
// Create handler
azSqlHandler = new AzureSqlHandler(log, connectionString);
}
}
}
我在 POST 中调用此请求:http://localhost:7071/API/SendToAzureSql 我得到500。
【问题讨论】:
-
错误是什么? “我有一些错误”信息不足。
-
@Kashyap,是的,我最后提到了.. 我在本地运行它然后我得到 500:Internal Serveur Error
-
如果您在本地运行此程序,您应该能够在日志中看到完整的异常堆栈跟踪。如果您在云中运行它,请转到门户->功能-应用程序->功能->->监控并单击失败的执行以查看日志。为了调试,我建议您将所有函数的实现放在一个 try-catch-all 块中并记录它。
-
参数:连接字符串:[未指定连接字符串],资源:database.windows.net,权限:。异常消息:尝试使用托管服务标识获取令牌。无法连接到实例元数据服务 (IMDS)。跳过对托管服务身份 (MSI) 令牌端点的请求。我明白了
-
太棒了!修复它。
标签: c# .net azure-functions postman