how to send data from a wcf server to a wpf client?

Ingeborg Conrad Oscar 26 Reputation points
2021-06-27T21:06:24.923+00:00

So my problem is, that i want to read the data from an xml-file into a list of weather data and then send the data to the client, the client should display the data but i only get null. In every column there is null but i dont know why. Is there a syntax error or something else i have not seen? And sorry i dont know how to format the sourcecode.

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

namespace WCF_Weather
{
// 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
{
Measurements measurements = null;

    public Service1()
    {
        using (TextReader reader = new StreamReader(AppDomain.CurrentDomain.BaseDirectory + "/weatherdata.xml"))
        {
            XmlSerializer serializer = new XmlSerializer(typeof(Measurements));
            measurements = (Measurements)serializer.Deserialize(reader);
        }
    }


    public Measurements getAllMeasurements()
    {
        return measurements;
    }

    public List<Measurement> getMeasurement(string filter)
    {
        return (from p in measurements.measurementList
                where p.date.IndexOf(filter, StringComparison.OrdinalIgnoreCase) >= 0 ||
                p.time.IndexOf(filter, StringComparison.OrdinalIgnoreCase) >= 0 ||
                p.temperature.IndexOf(filter, StringComparison.OrdinalIgnoreCase) >= 0 ||
                p.humidity.IndexOf(filter, StringComparison.OrdinalIgnoreCase) >= 0 ||
                p.pressure.IndexOf(filter, StringComparison.OrdinalIgnoreCase) >= 0 ||
                p.rain.IndexOf(filter, StringComparison.OrdinalIgnoreCase) >= 0 ||
                p.wind.IndexOf(filter, StringComparison.OrdinalIgnoreCase) >= 0 ||
                p.direction.IndexOf(filter, StringComparison.OrdinalIgnoreCase) >= 0
                select p).ToList();
    }
}

}

and this is the interface of it

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

namespace WCF_Weather
{
// 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.
[ServiceContract]
public interface IService1
{
[OperationContract]
List<Measurement> getMeasurement(string filter);

    [OperationContract]
    Measurements getAllMeasurements();

    // TODO: Hier Dienstvorgänge hinzufügen
}


// Use a data contract as illustrated in the sample below to add composite types to service operations.
[DataContract]
[XmlRootAttribute("weatherdata")]
[Serializable]
public class Measurements
{
    [DataMember]
    [XmlElement("measurement")]
    public List<Measurement> measurementList { get; set; }
}

[DataContract]
[Serializable]
public class Measurement
{
    [DataMember]
    [XmlElement("Date")]
    public string date { get; set; }

    [DataMember]
    [XmlElement("Time")]
    public string time { get; set; }

    [DataMember]
    [XmlElement("Temperature")]
    public string temperature { get; set; }

    [DataMember]
    [XmlElement("Humidity")]
    public string humidity { get; set; }
    [DataMember]
    [XmlElement("Pressure")]
    public string pressure { get; set; }
    [DataMember]
    [XmlElement("Rain")]
    public string rain { get; set; }
    [DataMember]
    [XmlElement("Wind")]
    public string wind { get; set; }
    [DataMember]
    [XmlElement("Direction")]
    public string direction { get; set; }
}

}

my client side is a wpf serviceapplication

<Window x:Class="WPF_Weather.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:WPF_Weather"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<DataGrid CanUserAddRows="false" ItemsSource="{Binding}" AutoGenerateColumns="False" x:Name="dataGrid" HorizontalAlignment="Left" Height="187" Margin="132,125,0,0" VerticalAlignment="Top" Width="498" SelectionChanged="dataGrid_SelectionChanged">
<DataGrid.Columns>
<DataGridTextColumn Header="Date" Binding="{Binding Path=date}" />
<DataGridTextColumn Header="Time" Binding="{Binding Path=time}"/>
<DataGridTextColumn Header="Temperature" Binding="{Binding Path=temperature}"/>
<DataGridTextColumn Header="Humidity" Binding="{Binding Path=humidity}"/>
<DataGridTextColumn Header="Pressure" Binding="{Binding Path=pressure}"/>
<DataGridTextColumn Header="Rain" Binding="{Binding Path=rain}"/>
<DataGridTextColumn Header="Wind" Binding="{Binding Path=wind}"/>
<DataGridTextColumn Header="Direction" Binding="{Binding Path=direction}"/>
</DataGrid.Columns>
</DataGrid>
<TextBox x:Name="searchBox" HorizontalAlignment="Left" Height="23" Margin="268,89,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="362" TextChanged="searchBox_TextChanged"/>
<Label Content="Nach Datum suchen:" HorizontalAlignment="Left" Margin="132,86,0,0" VerticalAlignment="Top"/>
<Label Content="Wetterstation" HorizontalAlignment="Left" Margin="263,10,0,0" VerticalAlignment="Top" FontSize="36"/>
<Label Content="" HorizontalAlignment="Left" Margin="76,32,0,0" VerticalAlignment="Top"/>
</Grid>
</Window>

and here the .cs file from the wpf

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
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;

namespace WPF_Weather
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
ServiceReference1.IService1 service;
ObservableCollection<ServiceReference1.Measurement> measurements;
ServiceReference1.Measurement currentNote;
public MainWindow()
{
service = new ServiceReference1.Service1Client();
measurements = new ObservableCollection<ServiceReference1.Measurement>(service.getAllMeasurements().measurementList);

        InitializeComponent();

        dataGrid.DataContext = measurements;
    }

    private void dataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {

    }

    private void searchBox_TextChanged(object sender, TextChangedEventArgs e)
    {
        measurements.Clear();
        service.getMeasurement(searchBox.Text).ToList().ForEach(measurements.Add);
    }
}

}

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,647 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,648 questions
{count} vote

1 answer

Sort by: Most helpful
  1. Lex Li (Microsoft) 5,322 Reputation points Microsoft Employee
    2021-07-03T03:46:11.213+00:00

    Though duplex communication is possible (and rather complicated) in WCF, for simplicity you should use a framework like SignalR instead.

    0 comments No comments