Cannot serialize Member * because it is an interface

Asked

Viewed 137 times

0

Staff bumped into above error while trying to create a webservice and I’m having a hard time interpreting and treating him.

Detailing what I did:

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

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

I managed to return a XML with the data as expected.

Then in the same project I created a WebService "Webserviceusuario.asmx", copied the controller method for this [WebMethod] and tried to run it in order to test my webservice but I stumbled upon the title error. I’m learning kind of in practice and I couldn’t understand the error nor a way to correct it.

Below is the method:

 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.

  • Post the definition of Data.Usuario, please.

  • Try to hit a [Serializable] in class.

  • You haven’t solved it yet?

  • I’m researching here to see what can be, I tried the Serializable but he says it is not possible to use with this type of data, maybe I should return in another format

1 answer

0


From what I understand the problem WebMethod is not able to serialize an interface, i.e., it is not able to serialize the class coming directly from the Entity.

To fix the problem I created a class UsuarioWebMethod which will serve as an intermediary, thus the WebMethod return an instance of this class with 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;
  }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.