Send List of objects to my wcf web service

0

Good people,

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

So far I have the following status implemented:

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)' can not be converted to   'MyListofClients'.

I think the problem has to do with the list that must be of type ' MyListofClients '.

Dim list As New List(Of APICS.MyListofClients)

However I can not figure out how to send the list this way

Any tips ??

    
asked by anonymous 15.07.2016 / 19:10

1 answer

0

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

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

If you configure this way, your client application would not even allow you to send a list of strings instead of a list of clients.

Now try changing 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 code above may have some basic (syntax) errors, but its logic remains the same.

    
15.07.2016 / 19:37