My code:
var configuration = await Task.Factory.StartNew(() =>
JsonConvert.DeserializeObject<IConfigurationService>(json));
fails at run-time with the error:
Newtonsoft.Json.JsonSerializationException: 'Could not create an instance of type .....IConfigurationService. Type is an interface or abstract class and cannot be instantiated. Path 'MyProperty', line 2, position 25.'
If I replace the IConfigurationService with the concrete version (that implements IConfigurationService) like so:
var configuration = await Task.Factory.StartNew(() =>
JsonConvert.DeserializeObject<ConfigurationService>(json));
It works fine. But that defeats the object of using an interface. How can I get the deserialise to work with the interface?
Assume IConfigurationService is this simple:
public interface IConfigurationService
{
string MyProperty{ get; set; }
}
I have looked on the Web and it seems I need to use a JsonConverter, however all of the examples relate to MyProperty being an Interface.