question

MaxBrannstetter-5337 avatar image
0 Votes"
MaxBrannstetter-5337 asked DuaneArnold-0443 commented

Why is my WCF Service not working?

Interface IService:



 [ServiceContract]
     public interface IService1 {
         [OperationContract]
         List<Note> getNote(string filter);
    
         [OperationContract]
         Notes getAllNotes();
    
         [OperationContract]
         void addNote(Note note);
    
         [OperationContract]
         void deleteNote(Note note);
    
         [OperationContract]
         void updateNote(Note oldNote, Note newNote);
    
         // 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("notes")]
     [Serializable]
     public class Notes {
         [DataMember]
         [XmlElement("note")]
         public List<Note> noteList { get; set; }
     }
     [DataContract]
     [Serializable]
     public class Note {
    
         [DataMember]
         [XmlElement("to")]
         public string to { get; set; }
    
         [DataMember]
         [XmlElement("from")]
         public string von { get; set; }
    
         [DataMember]
         [XmlElement("heading")]
         public string heading { get; set; }
    
         [DataMember]
         [XmlElement("body")]
         public string body { get; set; }
    
         public override bool Equals(Object obj) {
             // Perform an equality check on two rectangles (Point object pairs).
             if (obj == null || GetType() != obj.GetType())
                 return false;
             Note r = (Note)obj;
             return to.Equals(r.to) && von.Equals(r.von) && heading.Equals(r.heading) && body.Equals(r.body);
         }
     }

Service1 Class:



 public class Service1 : IService1 {
         Notes notes = null;
    
         public Service1() {
             using (TextReader reader = new StreamReader(AppDomain.CurrentDomain.BaseDirectory + "/note.xml")) {
                 XmlSerializer serializer = new XmlSerializer(typeof(Notes));
                 notes = (Notes)serializer.Deserialize(reader);
             }
         }
    
         public void addNote(Note note) {
             notes.noteList.Add(note);
    
             using (TextWriter writer = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "/note.xml")) {
                 XmlSerializer serializer = new XmlSerializer(typeof(Notes));
                 serializer.Serialize(writer,notes); 
             }
         }
    
         public void deleteNote(Note note) {
             notes.noteList.Remove(note);
    
             using (TextWriter writer = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "/note.xml")) {
                 XmlSerializer serializer = new XmlSerializer(typeof(Notes));
                 serializer.Serialize(writer, notes);
             }
         }
    
         public Notes getAllNotes() {
             return notes;
         }
    
         public List<Note> getNote(string filter) {
             return (from p in notes.noteList
                     where p.to.IndexOf(filter, StringComparison.OrdinalIgnoreCase) >= 0 ||
                     p.von.IndexOf(filter, StringComparison.OrdinalIgnoreCase) >= 0 ||
                     p.heading.IndexOf(filter, StringComparison.OrdinalIgnoreCase) >= 0 ||
                     p.body.IndexOf(filter, StringComparison.OrdinalIgnoreCase) >= 0 
                     select p).ToList();
         }
    
         public void updateNote(Note oldNote, Note newNote) {
             notes.noteList.Remove(oldNote);
             notes.noteList.Add(newNote);
    
             using (TextWriter writer = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "/note.xml")) {
                 XmlSerializer serializer = new XmlSerializer(typeof(Notes));
                 serializer.Serialize(writer, notes);
             }
         }
     }

Main Window:



  public partial class MainWindow : Window {
         ServiceReference1.IService1 service;
         ObservableCollection<ServiceReference1.Note> notes;
         ServiceReference1.Note currentNote;
         public MainWindow() {
             service = new ServiceReference1.Service1Client();
             notes = new ObservableCollection<ServiceReference1.Note>(service.getAllNotes().noteList);
    
             InitializeComponent();
    
             dataGrid.DataContext = notes;
         }
    
         private void dataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e) {
    
         }
    
         private void textBox_TextChanged(object sender, TextChangedEventArgs e) {
             notes.Clear();
             service.getNote(textBox.Text).ToList().ForEach(notes.Add);
         }
    
         private void Button_Click(object sender, RoutedEventArgs e) {
             ServiceReference1.Note note = new ServiceReference1.Note();
             note.von = fromBox.Text;
             note.to = toBox.Text;
             note.body = bodyBox.Text;
             note.heading = headingBox.Text;
             service.addNote(note);
             notes.Clear();
             service.getNote(textBox.Text).ToList().ForEach(notes.Add);
         }
    
         private void dataGrid_SelectionChanged_1(object sender, SelectionChangedEventArgs e) {
             currentNote = (ServiceReference1.Note)dataGrid.SelectedItem;
                
         }
    
         private void deleteButton_Click(object sender, RoutedEventArgs e) {
             if (currentNote != null) {
                 service.deleteNote(currentNote);
                 notes.Clear();
                 service.getNote(textBox.Text).ToList().ForEach(notes.Add);
             }
         }
    
         private void updateButton_Click(object sender, RoutedEventArgs e) {
             if (currentNote != null) {
                 ServiceReference1.Note note = new ServiceReference1.Note();
                 note.von = fromUpdateBox.Text;
                 note.to = toUpdateBox.Text;
                 note.body = bodyUpdateBox.Text;
                 note.heading = headingUpdateBox.Text;
                 service.updateNote(currentNote,note);
                 notes.Clear();
                 service.getNote(textBox.Text).ToList().ForEach(notes.Add);
             }
               
         }
     }


