Send List of Objects to my wcf web service

Asked

Viewed 255 times

1

Good staff,

I need to create a Web service WCF which receives a list of objects and inserts it into a database postgresql. To test created a client app Vb.net, but something is not going as expected.

So far I have implemented the following condition:

Iservice.Cs

  public interface IService1
   {
    [OperationContract]
    int InsertClients(MyListofClients clients);
   }

  [DataContract]
  public class MyListofClients
  {

    [DataMember]
    List<Client> Clients { get; set; }

  }

[DataContract]
public class Client
{
    [DataMember]
    public string clientId { get; set; }

    [DataMember]
    public string ClientName { get; set; }

    [DataMember]
    public List<Client> Clients { get; set; }
}
}  

Servic1.svc

 public int InsertClients(MyListofClients clients)
  {
    int res, result;
    using (NpgsqlConnection conn = new NpgsqlConnection(connStringFarm))
    {
        conn.Open();
        List<MyListofClients> firstStringList = new       List<MyListofClients>();
        string cmdStr = String.Format("Insert Into table (x1,x2)" +
                                       " VALUES(@x1,@x2)");

        foreach (var item in firstStringList)
        {
            NpgsqlCommand cmd = new NpgsqlCommand(cmdStr, conn);
            result = cmd.ExecuteNonQuery();
        }

        conn.Close();
        return 0;
       }

App Client

  Private Sub Button1_Click(sender As Object, e As EventArgs) Handles          Button1.Click
    Dim db_comm As New NpgsqlCommand
    Dim Reader As NpgsqlDataReader


    Dim query As String = "Select code1,name1 from clientes"
    db_comm.CommandText = query
    db_comm.Connection = conn
    conn.open
    Reader = db_comm.ExecuteReader

    Dim list As New List(Of String)

    While Reader.Read
            list.Add(Reader.GetString("code1"))
            list.Add(Reader.GetString("name1"))
    End While

    Dim API As APICS.Service1Client = New APICS.Service1Client()
    API.InsertClientsAsync(list)
End Sub

ERROR Value of type 'List(Of String)' cannot be converted to 'Mylistofclients'.

I think the problem has to do with list that has to be like 'Mylistofclients'.

Dim list As New List(Of APICS.MyListofClients)

however I can not understand how I should send the list this way

Any hint??

1 answer

0

CS, I advise you to generate the Webservice proxy using the "Add Service Reference..." option that appears when right-clicking on "References".

try to use a "Metadata Exchange" endpoint when downloading service information, this approach is preferable to using the good old ?wsdl.

if configured this way, your client application would not even allow sending a list of strings instead of a client list.

now try to change your call to the following.:

Dim list As New List(Of APICS.Client)

While Reader.Read
        Dim cliente As APICS.Client = New APICS.Client()
        cliente.clientId = Reader.GetString("code1"))
        cliente.ClientName = Reader.GetString("name1"))
        list.Add(cliente)
End While

remember that I am not a VB.NET programmer, so the above code may present some basic (syntax) errors, but its logic remains the same.

Browser other questions tagged

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