0

How to make it deserialize to original type? WebJobs SDK allows to specify the type of a POCO object and add QueueTriggerAttribute near it to make it function (the doc). Now it deserializes not to an original type but some other (from run to run it varies). Here is the code:

public class PocoCommandA { public string Prop { get; set; } }

public class PocoCommandB { public string Prop { get; set; } }

public static void Func1([QueueTrigger("myqueue")] PocoCommandA aCommand)
{... }

public static void Func2([QueueTrigger("myqueue")] PocoCommandB bCommand)
{ ... }

And it calls Fun2 (or some other) when

var a = new PocoCommandA();
var cloudQueueMessage = new CloudQueueMessage(JsonConvert.SerializeObject(a));

await queue.AddMessageAsync(cloudQueueMessage);
Joy Wang
  • 39,905
  • 3
  • 30
  • 54
Artyom
  • 3,507
  • 2
  • 34
  • 67
  • Did you mean you want to deserialize messages in queue to PocoCommandA and PocoCommandB type? If so, it seems that there are many messages in queue. And their field name are the same. Only the class name are different. So we can not determine which message belongs to which class. – Janley Zhang Mar 22 '18 at 08:44

2 Answers2

1

If PocoCommandA and PocoCommandB 2 classes has the same properties that it can't know which message belong to specific object. As it could be any of the 2 Objects. Generally, we don't recommond that 2 classes have the same properties.

If PocoCommandA and PocoCommandB 2 classes have the different properties, then if we send the Poco message to queue, then corresponding function will be triggered. And we no need to deserialize it. We could use the aCommand or bCommand directly.

Tom Sun - MSFT
  • 24,161
  • 3
  • 30
  • 47
0

Fixed by manually handling serialization / deserialization. This entails that handling of the messages are done using switch (could be Reflection of course).

Deserialize:

var message = (IMessage) JsonConvert.DeserializeObject(msg, 
                new JsonSerializerSettings() {
                        TypeNameHandling = TypeNameHandling.Objects,
                        DateParseHandling = DateParseHandling.DateTimeOffset
} );

Serialize:

new CloudQueueMessage(JsonConvert.SerializeObject(msg, /* the same */));

WebJobs SDK place where it is handled is here. But it is not configurable. Issue created there.

Artyom
  • 3,507
  • 2
  • 34
  • 67