Submit an Object to a WCF Service

0

I would like to know if there is any way to send an object to a WCF

service

the program you send is in WindowsForm c #

who receives this in asp.net c #

        OleDbConnection connection = MSAConnection.getConnection();

        string sql = @"SELECT 
                        cod as cod,
                        Format(fecha, 'Short Date') as fecha,
                        Format(hora, 'Short Time') as hora,
                        1 as Tipo
                FROM Control";

        OleDbDataAdapter d = new OleDbDataAdapter(sql, connection);
        DataTable dt = new DataTable();
        d.Fill(dt);
        dataGridView1.DataSource = dt;

        var Control= (from rw in dt.AsEnumerable()
                          select new DatosModelObj()
                          {
                              cod = Convert.ToInt32(rw["cod"]),
                              fecha = Convert.ToDateTime(rw["fecha"]),
                              hora = Convert.ToDateTime(rw["hora"]),
                              Tipo = Convert.ToInt32(rw["Tipo"])
                          }).ToList();

        WebRequest request = WebRequest.Create(@"http://localhost:77777/Services/Control.svc/Control/" + Control);
        request.Credentials = CredentialCache.DefaultCredentials;
        WebResponse response = request.GetResponse();
        Stream dataStream = response.GetResponseStream();
        StreamReader reader = new StreamReader(dataStream);
        reader.Close();
        response.Close();

MY SERVICE

namespace Registro.Services
{
    [ServiceContract]
    public interface IControl
    {
        [OperationContract]
        [WebGet(UriTemplate = "Control/{Control}",
                RequestFormat = WebMessageFormat.Json,
                ResponseFormat = WebMessageFormat.Json)]
        List<ControlModel> GetRegistro(ControlModel Control);
    }
}
    
asked by 20.03.2018 в 15:34
source

1 answer

0

Is it necessary to send the object through a Json ? since I notice that you have your GetRegistro tag "RequestFormat = WebMessageFormat.Json" . Either way, have you used the Service References ? I leave you a simple example since you can send defined objects in your WCF service (the DataContract ) from your WebForms application.

For the example, use two projects, one LittleClient (console app) and LittleService (WCF Service app).

LittleService contains a DataContract called Student (which is the object that was sent from the client) and a OperationContract (which is the method that we will consume in the client)

In the console application (LittleClient) we add a ServiceReference from our WCF service, this is right clicked on the project / Add / Service Rerefence.

If the service is in the same project you can press the discover button, if you can not add the link where your service is WCF

You will notice that now you have a folder called Service References and in it the reference to your WCF service.

Next we create the object that has the service defined (that is, the datacontract Student)

Finally to make the call to the method from your client (winforms, console, etc) you create an instance of the client that comes in the service references "LittleServiceReference.Service1Client" .

Finally the result is that the service receives the Student object when the client makes the call to the CreateStudent method:

This is the object that receives the Web service:

And here is the answer of the service in the client:

If the answer is useful, let me know if not also xd

Best regards.

    
answered by 22.03.2018 в 07:27