Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

730
Visualizações
Deserialization of Inteface types not supported in API Request

I am getting this error message System.NotSupportedException: Deserialization of interface types is not supported.

while calling an API which taken request as per below.

example

   [HttpPost]
   public IActionResult AddSomeData(MyClass MyClass)
   {
       return Ok();
   }
   
   public class MyClass : MyInterface
   {
       return Ok();
   }
   
   public interface MyInterface 
   {
        public string SomeOtherProperties { get; set; }

        public MyInterface Property { get; set; } //Or reference of any other interface
   }

I have tried examples given online, Like creating Concrete Class and adding attribute over the property. But it is not helping out.

As workaround I have removed public MyInterface Property { get; set; } and added public MyClass Property { get; set; }

Any better solution for this?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

The problem with deserializing to an interface is that you lose information

public class MyClass : MyInterface
{
    public string SomeOtherProperties { get; set; }
    public MyInterface Property { get; set; }
    public string importantString { get; set; } = "Don't lose me please";
}

public interface MyInterface
{
    string SomeOtherProperties { get; set; }
    MyInterface Property { get; set; }
}

Take the above example, trying to deserialize to the interface would involve telling the deserializer to only know about the properties on the interface - and any other fields your end user is supplying getting dropped entirely. On top of that you might have methods on the concrete class that support the interface properties that need to exist.

There are some options involving reflection that you could use if you need the API to support multiple implementations of an interface, but the question is why you need the interface in the first place? An API is supposed to cleanly define what needs to be supplied by the user, abstracting that with an interface that needs to be implemented by some mystery concrete class seems like a bad idea.

Edit: if all you need is the shared information and there isn't anything fancy - you could just use MyInterface as a standard class

public class MyClass : MyInterface
{
    public string nonImportantString { get; set; } = "lose me";
}
public class MyInterface
{
    public  string SomeOtherProperties { get; set; }
    public  MyInterface Property { get; set; }
}

In doing so your api is clean, and you can deserialize fine

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda