WCF plus WPF

lookforme 21 Reputation points
2022-05-22T19:45:34.537+00:00

IService:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace WeatherDataWCF
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
[ServiceContract]
public interface IService1
{

     // TODO: Add your service operations here

     [OperationContract]

     List<WeatherDataDTO> GetWeatherData(RangeFilter rangeFilter);
 }

 [DataContract]
 public class RangeFilter
 {
     [DataMember]
     public string From { get; set; }

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

     public RangeFilter() { }

     public RangeFilter(string From, string To)
     {
         this.From = From;
         this.To = To;
     }
 }

 [DataContract]
 public class WeatherDataDTO
 {
     [DataMember]
     public int Id { get; set; }

     // send as string because serialization error with DateTime
     [DataMember]
     public string Date { get; set; }

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

     [DataMember]
     public double Temperature { get; set; }

     [DataMember]
     public int Pressure { get; set; }

     [DataMember]
     public int Rain { get; set; }

     [DataMember]
     public double Wind { get; set; }

     [DataMember]
     public int Direction { get; set; }

     [DataMember]
     public int Humidity { get; set; }
 }

}

Service:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace WeatherDataWCF
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
// NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging.
public class Service1 : IService1
{
private WeatherDataEntities context = new WeatherDataEntities();
public List<WeatherDataDTO> GetWeatherData(RangeFilter rangeFilter)
{
List<WeatherDataDTO> weatherDataDTOs = new List<WeatherDataDTO>();

         var start = parseDateTime(rangeFilter.From);
         var end = parseDateTime(rangeFilter.To);

         var query = from WeatherData in context.WeatherData
                     where WeatherData.Datum >= start && WeatherData.Datum <= end
                     select WeatherData;

         var i = 0; 

         foreach (var weatherData in query)
         {
             weatherDataDTOs.Add(new WeatherDataDTO
             {
                 Id = weatherData.Id,
                 Date = weatherData.Datum.ToString(),
                 Direction = weatherData.Richtung.Value,
                 Humidity = weatherData.Feuchtigkeit.Value,
                 Pressure = weatherData.Luftdruck.Value,
                 Rain = weatherData.Regen.Value,
                 Temperature = weatherData.Temperatur.Value,
                 Time = weatherData.Zeit.Value.ToString(),
                 Wind = weatherData.Wind.Value
             });

             if(i == 100)
             {
                 break;
             }
             i++;
         }

         return weatherDataDTOs;
     }

     private DateTime parseDateTime(string dateString)
     {
         return DateTime.Parse(dateString);
     }
 }

}

MainWindow Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WeatherDataWPF.ServiceReference1;

namespace WeatherDataWPF
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private ServiceReference1.IService1 client = new ServiceReference1.Service1Client();

     public MainWindow()
     {
         InitializeComponent();
         this.fromPicker.SelectedDate = new DateTime(2013, 1, 1);
         this.toPicker.SelectedDate = new DateTime(2013, 1, 5);
     }

     private void Button_Click(object sender, RoutedEventArgs e)
     {

         var from = this.fromPicker.SelectedDate.ToString().Split(' ')[0];
         var to = this.toPicker.SelectedDate.ToString().Split(' ')[0];


         var filter = new RangeFilter
         {
             From = from,
             To = to
         };

         var data = this.client.GetWeatherData(filter);
         this.grid.ItemsSource = data;
     }
 }

}

This is how the Xaml Code looks like:

<Window x:Class="WeatherDataWPF.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WeatherDataWPF"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<DatePicker x:Name="fromPicker" HorizontalAlignment="Left" Margin="163,68,0,0" VerticalAlignment="Top"/>
<DatePicker x:Name="toPicker" HorizontalAlignment="Left" Margin="399,68,0,0" VerticalAlignment="Top"/>
<Label Content="Anfangsdatum:" HorizontalAlignment="Left" Margin="58,66,0,0" VerticalAlignment="Top"/>
<Label Content="Enddatum:" HorizontalAlignment="Left" Margin="313,66,0,0" VerticalAlignment="Top"/>
<Button Content="Ausgabe" HorizontalAlignment="Left" Margin="555,69,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
<DataGrid x:Name="grid" HorizontalAlignment="Left" Height="239" Margin="58,118,0,0" VerticalAlignment="Top" Width="546"/>

 </Grid>

</Window>

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,342 questions
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,666 questions
{count} votes