Window XAML:



 <Window x:Class="WPF_Window.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_Window"
         mc:Ignorable="d"
         Title="MainWindow" Height="450" Width="800">
     <Grid>
         <DataGrid CanUserAddRows="false" ItemsSource="{Binding}" AutoGenerateColumns="False" x:Name="dataGrid" HorizontalAlignment="Left" Height="195" Margin="35,121,0,0" VerticalAlignment="Top" Width="308" SelectionChanged="dataGrid_SelectionChanged_1" IsReadOnly="True">
             <DataGrid.Columns>
                 <DataGridTextColumn Header="to" Binding="{Binding Path=to}" />
                 <DataGridTextColumn Header="from" Binding="{Binding Path=von}"/>
                 <DataGridTextColumn Header="heading" Binding="{Binding Path=heading}"/>
                 <DataGridTextColumn Header="body" Binding="{Binding Path=body}"/>
             </DataGrid.Columns>
         </DataGrid>
         <TextBox x:Name="textBox" HorizontalAlignment="Left" Height="23" Margin="99,84,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" TextChanged="textBox_TextChanged"/>
         <Label Content="Suche" HorizontalAlignment="Left" Margin="35,79,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.417,-1.301"/>
         <Label Content="to:" HorizontalAlignment="Left" Margin="514,59,0,0" VerticalAlignment="Top" Width="27"/>
         <Label Content="from:" HorizontalAlignment="Left" Margin="514,83,0,0" VerticalAlignment="Top" Width="38"/>
         <Label Content="heading:" HorizontalAlignment="Left" Margin="494,109,0,0" VerticalAlignment="Top" Width="58"/>
         <Label Content="body:" HorizontalAlignment="Left" Margin="509,135,0,0" VerticalAlignment="Top" Width="58"/>
         <TextBox x:Name="toBox" HorizontalAlignment="Left" Height="23" Margin="572,62,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
         <TextBox x:Name="fromBox" HorizontalAlignment="Left" Height="23" Margin="572,87,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
         <TextBox x:Name="headingBox" HorizontalAlignment="Left" Height="23" Margin="572,112,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
         <TextBox x:Name="bodyBox" HorizontalAlignment="Left" Height="23" Margin="572,138,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
         <Button Content="Hinzufügen" HorizontalAlignment="Left" Margin="617,175,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
         <Button x:Name="deleteButton" Content="Löschen" HorizontalAlignment="Left" Margin="365,294,0,0" VerticalAlignment="Top" Width="75" Click="deleteButton_Click"/>
         <Label Content="to:" HorizontalAlignment="Left" Margin="514,208,0,0" VerticalAlignment="Top" Width="27"/>
         <Label Content="from:" HorizontalAlignment="Left" Margin="514,232,0,0" VerticalAlignment="Top" Width="38"/>
         <Label Content="heading:" HorizontalAlignment="Left" Margin="494,258,0,0" VerticalAlignment="Top" Width="58"/>
         <Label Content="body:" HorizontalAlignment="Left" Margin="509,284,0,0" VerticalAlignment="Top" Width="58"/>
         <TextBox x:Name="toUpdateBox" HorizontalAlignment="Left" Height="23" Margin="572,211,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" Text="{
                         Binding ElementName=dataGrid,
                          Path=SelectedItem.to,
                          Mode=OneWay,
                          UpdateSourceTrigger=PropertyChanged}"/>
         <TextBox x:Name="fromUpdateBox" HorizontalAlignment="Left" Height="23" Margin="572,236,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" Text="{
                         Binding ElementName=dataGrid,
                          Path=SelectedItem.von,
                          Mode=OneWay,
                          UpdateSourceTrigger=PropertyChanged}"/>
         <TextBox x:Name="headingUpdateBox" HorizontalAlignment="Left" Height="23" Margin="572,261,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" Text="{
                         Binding ElementName=dataGrid,
                          Path=SelectedItem.heading,
                          Mode=OneWay,
                          UpdateSourceTrigger=PropertyChanged}"/>
         <TextBox x:Name="bodyUpdateBox" HorizontalAlignment="Left" Height="23" Margin="572,287,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" Text="{
                         Binding ElementName=dataGrid,
                          Path=SelectedItem.body,
                          Mode=OneWay,
                          UpdateSourceTrigger=PropertyChanged}"/>
         <Button x:Name="updateButton" Content="Update" HorizontalAlignment="Left" Margin="617,324,0,0" VerticalAlignment="Top" Width="75" Click="updateButton_Click"/>
     </Grid>
 </Window>







dotnet-csharpwindows-wcf
· 2
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.

What version of dot net are you using for the wpf app? Is the wcf app in the same solution? If so did you right click on the solution and select set startup apps and make sure the wcf and wpf both are set to start? What is the error you are getting?

0 Votes 0 ·

What do you mean by not working?

0 Votes 0 ·

0 Answers