C# Azure函数从调用端点获取参数
我试图实现一个方法,返回任何被调用Azure函数的参数名称 如果我有此Azure功能代码:C# Azure函数从调用端点获取参数,c#,azure,azure-functions,C#,Azure,Azure Functions,我试图实现一个方法,返回任何被调用Azure函数的参数名称 如果我有此Azure功能代码: [FunctionName("Something")] public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "customer/{param1}/{param2}")]HttpRequestMessage req, string
[FunctionName("Something")]
public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "customer/{param1}/{param2}")]HttpRequestMessage req, string param1, string param2, TraceWriter log)
{
//get the route params by calling the helper function
//my body
}
[FunctionName(“某物”)]
公共静态异步任务运行([HttpTrigger(AuthorizationLevel.Anonymous,“get”,Route=“customer/{param1}/{param2}”)]HttpRequestMessage请求,字符串param1,字符串param2,TraceWriter日志)
{
//通过调用helper函数获取路由参数
//我的身体
}
- 如果我使用以下命令调用函数端点:
- 辅助函数的输出->['param1','param2']
StackTrace
并搜索“Run”功能,
但我无法进入参数名称HttpRequestMessage
:我可以找到url,但其中包含值,但是我需要参数的实际名称function.json
中。文件结构如下所示
{
"generatedBy": "Microsoft.NET.Sdk.Functions-1.0.21",
"configurationSource": "attributes",
"bindings": [
{
"type": "httpTrigger",
"route": "customer/{param1}/{param2}",
"methods": [
"get",
"post"
],
"authLevel": "function",
"name": "req"
}
],
"disabled": false,
"scriptFile": "../bin/FunctionAppName.dll",
"entryPoint": "FunctionAppName.FunctionName.Run"
}
因此,一个棘手的方法是读取function.json
将ExecutionContext
添加到方法签名中,以便我们可以直接获取函数目录
public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "customer/{param1}/{param2}")]HttpRequestMessage req, string param1, string param2, TraceWriter log, ExecutionContext context)
{
//get the route params by calling the helper function
GetRouteParams(context, log);
//my body
}
public static void GetRouteParams(ExecutionContext context, TraceWriter log)
{
//function.json is under function directory
using (StreamReader r = File.OpenText(context.FunctionDirectory + "/function.json"))
{
// Deserialize json
dynamic jObject = JsonConvert.DeserializeObject(r.ReadToEnd());
string route = jObject.bindings[0].route.ToString();
// Search params name included in brackets
Regex regex = new Regex(@"(?<=\{)[^}]*(?=\})", RegexOptions.IgnoreCase);
MatchCollection matches = regex.Matches(route);
var list = matches.Cast<Match>().Select(m => m.Value).Distinct().ToList();
log.Info("[" + string.Join(", ", list) + "]");
}
}
公共静态异步任务运行([HttpTrigger(AuthorizationLevel.Anonymous,“get”,Route=“customer/{param1}/{param2}”)]HttpRequestMessage请求,字符串param1,字符串param2,TraceWriter日志,ExecutionContext上下文)
{
//通过调用helper函数获取路由参数
GetRouteParams(上下文、日志);
//我的身体
}
公共静态void GetRouteParams(ExecutionContext上下文,TraceWriter日志)
{
//function.json位于函数目录下
使用(StreamReader r=File.OpenText(context.FunctionDirectory+“/function.json”))
{
//反序列化json
DynamicJobject=JsonConvert.DeserializeObject(r.ReadToEnd());
字符串route=jObject.bindings[0]。route.ToString();
//搜索括号中包含的参数名称
正则表达式正则表达式=新正则表达式(@“(?我正在尝试实现一个helper函数,该函数返回任何被调用函数的参数名
我不认为您可以调用help函数来获取任何函数的参数,这似乎也很难。根据我的理解,如果您想调用一个函数并尝试获得预期的结果,您需要在您的计算机中实现它r Azure函数本身。在您的情况下,可以使用var p1=nameof(param1)和var p2=nameof(param2)
获取函数中的参数名。helper函数不是azure函数。它只是一个实用函数。正如我前面提到的,我认为没有办法让终结点获取终结点的参数名,除非你是终结点的所有者并将其作为输出。@编码仍然困惑吗?如果我的建议不存在的话你不能满足你的需要,你能提供更多的细节吗?