使用BrokeredMessage从Azure Service Bus队列(v1)反序列化强类型对象 [英] Deserialize strongly typed object from Azure Service Bus Queue (v1) using BrokeredMessage

查看:119
本文介绍了使用BrokeredMessage从Azure Service Bus队列(v1)反序列化强类型对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无论出于何种原因,我似乎都无法弄清楚如何将对象从队列中拉出并反序列化为放置在其中的对象( AccountEventDTO ).

for whatever reason I can't seem to figure out how to pull my object out of my queue and deserialize it back into what it was placed into it as (An AccountEventDTO).

Azure函数成功将对象放入队列:

[FunctionName("AccountCreatedHook")]
public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "post", Route = null)]HttpRequestMessage req,
    TraceWriter log, [ServiceBus("topic-name", Connection = "BusConnectionString", EntityType = Microsoft.Azure.WebJobs.ServiceBus.EntityType.Topic)] IAsyncCollector<BrokeredMessage> accountCreatedTopic)
{
    var accountEvent = await req.Content.ReadAsAsync<AccountEventDTO>();

    if (accountEvent != null && accountEvent.Name != null)
    {
        // Serialization
        var bytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(accountEvent));
        var memoryStream = new MemoryStream(bytes, writable: false);
        var message = new BrokeredMessage(memoryStream) { SessionId = Guid.NewGuid().ToString() };

        await accountCreatedTopic.AddAsync(message);
        return req.CreateResponse(HttpStatusCode.OK, "Account successfully added to topic.");
    }

    return req.CreateResponse(HttpStatusCode.BadRequest, "Account was not formed well.");
}

Azure函数从队列中拉出对象:

[FunctionName("AccountCreatedSubscriber")]
public static void Run([ServiceBusTrigger("topic-name", "license-keys", Connection = "BusConnectionString")]BrokeredMessage accountEvent, ILogger log)
{
    // ERROR on this line during deserialization
    var account = accountEvent.GetBody<AccountEventDTO>();

    var accountAddedEvent = Mapper.Map<AccountEventDTO, AccountAddedEvent>(account);
    _accountHandler.Handle(accountAddedEvent);
    GenericLogger.AccountLogging(log, accountAddedEvent);
}

错误消息:

AccountEventDTO:

public class AccountEventDTO : IAccountEvent
{
    public string Name { get; set; }
    public string SugarId { get; set; }
    public string AccountSubTypeRaw { get; set; }
    public AccountType AccountType { get; set; } = AccountType.Customer;
    public AccountSubType? AccountSubType { get; set; } = null;
    public string Phone { get; set; }
    public string PhoneAlternate { get; set; }
    public string BillingAddressCity { get; set; }
    public string BillingAddressCountry { get; set; }
    public string BillingAddressPostalCode { get; set; }
    public string BillingAddressState { get; set; }
    public string BillingAddressStreet { get; set; }
    public string ShippingAddressCity { get; set; }
    public string ShippingAddressCountry { get; set; }
    public string ShippingAddressPostalCode { get; set; }
    public string ShippingAddressState { get; set; }
    public string ShippingAddressStreet { get; set; }
    public string Website { get; set; }
}

推荐答案

最终通过更改我在发送方对消息进行序列化的方式以及在接收方将其拉低的方式来解决此问题.

Ended up solving this by altering the way I was serializing my message on the send side and how I was pulling it down on the receiving side.

发送序列号:

var jsonString = JsonConvert.SerializeObject(accountEvent);
var message = new BrokeredMessage(jsonString);
message.SessionId = Guid.NewGuid().ToString();
message.ContentType = "application/json";

接收反序列化:

var content = accountEvent.GetBody<string>();
var account = JsonConvert.DeserializeObject<AccountEventDTO>(content);

这篇关于使用BrokeredMessage从Azure Service Bus队列(v1)反序列化强类型对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