Type not expected when serializing web API

3

I'm trying to use the Api web and getting this error when trying to serialize an array.

  

The type 'HT.Data.Client' with data contract name 'Client: link 'is not expected. Consider using a DataContractResolver if you are using a DataContractSerializer or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to the serializer.

My class that stays in a project other than the web API:

namespace HT.Data
{
    [DataContract]
    public partial class Client
    {
        [Key]
        [DataMember(Order = 1, IsRequired = true)]
        [Required(ErrorMessage = "Campo obrigatorio")] 
        [MaxLength(128, ErrorMessage = "Tamanho maximo do campo: 128 caracteres. ")]
        public string Id { get; set; } // Id (Primary key)


        [DataMember(Order = 2, IsRequired = true)]
        [Required(ErrorMessage = "Campo obrigatorio")] 
        public string Secret { get; set; } // Secret


        [DataMember(Order = 3, IsRequired = true)]
        [Required(ErrorMessage = "Campo obrigatorio")] 
        [MaxLength(100, ErrorMessage = "Tamanho maximo do campo: 100 caracteres. ")]
        public string Name { get; set; } // Name


        [DataMember(Order = 4, IsRequired = true)]
        [Required(ErrorMessage = "Campo obrigatorio")] 
        public int ApplicationType { get; set; } // ApplicationType


        [DataMember(Order = 5, IsRequired = true)]
        [Required(ErrorMessage = "Campo obrigatorio")] 
        public bool Active { get; set; } // Active


        [DataMember(Order = 6, IsRequired = true)]
        [Required(ErrorMessage = "Campo obrigatorio")] 
        public int RefreshTokenLifeTime { get; set; } // RefreshTokenLifeTime


        [DataMember(Order = 7, IsRequired = false)]
        [MaxLength(100, ErrorMessage = "Tamanho maximo do campo: 100 caracteres. ")]
        public string AllowedOrigin { get; set; } // AllowedOrigin


        public Client()
        {
            InitializePartial();
        }

        partial void InitializePartial();
    }

My controller:

namespace App.ResourceServer.Controllers
{
    //[Authorize]
    [RoutePrefix("api/sample")]
    public class SampleController : ApiController
    {    
        private UnitOfWork db = new UnitOfWork();

        [Route("")]
        public IEnumerable<object> Get()
        {    
            Client[] Result = db.ClientRepository.GetAllClients().ToArray(); 
            return Result;
        }
    }
}

And initializing my API:

namespace AngularJSAuthentication.ResourceServer.App_Start
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Web API routes
            config.MapHttpAttributeRoutes();

            var jsonFormatter = config.Formatters.OfType<JsonMediaTypeFormatter>().First();
            jsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
            jsonFormatter.SerializerSettings.ContractResolver.ResolveContract(typeof(Client));

        }
    }
}

How do I configure the class to be known / expected?

    
asked by anonymous 07.11.2016 / 18:31

2 answers

1

I solved this error by adding in the Global.asax.cs file, Application_Start method

        //configura retorno json
        GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings
.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
        GlobalConfiguration.Configuration.Formatters
            .Remove(GlobalConfiguration.Configuration.Formatters.XmlFormatter);
    
20.02.2017 / 20:58
0

How I would solve:

  • Create a test project;
  • Instantiate your class with dummy values;
  • Try to serialize using the same serializer as your main application EX: If it is NewtonSoft, just use JsonConvert.SerializeObject (obj);

  • You'll probably have the same problem, just inspect the error and identify which property it's claiming.

  • 05.01.2017 / 04:16