微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Azure Functions 未在本地创建 Azure 表列

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

解决方法

我相信您遇到这个问题是因为您的 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;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。