C# 无法从post Man调用smulate函数来更新数据库
我有一个Azure函数来获取HTTP请求,这个Azure函数调用一个存储过程来更新数据库, 存储过程运行良好,并在DB中更新表。 但当我想用邮递员模拟这个请求时,我出现了错误(C# 无法从post Man调用smulate函数来更新数据库,c#,.net,azure-functions,postman,C#,.net,Azure Functions,Postman,我有一个Azure函数来获取HTTP请求,这个Azure函数调用一个存储过程来更新数据库, 存储过程运行良好,并在DB中更新表。 但当我想用邮递员模拟这个请求时,我出现了错误(500:internalserveur Erro) 以下是我的函数Azure: using System.IO; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.Azure.WebJobs; using Micros
500:internalserveur 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);
}
}
使用System.IO;
使用System.Threading.Tasks;
使用Microsoft.AspNetCore.Mvc;
使用Microsoft.Azure.WebJobs;
使用Microsoft.AspNetCore.Http;
使用Microsoft.Extensions.Logging;
使用Newtonsoft.Json;
使用制度;
使用Core.Handlers;
使用System.Text.Json;
使用Newtonsoft.Json.Linq;
命名空间RealTimeTrigger函数
{
公共静态类SendToAzureSql
{
专用静态只读字符串AZURE\u TABLE1=
GetEnvironmentVariable(“AZURE_表1”);
//处理者
私有静态AzureSqlHandler AzzlHandler;
[函数名(“SendToAzureSql”)]
公共静态异步任务运行(
[HttpTrigger(AuthorizationLevel.Anonymous,“get”,“post”,Route=null)]HttpRequest请求,
ILogger日志)
{
字符串procedureName=“”;
尝试
{
string requestBody=等待新的StreamReader(req.Body).ReadToEndAsync();
if(string.IsNullOrEmpty(requestBody))
{
返回新的BadRequestResult();
}
动态消息=JsonConvert.DeserializeObject(requestBody);
如果(消息==null)
{
返回新的BadRequestResult();
}
log.LogInformation((字符串)message.type.ToString()+“progress…”);
开关(message.type.ToString())
{
案例“xx.yy.zz.event.table1.table1”:
procedureName=“存储的表格1”;
InitHandlers(日志,AZURE_表1);
打破
违约:
返回新的BadRequestObjectResult(“错误的请求!”);
}
var dataJson=JsonDocument.Parse(requestBody);
string actionType=message.type.ToString().包含(“已删除”)?“已删除”:“默认值”;
等待azSqlHandler.UpsertItemAsync(过程重命名、操作类型、负载:
dataJson.RootElement);
返回新的OkObjectResult(message.type.ToString()+“Processed”);
}
捕获(例外e)
{
LogError($“处理请求时出错:{e.Message}'”;
投掷e;
}
}
///
///初始连接
///
私有静态void InitHandlers(ILogger日志、字符串连接字符串)
{
//创建处理程序
azSqlHandler=新的AzureSqlHandler(日志、连接字符串);
}
}
}
我在POST中称此请求为:http://localhost:7071/API/SendToAzureSql
我得到了500
我在这里找到了我的答案:
我在Azure cli中运行az-login,它可以正常工作有什么错误?“我有一些错误”是不够的信息。@Kashyap,是的,我在最后提到了。。我在本地运行它,然后得到500:internalserveur错误如果您在本地运行它,您应该能够在日志中看到完整的异常堆栈跟踪。如果您在云中运行此功能,请转到门户->功能应用->功能->->->监视器,然后单击失败的执行以查看日志。要进行调试,我建议您将函数的所有实现放在一个try-catch-all块中并记录它。参数:连接字符串:[未指定连接字符串]、资源:、权限:。异常消息:尝试使用托管服务标识获取令牌。无法连接到实例元数据服务(IMDS)。正在跳过对托管服务标识(MSI)令牌终结点的请求。我明白了,太棒了!解决这个问题。