Here is one way you can do this. Have a message handler read the request and add a content header like this.
public class TypeDecidingHandler : DelegatingHandler
{
protected override async Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request, CancellationToken cancellationToken)
{
// Inspect the request here and determine the type to be used
request.Content.Headers.Add("X-Type", "SomeType");
return await base.SendAsync(request, cancellationToken);
}
}
Then, you can read this header from the formatter inside ReadFromStreamAsync
.
public override Task<object> ReadFromStreamAsync(
Type type, Stream readStream,
HttpContent content,
IFormatterLogger formatterLogger)
{
string typeName = content.Headers.GetValues("X-Type").First();
// rest of the code based on typeName
}