如何根据属性之一反序列化服务总线消息队列

时间:2020-06-03 08:30:03

标签: c# asp.net .net json

我有以下DTO:

public class ClientDocument
    {
        [System.ComponentModel.DataAnnotations.Key]
        [IsFilterable]
        public string Id { get; set; }

        public string IndexName { get; set; }

        [IsSearchable, IsFilterable]
        public string ClientCode { get; set; }

        [IsSearchable, IsFilterable]
        public string GoogleDriveLink { get; set; }

        [IsSearchable, IsFilterable]
        public string FileName { get; set; }

        [IsSearchable, IsFilterable]
        public string DocumentType { get; set; }

        [IsSearchable, IsFilterable]
        public DateTime CreatedDate { get; set; }
    }


   public class JobDocument
    {
        [System.ComponentModel.DataAnnotations.Key]
        [IsFilterable]
        public string Id { get; set; }

        public string IndexName { get; set; }

        [IsSearchable, IsFilterable]
        public string ClientCode { get; set; }

        [IsSearchable, IsFilterable]
        public string JobCode { get; set; }


        [IsSearchable, IsFilterable]
        public string GoogleDriveLink { get; set; }

        [IsSearchable, IsFilterable]
        public string FileName { get; set; }

        [IsSearchable, IsFilterable]
        public string DocumentType { get; set; }

        [IsSearchable, IsFilterable]
        public DateTime CreatedDate { get; set; }
    }


   public class BillCycleDocument
    {
        [System.ComponentModel.DataAnnotations.Key]
        [IsFilterable]
        public string Id { get; set; }

        public string IndexName { get; set; }

        [IsSearchable, IsFilterable]
        public string ClientCode { get; set; }

        [IsSearchable, IsFilterable]
        public string JobCode { get; set; }
        [IsSearchable, IsFilterable]
        public string BillCycleId { get; set; }

        [IsSearchable, IsFilterable]
        public string GoogleDriveLink { get; set; }

        [IsSearchable, IsFilterable]
        public string FileName { get; set; }

        [IsSearchable, IsFilterable]
        public string DocumentType { get; set; }

        [IsSearchable, IsFilterable]
        public DateTime CreatedDate { get; set; }
    }

在我的Web作业中,我将有一个队列触发器,在该触发器中,我将以以下格式接收消息,DTO非常相似,但不相同,并且取决于indexname属性,然后我需要将它们反序列化为正确的格式DTO。

将显示在队列中的json消息如下:

Client Documents
{
   IndexName: “idx-clientdocuments”
   ClientCode:
   GoogleDriveLink:
   FileName:
   DocumentType:
   CreatedDate: 
}


Index:
JobDocuments
{
   IndexName: “idx-jobdocuments”
   ClientCode:
   JobCode: 
   GoogleDriveLink:
   FileName: 
   DocumentType:
   CreatedDate :
}

Bill Cycle Documents: 
{
   IndexName: “idx-billcycledocuments”
   ClientCode:
   JobCode: 
   BillCycleId: 
   GoogleDriveLink:
   FileName: 
   DocumentType: 
   CreatedDate:
}

在我的代码中,我需要执行以下操作:

[FunctionName("MyFunctionToReceive")]
        public async Task Receive(
        [ServiceBusTrigger("%QueueToIndexDocumentsFrom%", IsSessionsEnabled = true)] Message requestMessage,
        ILogger logger,
        CancellationToken cancellationToken)
        {
            string messageBody = Encoding.UTF8.GetString(requestMessage.Body, 0, requestMessage.Body.Length);
            logger.LogInformation($"Message received at  {DateTime.UtcNow}, Body = @ObjectBody", messageBody);

            var requestObject = JsonConvert.DeserializeObject<ClientDocument>(messageBody);

            await _indexer.IndexClientDocument(requestObject).ConfigureAwait(false);
        }

上面的代码段的问题是我需要将类型名称反序列化为正确的对象类型,是否有更好的方法呢?

谢谢

0 个答案:

没有答案