Azure Functions 未在本地创建 Azure 表列

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

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

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 存储资源管理器中看不到该列。每次触发我的函数时,我都会看到一行。请帮助我理解为什么我看不到我的数据。

song0007 回答:Azure Functions 未在本地创建 Azure 表列

我相信您遇到这个问题是因为您的 MyProperty 没有公开的 setter。

请尝试更改这行代码:

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;
}
本文链接:https://www.f2er.com/389488.html

大家都在问