首页 > 解决方案 > Azure 中的 CloudQueueMessage QueueTrigger 与本地实例

问题描述

我需要能够访问队列项本身(而不是字符串),以便我可以从我的函数中对其进行操作。

在 run.csx 代码中运行,我可以访问CloudQueueMessage

#r "Microsoft.WindowsAzure.Storage"

using Microsoft.WindowsAzure.Storage.Queue;
using System;

public static void Run(CloudQueueMessage myQueueItem, ILogger log)
{
            log.LogInformation($"Queue ID: {myQueueItem.Id}");
            log.LogInformation($"Queue Insertion Time: {myQueueItem.InsertionTime}");
            log.LogInformation($"Queue Expiration Time: {myQueueItem.ExpirationTime}");
            log.LogInformation($"Queue Payload: {myQueueItem.AsString}");
}

在我的本地开发中,此代码会引发错误。我使用所有最新的 Nuget。

VS2017 15.8.8

Azure 函数和 Web 作业工具:15.10.2046.0

    using Microsoft.Azure.WebJobs;
    using Microsoft.Extensions.Logging;
    using Microsoft.WindowsAzure.Storage.Queue;

    namespace AzurePumps.MyForce
    {
        public static class PersonQueueProcessor
        {
            [FunctionName("PersonQueueProcessor")]
            public static void Run([QueueTrigger("sfcontact-update-error", Connection = "storage-queue-connection")]CloudQueueMessage myQueueItem, ILogger log)
            {
                log.LogInformation($"Queue ID: {myQueueItem.Id}");
                log.LogInformation($"Queue Insertion Time: {myQueueItem.InsertionTime}");
                log.LogInformation($"Queue Expiration Time: {myQueueItem.ExpirationTime}");
                log.LogInformation($"Queue Payload: {myQueueItem.AsString}");
            }
        }
      }

日志:

Azure Functions Core Tools (2.1.748 Commit hash: 5db20665cf0c11bedaffc96d81c9baef7456acb3)
Function Runtime Version: 2.0.12134.0
Skipping 'SF_SecurityToken' from local settings as it's already defined in current environment variables.
[10/26/2018 8:07:51 PM] Building host: startup suppressed:False, configuration suppressed: False
[10/26/2018 8:07:52 PM] Reading host configuration file 'D:\Projects\Force\bin\Debug\netstandard2.0\host.json'
[10/26/2018 8:07:52 PM] Host configuration file read:
[10/26/2018 8:07:52 PM] {
[10/26/2018 8:07:52 PM]   "version": "2.0"
[10/26/2018 8:07:52 PM] }
[10/26/2018 8:07:53 PM] Initializing Host.
[10/26/2018 8:07:53 PM] Host initialization: ConsecutiveErrors=0, StartupCount=1
[10/26/2018 8:07:53 PM] Starting JobHost
[10/26/2018 8:07:53 PM] Starting Host (HostId=vddk35x1fmsnlt-1608145051, InstanceId=e9842cc2-f4a1-46c2-80d0-3e0aad9ae83b, Version=2.0.12134.0, ProcessId=16720, AppDomainId=1, Debug=False, FunctionsExtensionVersion=)
[10/26/2018 8:07:53 PM] Loading functions metadata
[10/26/2018 8:07:53 PM] 2 functions loaded
[10/26/2018 8:07:54 PM] Generating 2 job function(s)
[10/26/2018 8:07:55 PM] Found the following functions:
[10/26/2018 8:07:55 PM] AzurePumps.MyForce.PersonProcessor.Run
[10/26/2018 8:07:55 PM] AzurePumps.MyForce.PersonQueueProcessor.Run
[10/26/2018 8:07:55 PM]
[10/26/2018 8:07:55 PM] Host initialized (1859ms)
[10/26/2018 8:08:00 PM] Host started (6876ms)
[10/26/2018 8:08:00 PM] Job host started
Hosting environment: Production
Content root path: D:\Projects\Force\bin\Debug\netstandard2.0
Now listening on: http://0.0.0.0:7071
Application started. Press Ctrl+C to shut down.
Listening on http://0.0.0.0:7071/
Hit CTRL-C to exit...
[10/26/2018 8:08:06 PM] Host lock lease acquired by instance ID '0000000000000000000000006DB263D8'.
[10/26/2018 8:08:22 PM] Executing 'PersonQueueProcessor' (Reason='New queue message detected on 'sfcontact-update-error'.', Id=1e37d3a4-a992-41c8-b6ca-595787e5224e)
[10/26/2018 8:08:23 PM] Executed 'PersonQueueProcessor' (Failed, Id=1e37d3a4-a992-41c8-b6ca-595787e5224e)
**[10/26/2018 8:08:23 PM] System.Private.CoreLib: Exception while executing function: PersonQueueProcessor. Microsoft.Azure.WebJobs.Host: Exception binding parameter 'myQueueItem'. Microsoft.Azure.WebJobs.Extensions.Storage: Binding parameters to complex objects (such as 'CloudQueueMessage') uses Json.NET serialization.
1. Bind the parameter type as 'string' instead of 'CloudQueueMessage' to get the raw values and avoid JSON deserialization, or
2. Change the queue payload to be valid json. The JSON parser failed: Unexpected character encountered while parsing value: i. Path '', line 0, position 0.**

标签: azureazure-functions

解决方案


确保您没有引用/安装WindowsAzure.Storage最新版本 9.3.2,与 Function SDK 集成时似乎存在错误。查看跟踪的问题。

现在当我们创建一个 v2 函数项目时,模板中的依赖项,例如默认Microsoft.NET.SDK.Functions引用WindowsAzure.Storage9.3.1。此版本运行良好(正如您在 Azure 门户中看到的那样),无需单独安装包。

此外,建议您更新Microsoft.Azure.WebJobs.Extensions.Storage到 3.0.1 以避免一些第一次机会异常抛出。


推荐阅读