Jump to content

Passagem de dados WebService para FormApplication


pcosta94
 Share

Recommended Posts

Boa tarde, estou com um problema numa aplicaçao que consome um webservice criado localmente. Um dos metodos do webservice deve devolver uma lista mas quando esse metodo é chamado e atribuido a uma lista na aplicação essa lista fica com tamanho 0.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WeatherConsumer.WeatherReference;
using System.Diagnostics;

namespace WeatherConsumer
{
public partial class Form1 : Form
{
 public Form1()
 {
	 InitializeComponent();
	 Service1Client servico = new Service1Client();
	 List<Temperatura> temps = new List<Temperatura>();
	 temps = servico.getTemperaturas().ToList();
	 int count = temps.Count;
	 Debug.WriteLine(count);
	 //List<Humidade> hums = servico.getHumidades().ToList();
	 //List<Pressao> press = servico.getPressoes().ToList();

 }
 private void obterBtn_Click(object sender, EventArgs e)
 {
	 if(!pressureBox.Checked && !temperatureBox.Checked && !pressureBox.Checked )
	 {
		 MessageBox.Show("Non of the options were selected",
"Error");
	 }
	 else
	 {
		 //if(pressureBox.Checked)
	 }
 }





}
}

E a parte do webservice

using System.Data.SqlClient;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Diagnostics;
namespace WebServiceWeather
{
public class Service1 : IService1
{
 public string GetData(int value)
 {
	 return string.Format("You entered: {0}", value);
 }
 public CompositeType GetDataUsingDataContract(CompositeType composite)
 {
	 if (composite == null)
	 {
		 throw new ArgumentNullException("composite");
	 }
	 if (composite.BoolValue)
	 {
		 composite.StringValue += "Suffix";
	 }
	 return composite;
 }

 public List<Temperatura> getTemperaturas()
 {
	 List<Temperatura> temps = new List<Temperatura>();
 /*try
	 {
		 SqlConnection conn = null;
		 conn = new SqlConnection(@"Data Source=(localdb)\ProjectsV12;Initial Catalog=BDProdutos;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False");
		 conn.Open();
		 string sql = "SELECT * FROM Temperature";
		 SqlCommand cmd = new SqlCommand(sql, conn);
		 SqlDataReader reader = cmd.ExecuteReader();
		 if (cmd.ExecuteNonQuery() > 0)
		 {

			 while (reader.Read())
			 {
				 Temperatura temp = null;
				 temp.Data = (DateTime)reader.GetDateTime(1);
				 temp.Temperature = (float)reader["Value"];
				 temps.Add(temp);
				 Console.WriteLine(temp.Data);

			 }
		 }
		 conn.Close();
	 }
	 catch(Exception)
	 {

	 }*/
	 Temperatura tp1 = new Temperatura();
	 tp1.Data = new DateTime(2012,1,1);
	 tp1.Temperature = 12.5F;
	 Temperatura tp2 = new Temperatura();
	 tp2.Data = new DateTime(2012, 1, 1);
	 tp2.Temperature = 12.8F;
	 temps.Add(tp1);
	 temps.Add(tp2);
	 foreach (Temperatura item in temps )
	 {
		 Debug.WriteLine(item.Data.ToString() + " " + item.Temperature +"\n");
	 }
	 return temps;

 }

E classe é esta

[DataContract]
public class Temperatura
{
 float temperature = 0;
 DateTime date;
 [DataMember]
 public float Temperature
 {
	 get { return temperature; }
	 set { temperature = value; }
 }
 [DataMember]
 public DateTime Data
 {
	 get { return date; }
	 set { date = value; }
 }
Link to comment
Share on other sites

Boas,

Podes fazer debug no código do webservice?

Verifica se o Objecto temps, quando faz o retorno, possui elementos. Se sim, se tem os 2 elementos que inseres.

BTW: Verifica também se tens o método getTemperaturas() declarado na Interface do serviço.

Cumps.

Edited by Guest
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.