C# Azure Functions:不能使用 CloudQueue 类型作为输出绑定
C# Azure Functions: Can't use CloudQueue type as output binding
我正在创建一个通用的 WebHook
触发器函数。我正在尝试将输出绑定添加到 Azure 队列存储。从文档中,我看到此输出支持 CloudQueue
类型。但是当我在传送门中运行下面的代码时:
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log, CloudQueue outputQueueItem)
{
log.Info("C# HTTP trigger function processed a request.");
}
它return给我一个错误:
error CS0246: The type or namespace name 'CloudQueue' could not be found (are you missing a using directive or an assembly reference?)
当我 运行 使用以下代码从 Visual Studio 发布的一个新的 webhook 函数时:
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"
});
}
}
}
它return给我一个错误:
"'SurveyWebHook' can't be invoked from Azure WebJobs SDK. Is it missing Azure WebJobs SDK attributes?"
我如何实际添加 CloudQueue
类型变量作为我的 WebHook
函数的输出绑定?
更新:
当我使用 IBinder
类型时:
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"
});
}
}
}
它不会 return 错误。但它也不会将消息放入队列。
当我使用 [Queue("myqueue")] CloudQueue
时也会发生同样的情况。
它仅在我使用 IAsyncCollector<T>
时有效
更新:
最后,我明白了为什么我在队列中看不到消息。
当我从 Visual Studio 发布 Azure Functions 项目时,它会将 "configurationSource": "attributes"
参数添加到 function.json
。这会覆盖我的 connection
输出绑定到我的 Function App 服务的默认存储帐户的参数。我的队列是在这个默认存储帐户中创建的。我删除了 configurationSource
参数,我的函数开始按预期工作。
要使您的第一个示例正常工作,请修复参考 - 在顶部添加这些行:
#r "Microsoft.WindowsAzure.Storage"
using Microsoft.WindowsAzure.Storage.Queue;
要使您的第二个示例正常工作,请将 CloudQueue
参数标记为 QueueAttribute
:
[FunctionName("SurveyWebHook")]
public static async Task<object> Run(
[HttpTrigger(WebHookType = "genericJson")] HttpRequestMessage req,
[Queue("myqueue")] CloudQueue outputQueueItem,
TraceWriter log)
我正在创建一个通用的 WebHook
触发器函数。我正在尝试将输出绑定添加到 Azure 队列存储。从文档中,我看到此输出支持 CloudQueue
类型。但是当我在传送门中运行下面的代码时:
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log, CloudQueue outputQueueItem)
{
log.Info("C# HTTP trigger function processed a request.");
}
它return给我一个错误:
error CS0246: The type or namespace name 'CloudQueue' could not be found (are you missing a using directive or an assembly reference?)
当我 运行 使用以下代码从 Visual Studio 发布的一个新的 webhook 函数时:
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"
});
}
}
}
它return给我一个错误:
"'SurveyWebHook' can't be invoked from Azure WebJobs SDK. Is it missing Azure WebJobs SDK attributes?"
我如何实际添加 CloudQueue
类型变量作为我的 WebHook
函数的输出绑定?
更新:
当我使用 IBinder
类型时:
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"
});
}
}
}
它不会 return 错误。但它也不会将消息放入队列。
当我使用 [Queue("myqueue")] CloudQueue
时也会发生同样的情况。
它仅在我使用 IAsyncCollector<T>
更新:
最后,我明白了为什么我在队列中看不到消息。
当我从 Visual Studio 发布 Azure Functions 项目时,它会将 "configurationSource": "attributes"
参数添加到 function.json
。这会覆盖我的 connection
输出绑定到我的 Function App 服务的默认存储帐户的参数。我的队列是在这个默认存储帐户中创建的。我删除了 configurationSource
参数,我的函数开始按预期工作。
要使您的第一个示例正常工作,请修复参考 - 在顶部添加这些行:
#r "Microsoft.WindowsAzure.Storage"
using Microsoft.WindowsAzure.Storage.Queue;
要使您的第二个示例正常工作,请将 CloudQueue
参数标记为 QueueAttribute
:
[FunctionName("SurveyWebHook")]
public static async Task<object> Run(
[HttpTrigger(WebHookType = "genericJson")] HttpRequestMessage req,
[Queue("myqueue")] CloudQueue outputQueueItem,
TraceWriter log)