Question:
I'm trying to use the Web Api and getting this error when trying to serialize an array.
Type 'HT.Data.Client' with data contract name 'Client: http://schemas.datacontract.org/2004/07/HT.Data ' is not expected. Consider using a DataContractResolver if you are using a DataContractSerializer or add any unknown types statically to the list of known types – for example, using the KnownTypeAttribute attribute or adding them to the list of known types passed to the serializer.
My class that is in a different web API project:
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 my API initialization:
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 to configure so that the class is known/expected?
Answer:
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);