Azure Functions 不在本地创建 Azure 表列

Azure Functions not creating Azure Tables Columns Locally

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

我的触发器是队列接收到的新负载。我的触发器工作正常,当我将数据写入 Azure 存储 table 时,我可以看到 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 存储资源管理器中看不到该列。每次触发我的功能时,我都会看到一行。请帮助我理解为什么我看不到我的数据。

我相信你 运行 正在解决这个问题是因为你没有 public setter 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(见代码中的“强制publicgetter/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;
}