How to send an object to an Asp.NET C#webservice?

Asked

Viewed 875 times

1

I need to send an object from a class that I created to a webservice, but when I call the method passing the object it to the types are incompatible. I tried to receive the object as Object, but it gives an error when generating XML document. How can I send this object?

Example: User:

[Serializable]
public class Usuario 
{ 
   String _Nome; 

   public String Nome { 
      get { return _Nome;} 
      set { _Nome = value; } 
   } 
} 

A method that passes an object to my webservice as follows:

{
... 
WS.InformarNome(usuario); 
}

And the webservice receives the user:

public void InformarNome(Usuario u) { ... }
  • 6

    Could provide a code example of what was done?

  • I have a User class: [Serializable] public class Usuario { String _Name; public String Name { get { Return _Name;} set { _Name = value; } } } A method that passes an object to my webservice as follows: ... WS.Informname(user); And the webservice receives the user: ... public void Informname(User u) { ... }

  • 2

    The best is to edit your question and put the code, it is more readable. http://answall.com/posts/45143/edit

  • Edited, thank you very much!

  • 1

    Following the code you posted no error happened to me. The user you are ordering on ws.InformarNome(usuario) is a User object or an Object?

  • Type Same user

Show 1 more comment

1 answer

1


Use Json, let the method receive a string that in this case will be the object and then when receiving this serialized object, just use the proper api of . net to convert to object.

public string Cadastro(string json)
        {
            Aluno aluno= JsonConvert.DeserializeObject<Aluno>(aluno);
        }

Browser other questions tagged

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