Can not serialize member * because it is an interface

0

Personally I ran into the above error when trying to create a webservice and I'm having difficulty interpreting and treating it.

Detailing what I did:

Within a InpartSaude.WebApi.Api project in one of my controllers I have a method to get data from a user, when accessed by the browser:

http://localhost:24083/InpartSaudeApi/Usuario/ObterUsuario?login=kk&senha=kkk

I was able to return a XML with the data as expected.

Then in the same project I created a WebService "WebServiceUsuario.asmx", I copied the controller method to this [WebMethod] and tried to execute it in order to test my webservice but I came across the title error . I am learning rather than in practice and could not understand the error and not a way to correct it.

The following is the method below:

 public class WebServiceUsuario : System.Web.Services.WebService
    {
        [WebMethod]
        public List<Data.Usuario> ObterUsuario(string login, string senha)
        {
            using (InpartSaudeEntities db = new InpartSaudeEntities()) {

                return (from x in db.Usuario
                             from y in db.UsuarioDistribuidor.Where(a => a.idUsuario == x.idUsuario).DefaultIfEmpty()
                             from z in db.Distribuidor.Where(b => b.idDistribuidor == y.idDistribuidor).DefaultIfEmpty()
                             where x.nmLogin == login
                                  && x.nmSenha == senha
                                  && x.blAtivo == 1
                                  && x.blBloqueado == false
                             select x
                       ).ToList().Select(x => new Usuario {
                           idUsuario = x.idUsuario,
                           cdTipoUsuario = x.cdTipoUsuario,
                           idGrupoCliente = x.idGrupoCliente,
                           idPerfil = x.idPerfil,
                           nmEmail = x.nmEmail,
                           nmLogin = x.nmLogin,
                           nmUsuario = x.nmUsuario,
                           idDistribuidor = x.idDistribuidor
                       }
                    ).ToList();


            }
        }
    }

My class Data.Usuario comes from edmx , that is, it was automatically generated by EF .

    
asked by anonymous 24.01.2018 / 20:50

1 answer

0

As far as I understand the problem, WebMethod is not able to serialize an interface, that is, it is not able to serialize the class coming directly from Entity .

To correct the problem I created a class UsuarioWebMethod that will serve as an intermediary, so WebMethod will return an instance of that class with the database data.

public class UsuarioWebMethod {
  public int? Id { get; set; }
  public String Nome { get; set; }
  public int? IdPerfil { get; set; }
  public String Login { get; set; }
  public String Email { get; set; }
  public String Senha { get; set; }
  public String TipoUsuario { get; set; }
  public int? IdGrupoCliente { get; set; }
  public int? IdDistribuidor { get; set; }
}

[WebMethod]
public List<UsuarioWebMethod> ObterUsuario(string login, string senha)
{
   using (InpartSaudeEntities db = new InpartSaudeEntities()) {
   var usuarios = (from x in db.Usuario
       from y in db.UsuarioDistribuidor.Where(a => a.idUsuario == x.idUsuario).DefaultIfEmpty()
       from z in db.Distribuidor.Where(b => b.idDistribuidor == y.idDistribuidor).DefaultIfEmpty()
       where x.nmLogin == login
         && x.nmSenha == senha
         && x.blAtivo == 1
         && x.blBloqueado == false
       select x
       ).ToList().Select(x => new UsuarioWebMethod {
         Id = x.idUsuario,
         TipoUsuario = x.cdTipoUsuario,
         IdGrupoCliente = x.idGrupoCliente,
         IdPerfil = x.idPerfil,
         Email = x.nmEmail,
         Login = x.nmLogin,
         Nome = x.nmUsuario,
         IdDistribuidor = x.idDistribuidor
      }).ToList();

    return usuarios;
  }
}
    
25.01.2018 / 14:03