2

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 function successfully placing object into queue:

[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 function pulling object from queue:

[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);
}

Error message:

Deserialization Error

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; }
}
2
  • Do you need to set the sessionid ? I am asking it becasue you can avoid manipulating brokeredmessage and directly work with your POCO
    – Thomas
    Oct 3, 2018 at 7:19
  • Yes. We are relying on FIFO for our topic and have to have a sessionid in order to insure that.
    – tokyo0709
    Oct 3, 2018 at 15:17

2 Answers 2

2

You're using BrokeredMessage (old Azure Service Bus client for .NET, WindowsAzure.ServiceBus). When message is sent as memory stream, it has to be received and deserialized using the same approach. GetBody<T> will work if you construct BrokeredMessage passing in an object of type T.

Note: the next generation client (Microsoft.Azure.ServiceBus) only works raw with byte array (memory stream for the old client). If this is a new project, recommend to stick with that approach rather than serialized types. More info is available in a GitHub issue here.

2
  • I totally agree, there were some reasons I had to stick with v1 for the time being but your answer pointed me in the right direction thank you.
    – tokyo0709
    Oct 2, 2018 at 16:39
  • 1
    Design rationale: Built in POCO serialization support was removed in the latest Microsoft.Azure.ServiceBus. This was because "while this hidden serialization magic is convenient, applications should take explicit control of object serialization and turn their object graphs into streams before including them into a message, and do the reverse on the receiver side. This yields interoperable results." learn.microsoft.com/en-us/azure/service-bus-messaging/…
    – Nick Evans
    Jan 20, 2019 at 0:40
2

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.

Sending Serialization:

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

Receiving Deserialization:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.