【问题标题】:Azure Functions not creating Azure Tables Columns LocallyAzure Functions 未在本地创建 Azure 表列
【发布时间】:2021-08-09 01:02:51
【问题描述】:

我正在尝试创建 Azure 函数。在本地,我希望能够在部署之前测试我的功能。我正在使用 VS Code 在 MacOS 11.2.3 上进行开发。我使用Azurite 作为在 Docker 中运行的本地存储模拟器。我可以连接到本地模拟器并查看我的队列和存储。我的 Functions 应用使用的是 netcoreapp3.1,是一个 Functions v3 应用。

我的触发器是队列接收到的新负载。我的触发器工作得很好,当我将数据写入 Azure 存储表时,我可以看到 RowKey、PartitionKey 和时间戳。我看不到我创建的任何数据。这是我的代码:

public static class MyFunction
{
    [FunctionName("MyFunction")]
    [return: Table("mytable")]
    public static MyObject Run([QueueTrigger("myqueue")]string queueItem, ILogger log)
    {
        log.LogInformation($"C# Queue trigger function processed");
        var myObject = JsonConvert.DeserializeObject<MyObject>(queueItem);
        log.LogInformation(JsonConvert.SerializeObject(myObject));
        return myObject;
    }
}

这里是 MyObject.cs

using System;
using Microsoft.WindowsAzure.Storage.Table;

namespace MyProject.models
{
    public sealed class MyObject : TableEntity
    {
        public MyObject(string myProperty)
        {
            string PartitionMonth = DateTime.Now.ToMonthName();
            string PartitionYear = DateTime.Now.Year.ToString();
            PartitionKey = $"{PartitionMonth}-{PartitionYear}";

            RowKey = Guid.NewGuid().ToString();
            MyProperty = myProperty;

        }
        public string MyProperty { get; }
    }
}

问题是我没有看到正在创建的 MyProperty 列。我提供给队列的 JSON 有效负载有它,我可以看到它记录到记录器中,我只是在 Azure 存储资源管理器中看不到该列。每次触发我的函数时,我都会看到一行。请帮助我了解为什么我看不到我的数据。

【问题讨论】:

    标签: azure-functions azure-storage azure-table-storage azure-storage-queues azure-storage-emulator


    【解决方案1】:

    我相信你遇到这个问题是因为你的MyProperty 没有公共设置器。

    请尝试更改这行代码:

    public string MyProperty { get; }
    

    public string MyProperty { get; set; }
    

    您的代码应该可以正常运行。

    参考:https://github.com/Azure/azure-storage-net/blob/933836a01432da169966017f0848d7d6b05fc624/Lib/Common/Table/TableEntity.cs#L406(参见代码中的“Enforce public getter / setter”)。

    internal static bool ShouldSkipProperty(PropertyInfo property, OperationContext operationContext)
    {
        // reserved properties
        string propName = property.Name;
        if (propName == TableConstants.PartitionKey ||
            propName == TableConstants.RowKey ||
            propName == TableConstants.Timestamp ||
            propName == TableConstants.Etag)
        {
            return true;
        }
    
        MethodInfo setter = property.FindSetProp();
        MethodInfo getter = property.FindGetProp();
    
        // Enforce public getter / setter
        if (setter == null || !setter.IsPublic || getter == null || !getter.IsPublic)
        {
            Logger.LogInformational(operationContext, SR.TraceNonPublicGetSet, property.Name);
            return true;
        }
    
        // Skip static properties
        if (setter.IsStatic)
        {
            return true;
        }
    
        // properties with [IgnoreAttribute]
    #if WINDOWS_RT || NETCORE 
        if (property.GetCustomAttribute(typeof(IgnorePropertyAttribute)) != null)
    #else
        if (Attribute.IsDefined(property, typeof(IgnorePropertyAttribute)))
    #endif
        {
            Logger.LogInformational(operationContext, SR.TraceIgnoreAttribute, property.Name);
            return true;
        }
    
        return false;
    }
    

    【讨论】:

    • 这确实有效。我也尝试过使用私人套装,但也没有用。有没有使用不可变对象和 Azure 表的好模式?
    猜你喜欢
    • 2020-09-14
    • 2022-01-20
    • 1970-01-01
    • 2022-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-02
    • 1970-01-01
    相关资源
    最近更新 更多