question

PhilippBuchberger-7556 avatar image
0 Votes"
PhilippBuchberger-7556 asked LanHuang-MSFT commented

C# Webservice

I have got my Webservice it should read Data from a Database and Transfer it to My WPF Application but it doesn't display any data.

IService.cs
namespace WcfService2
{

     [ServiceContract]
     public interface IService1
     {
    
         [OperationContract]
         string GetData(int value);
    
         [OperationContract]
         CompositeType GetDataUsingDataContract(CompositeType composite);
    
         // TODO: Hier Dienstvorgänge hinzufügen
         [OperationContract]
         WeatherContract GetWeatherData(WeatherContract weather);
     }
    
    
    
     [DataContract]
     public class CompositeType
     {
         bool boolValue = true;
         string stringValue = "Hello ";
    
         [DataMember]
         public bool BoolValue
         {
             get { return boolValue; }
             set { boolValue = value; }
         }
    
         [DataMember]
         public string StringValue
         {
             get { return stringValue; }
             set { stringValue = value; }
         }
     }
    
     [DataContract]
     public class WeatherContract
     {
         [DataMember]
         public DateTime start
         {
             set; get;
         }
         [DataMember]
         public DateTime end
         {
             set; get;
         }
    
         [DataMember]
         public List<WeatherDataDTO> weatherData
         {
             set; get;
         }
    
     }
    
     public partial class WeatherDataDTO
     {
         public int Id { get; set; }
         public Nullable<System.DateTime> Datum { get; set; }
         public Nullable<System.TimeSpan> Zeit { get; set; }
         public Nullable<double> Temperatur { get; set; }
         public Nullable<int> Luftdruck { get; set; }
         public Nullable<int> Regen { get; set; }
         public Nullable<double> Wind { get; set; }
         public Nullable<int> Richtung { get; set; }
         public Nullable<int> Feuchtigkeit { get; set; }
     }
 }

Service.svc

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 WeatherContract GetWeatherData(WeatherContract weather)
     {
         //WeatherDataEntities db = new WeatherDataEntities();

         WeatherDataDAO.getWeatherData(weather.start,weather.end);

         List<WeatherData> weatherDatas = new List<WeatherData>();
         List<WeatherDataDTO> dtos = new List<WeatherDataDTO>();

         foreach(WeatherData data in weatherDatas)
         {
             WeatherDataDTO weatherDataDTO = new WeatherDataDTO();

             weatherDataDTO.Id = data.Id;
             weatherDataDTO.Datum = data.Datum.Value;
             weatherDataDTO.Zeit = data.Zeit.Value;
             weatherDataDTO.Temperatur = data.Temperatur.Value;
             weatherDataDTO.Luftdruck = data.Luftdruck.Value;
             weatherDataDTO.Regen = data.Regen.Value;
             weatherDataDTO.Wind = data.Wind.Value;
             weatherDataDTO.Richtung = data.Richtung.Value;
             weatherDataDTO.Feuchtigkeit = data.Feuchtigkeit.Value;

             dtos.Add(weatherDataDTO);
         }

         weather.weatherData = dtos;

         return weather;

     }
 }  

}

WeatherDataDAO

 public class WeatherDataDAO
     {
         private static WeatherDataEntities weatherData = new WeatherDataEntities();
    
         public static List<WeatherData> getWeatherData(DateTime start, DateTime end)
         {
             var query = from weatherData in weatherData.WeatherData
                         where weatherData.Datum <= end && weatherData.Datum >= start
                         orderby weatherData.Id
                         select weatherData;
    
             return query.ToList();
         }
     }

My WeatherData DAO CLass in the WPF Application

 class WeatherDataDAO
     {
         public static ServiceReference1.Service1Client client = new ServiceReference1.Service1Client();
    
         public static ServiceReference1.WeatherDataDTO[] GetWeatherDatas(DateTime begin, DateTime end, int page)
         {
             var contract = new ServiceReference1.TemperatureContract();
             contract.BeginValue = begin;
             contract.EndValue = end;
             contract.PageValue = page;
    
    
             return client.GetTemperatures(contract).WeatherDataValues;
         }
    
         public static List<double> GetTemperatures(DateTime begin, DateTime end, int page)
         {
             var contract = new ServiceReference1.TemperatureContract();
             contract.BeginValue = begin;
             contract.EndValue = end;
             contract.PageValue = page;
    
             List<double> temperatures = new List<double>();
             foreach (ServiceReference1.WeatherDataDTO weatherData in client.GetTemperatures(contract).WeatherDataValues)
                 temperatures.Add(weatherData.Temperatur);
    
             return temperatures;
         }
     }

MAinWindows.xaml

 private void getButton_Click(object sender, RoutedEventArgs e)
         {
             if (!beginDatePickewr.SelectedDate.HasValue || !endDatePicker.SelectedDate.HasValue)
                 return;
    
             int page = 0;
             try
             {
                 page = int.Parse(pageTextBox.Text);
             }
             catch (Exception)
             {
    
             }
    
             temperatreDataGrid.ItemsSource = WeatherDataDAO.GetWeatherDatas(beginDatePickewr.SelectedDate.Value, endDatePicker.SelectedDate.Value, page);
         }


dotnet-csharpwindows-wpfwindows-wcf
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @PhilippBuchberger-7556,
Is there any error message? I suggest you breakpoint to check.
https://docs.microsoft.com/en-us/visualstudio/debugger/using-breakpoints?view=vs-2022
Best regards,
Lan Huang

0 Votes 0 ·

0 Answers