本文介绍了C#Azure函数:不能使用CloudQueue类型作为输出绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个通用的WebHook触发函数.我正在尝试将输出绑定添加到Azure队列存储.从文档中,我看到此输出支持CloudQueue类型.但是,当我在门户网站中运行以下代码时:

I am creating a generic WebHook trigger function. A am trying to add output binding to the Azure Queue Storage. From the documentation, I see that CloudQueue type is supported for this output. But when I run the following code in the portal:

public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log, CloudQueue outputQueueItem)
{
    log.Info("C# HTTP trigger function processed a request.");
}

它返回一个错误:

当我运行一个新的webhook函数时,该函数是使用以下代码从Visual Studio发布的:

When I run a new one webhook function that was published from Visual Studio with the following code:

namespace My.Azure.FunctionApp
{
    public static class SurveyWebHook
    {
        [FunctionName("SurveyWebHook")]
        public static async Task<object> Run([HttpTrigger(WebHookType = "genericJson")]HttpRequestMessage req,
           CloudQueue outputQueueItem, TraceWriter log)
        {
            log.Info($"Survey received");

            return req.CreateResponse(HttpStatusCode.OK, new
            {
                message = $"Survey received"
            });
        }
    }
}

它返回一个错误:

我怎样才能真正将CloudQueue类型变量作为输出绑定添加到我的WebHook函数?

How can I actually add CloudQueue type variable as output binding to my WebHook function?

更新:当我使用IBinder时,输入:

Update:When I use IBinder type:

namespace My.Azure.FunctionApp
{
    public static class SurveyWebHook
    {
        [FunctionName("SurveyWebHook")]
        public static async Task<object> Run([HttpTrigger(WebHookType = "genericJson")]HttpRequestMessage req, IBinder binder, TraceWriter log)
        {
            log.Info($"Survey received");
            string jsonContent = await req.Content.ReadAsStringAsync();
            CloudQueue outputQueue = await binder.BindAsync<CloudQueue>(new QueueAttribute("surveys"));
            await outputQueue.AddMessageAsync(new CloudQueueMessage("Test Message"));

            return req.CreateResponse(HttpStatusCode.OK, new
            {
                message = $"Survey received"
            });
        }
    }
}

它不返回错误.但这也不会使消息排队.当我使用[Queue("myqueue")] CloudQueue时,也会发生同样的情况.仅在使用IAsyncCollector<T>

It doesn't return an error. But it also doesn't put a message to the queue.The same happens when I use [Queue("myqueue")] CloudQueue.It only works when I use IAsyncCollector<T>

更新:最后,我了解了为什么我没有在队列中看到消息.当我从Visual Studio发布Azure Functions项目时,它将"configurationSource": "attributes"参数添加到function.json.这将覆盖我的connection参数,该输出绑定到我的Function App服务的默认存储帐户的输出中.我的队列是在此默认存储帐户中创建的.我删除了configurationSource参数,我的功能开始按预期运行.

Update:Finally, I have understood why I didn't see messages in my queue.When I publish Azure Functions project from Visual Studio, it adds "configurationSource": "attributes" parameter to the function.json. And this is overrides my connection parameter in the output binding to the default storage account of my Function App service. And my queue was created in this default storage account. I have removed configurationSource parameter and my function started to work as expected.

推荐答案

要使第一个示例生效,请修复引用-在顶部添加以下行:

To make your first example work, fix the reference - add these lines on top:

#r "Microsoft.WindowsAzure.Storage"

using Microsoft.WindowsAzure.Storage.Queue;

要使第二个示例正常工作,请在CloudQueue参数上标记QueueAttribute:

To make your second example work, mark the CloudQueue parameter with QueueAttribute:

[FunctionName("SurveyWebHook")]
public static async Task<object> Run(
    [HttpTrigger(WebHookType = "genericJson")] HttpRequestMessage req,
    [Queue("myqueue")] CloudQueue outputQueueItem,
    TraceWriter log)

这篇关于C#Azure函数:不能使用CloudQueue类型作为输出绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 02:18