0

I am really new to Dependency Injection and I am trying to have Json.Net deserialize a class the contains members that are Interfaces. I am receiving the following error.

"Could not create an instance of type IBioMetricCodeAttributes. Type is an interface or abstract class and cannot be instantiated."

The following is the class I am attempting to have Json.Net deserialize.

public sealed class EntryContract : IEntryContract
{
    [JsonIgnore] public int EntryId { get; set; }
    public string EntryTypeKey { get; set; }
    public string ApplicationKey { get; set; }
    public string PersonKey { get; set; }
    public string ScreeningDateTime { get; set; }
    [JsonIgnore] public int ApplicationId { get; set; }
    [JsonIgnore] public int EntryTypeId { get; set; }
    [JsonIgnore] public int? HealthProfileId { get; set; }
    [JsonIgnore] public Guid ChangeSetId { get; set; }
    [JsonIgnore] public DateTime EntryDate { get; set; }
    [JsonIgnore] public DateTime SavedDate { get; set; }
    [JsonIgnore] public string ExternalId { get; set; }
    [JsonIgnore] public int OriginatingApplicationId { get; set; }
    public List<IBioMetricCodeAttributes> CodeAttributes { get; set; }
    public List<IBioMetricCountAttributes> CountAttributes { get; set; }
    public List<IBioMetricDateAttributes> DateAttributes { get; set; }
    public List<IBioMetricEnumAttributes> EnumAttributes { get; set; }
    public List<IBioMetricFreeFormAttributes> FreeFormAttributes { get; set; }
    public List<IBioMetricMeasurementAttributes> MeasurementAttributes { get; set; }
    [JsonIgnore]
    public List<IBioMetricXmlAttributes> XmlAttributes { get; set; }
}

My Ninject module is being loaded. It is as follows:

public class NexusModule : NinjectModule
{
    public override void Load()
    {
        Bind<IBioMetricCodeAttributes>().To<BioMetricCodeAttributes>();
        Bind<IBioMetricCountAttributes>().To<BioMetricCountAttributes>();
        Bind<IBioMetricDateAttributes>().To<BioMetricDateAttributes>();
        Bind<IBioMetricEnumAttributes>().To<BioMetricEnumAttributes>();
        Bind<IBioMetricFreeFormAttributes>().To<BioMetricFreeFormAttributes>();
        Bind<IBioMetricMeasurementAttributes>().To<BioMetricMeasurementAttributes>();
        Bind<IBioMetricXmlAttributes>().To<BioMetricXmlAttributes>();
    }
}

The error is being thrown at the following line of code.

return JsonConvert.DeserializeObject<List<EntryContract>>(GetBiometricData(nexusId, entryTypes));

I am using Newtonsoft.Json version 6.0 and Ninject version 3.2.2.

Any help on this would be appreciated.

marocano1
  • 87
  • 3
  • 8
  • Did you try removing "sealed" ? – Spock Aug 13 '14 at 00:14
  • Do you need ninject to create the instances? If not, see http://stackoverflow.com/questions/15880574/json-net-how-to-deserialize-collection-of-interface-instances otherwise see https://json.codeplex.com/discussions/363458 – BatteryBackupUnit Aug 13 '14 at 06:41
  • I will look into the DefaultContractResolver option. I was headed down that path but I have never done it before, so, I was hesitant. Thanks! – marocano1 Aug 14 '14 at 12:40

0 Answers0