【问题标题】:C# and Azure Functions - Function isn't recognizedC# 和 Azure Functions - 无法识别函数
【发布时间】:2020-12-09 04:51:24
【问题描述】:

我编写了一个持久函数,它可以很好地处理传入的 JSON 数据并将其放入队列中进行处理。 (我的表存储也可以工作。)我的问题是处理队列触发器的 Azure 函数,其中我的一个函数没有被识别。我的完整代码目前是 200 行,所以这里是一个过于简化的示例:

namespace My.Namespace
{
    public static class Test
    {
        [FunctionName("Main")]
        public static async void Run([QueueTrigger("queue", Connection = "myinfo_STORAGE")] MyItem Item, ILogger log)
        {
            await DoSomethingElse("Information");
        }
        
        [FunctionName("DoSomething")]
        public static async Task Run(string msg, ILogger log)
        {
            // code to do something
            return;
        }       
    }
}   

在我尝试执行 await 的地方,它告诉我 The name 'DoSomething' does not exist in the current context. 我不明白 - 它在同一个类和命名空间中,这在我的持久函数编排项目中运行良好。但是,我注意到在我的持久函数编排项目中,持久函数有一个上下文,如下所示:

[OrchestrationTrigger] IDurableOrchestrationContext context,

然后我们使用上下文进行异步工作,例如:

string res = await context.CallActivityAsync<string>("AddSomeData", data);

AddSomeData 的定义如下:

[FunctionName("AddSomeData")]
public static async Task<Strin> Run(string data, ILogger log)
{
    // do work
    return "OK";
}

QueueTrigger 似乎没有与持久函数编排相同的上下文。我错过了什么?

(我是一名 VB.NET WinForms 程序员,正在使用 C# 过渡到 Azure 函数。)

【问题讨论】:

    标签: c# azure-functions


    【解决方案1】:

    不能直接通过 Azure Functions FunctionName 属性调用函数。

    在您的持久函数编排项目示例中,您不是直接调用函数,而是通过框架提供的机制调用:

    string res = await context.CallActivityAsync<string>("AddSomeData", data);
    

    您是否尝试过以下方法:

    string res = await AddSomeData(data); // no such function!
    

    甚至:

    string res = await AddSomeData(data, log); // still no such function!
    

    你最终会遇到同样的错误,除非 实际函数名 恰好也是 AddSomeData(在你的情况下,它是 Run - 所以你会得到错误)。

    因此,您可以使用函数的实际名称:

    [FunctionName("Main")]
    public static async void Run([QueueTrigger("queue", Connection = "myinfo_STORAGE")] MyItem Item, ILogger log)
    {
       await Run("Information", log);
    }
    

    请注意,您必须手动传递log - 在持久功能编排项目示例中,log 是通过context.CallActivityAsync 方法注入的。

    【讨论】:

    • 谢谢一百万,你是救生员。我肯定被那个函数名属性吓到了。
    猜你喜欢
    • 2022-08-22
    • 2015-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-15
    • 2021-07-16
    • 1970-01-01
    • 2021-05-31
    相关资源
    最近更新 更多