0

Request provides a batch of messages with a different signature and provides 'metadata' of this message as well. So, example is pretty simple:

{
    "SomeProp": 1,
    "Messages": [{
            "Type": "SomeMessage1",
            "Body": {
                "Id": 1,
                "Action": "Delete"
            }
        },
        {
            "Type": "AnotherMessage",
            "Body": {
                "Amount": 0.001,
                "Data": "data",
                "Ok": "True"
            }
        }
    ]
}

So the task is to bind each 'Messages[i].Body' expression. The only solution I see so far is to parse the whole request body string, try to deserialize all 'common' properties. And then do everything manually.

I've tried to create my own IModelBinder and then, extracted values from Messages[i].Type, make partial deserialization of request, but default ValuePropviders return null for model and it's parts.

Unfortunately, I have no more ideas.

dotFive
  • 363
  • 3
  • 19
  • You can use one class that has that properties and extract your needed data from it. Like: ```public Task Method(ModelClass class)```. Model class has exact properties you need. Access to data like ```class.Messages[i]``` – Sajad Afaghiy Oct 03 '18 at 07:33
  • I don't understand your question exactly.. You can write your own Model Binder and parse your object in the way you like? I think you have different object types with lots of properties, so you could use NewtonSoft.Json JObject to C# object to bind them automatically. Provide more details for more help please. Happy coding. – P. Grote Oct 03 '18 at 07:38
  • @SajadAfaghiy: I was thinking about this approach, but this complicates things when messages become biffer, or there will be lots of messages with different signature. – dotFive Oct 03 '18 at 07:50
  • @P.Grote: first of all, it tied on the data format and I would like to avoid it. the second argument that any serializer cannot deserialize abstract members without knowledge what is the type. Message bodies are completely different and binder will just return null. – dotFive Oct 03 '18 at 07:56
  • So what do you need at the end, only the whole json object or a strongly typed c# object differ by your Type property? I am afraid that you don't get around it, to do this manually. – P. Grote Oct 03 '18 at 08:19

0 Answers0