c# - Azure 函数 - 队列触发器将 DateTimeOffset 返回为 null

标签 c# .net azure azure-functions datetimeoffset

我正在运行一个 Azure Function,它连接到 API、获取一些数据并将其保存到 Azure 中的队列中。

然后我有另一个 Azure 函数作为该队列上的触发器运行,但我在 DateTimeOffset 方面遇到了一些问题?数据中对象之一的属性。所有其他属性均按预期工作。

简化的示例代码:

运行.csx:

using System;
using System.Collections.Generic;
using GoApi;
using GoApi.Reporting.AccountTransactions;

public static void Run(ReverseFactoringTransaction myQueueItem, TraceWriter log)
{
    log.Info(myQueueItem.Transaction.CreatedDate.ToString());
    log.Info(myQueueItem.Transaction.Text.ToString());
    log.Info(myQueueItem.Transaction.Amount.ToString());
    log.Info(myQueueItem.Transaction.Date.ToString());
    log.Info(myQueueItem.Partner.Name.ToString());
    log.Info(myQueueItem.Partner.IsActive.ToString());
    log.Info(myQueueItem.Partner.CreatedDate.ToString());
    log.Info(myQueueItem.LastSynchronizationDate.ToString());
    log.Info(myQueueItem.PostOnCreatedDate.ToString());
}

public class Auditable
{
    public string CreatedBy { get; set; }
    public string UpdatedBy { get; set; }
    public DateTime CreatedDate { get; set; }
    public DateTime UpdatedDate { get; set; }
}

public class Client : Auditable
{
    public Guid Id { get; set; }
    public long? CustomerId { get; set; }
    public string Name { get; set; }
    public string ClientKey { get; set; }
    public bool IsManuallyRecognized { get; set; }
    public bool IsActive { get; set; }
    public Guid PartnerId { get; set; }
    public Partner Partner { get; set; }
}

public class Partner : Auditable
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    public string PartnerKey { get; set; }
    public bool IsActive { get; set; }
    public List<Client> Clients { get; set; }
}

public class ReverseFactoringTransaction
{
    public Partner Partner { get; set; }
    public AccountTransaction Transaction { get; set; }
    public int ReverseAccountCode { get; set; }
    public DateTimeOffset LastSynchronizationDate { get; set; }
    public bool PostOnCreatedDate { get; set; }
}

项目.json:

{
    "frameworks":
    {
        "net46":
        {
            "dependencies":
            {
                "PowerOfficeGoSDK": "1.6.3"
            }
        }
    }
}

当我使用此测试输入时:

{
  "Partner": {
    "Id": "da462e01-d999-41d0-a71d-dbcf36ca219b",
    "Name": "Test Acme",
    "PartnerKey": "62504f06-1bee-4665-807c-6c918f2ac454",
    "IsActive": true,
    "CreatedBy": "sth",
    "UpdatedBy": "sth",
    "CreatedDate": "2017-10-18T07:40:16.3244273+00:00",
    "UpdatedDate": "2017-10-18T07:40:16.3244273+00:00"
  },
  "Transaction": {
    "Id": 4889211,
    "AccountCode": 1500,
    "Date": "2017-02-15T00:00:00",
    "VoucherNo": 14,
    "VoucherType": 4,
    "Text": "Invoice 1 for TestCompany Inc",
    "Description": "",
    "VatCode": "0",
    "VatAmount": 0.0,
    "VatRate": 0.0,
    "Amount": 380000.0,
    "CurrencyCode": "GBP",
    "CurrencyAmount": 380000.0,
    "VoucherImagesCount": 0,
    "LastChanged": "2017-10-17T15:31:14+00:00",
    "CreatedDate": "2017-10-17T15:31:14+00:00",
    "CustomerAccountNo": 10013,
    "VoucherDueDate": "2017-10-20T00:00:00",
    "VoucherCID": "141",
    "VoucherId": "95620904-623a-48b6-99a4-6316d893886b",
    "DocumentNo": "14",
    "CreatedFromImportJournalId": "1c9d60cf-9cc0-409e-9b3e-787461a0ae96",
    "IsCreatedFromEhf": false
  },
  "ReverseAccountCode": 1510,
  "LastSynchronizationDate": "2017-01-18T00:00:00+00:00",
  "PostOnCreatedDate": true,
  "$AzureWebJobsParentId": "765c4f56-0cdb-4d08-b9fa-8bc4a94e0499"
}

我得到这个作为函数的输出:

2017-10-18T08:05:50.391 Function started (Id=bb8f485c-9f33-4976-833f-4a1ac0840e22)
2017-10-18T08:05:50.391 
2017-10-18T08:05:50.391 Invoice 1 for TestCompany Inc
2017-10-18T08:05:50.391 380000.0
2017-10-18T08:05:50.391 2/15/2017 12:00:00 AM
2017-10-18T08:05:50.391 Test Acme
2017-10-18T08:05:50.391 True
2017-10-18T08:05:50.391 10/18/2017 7:40:16 AM
2017-10-18T08:05:50.391 1/18/2017 12:00:00 AM +00:00
2017-10-18T08:05:50.391 True
2017-10-18T08:05:50.391 Function completed (Success, Id=bb8f485c-9f33-4976-833f-4a1ac0840e22, Duration=1ms)

如您所见,DateTimeOffset? Transaction 对象上的 CreatedDate 显示为 null。但我完全不知道为什么。 有谁知道问题出在哪里吗?

最佳答案

AccountTransaction.CreatedDate内部 setter,因此在反序列化期间它将被忽略。

参见this question用于内部属性反序列化的描述。

如果您确实需要它,您可以接受队列项目作为字符串,然后使用自定义设置/解析器手动反序列化。

关于c# - Azure 函数 - 队列触发器将 DateTimeOffset 返回为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46887136/

相关文章:

.net - Rosetta 类似于 ASP.NET MVC Web 应用程序的本地化?

java - 使用 Java SDK、连接模式访问 Azure 服务总线

c# - dotnet core api 文件夹结构

c# - Json.NET MissingMemberHandling 设置

c# - 我的 Xpath 有什么问题吗?

c# - Entity Framework 不支持 x.ToString()!

c# - Visual Studio 2015 Convert.ToDouble 在尝试观看 lambda 表达式时出错

c#:将网页的html源读入字符串

azure - 如何在service-node-port-range默认范围之外的NodePort上启动kubernetes服务?

azure - 不使用应用程序测试 Azure 通知中心