我是新人,只是在WCF中待了一段时间。我创建了一个小型ServiceContract,如下所示:
[ServiceContract]
public interface IStudents
{
[OperationContract]
List<Student> GetStudents();
[OperationContract]
bool Add(Student stud);
}
使用DataContract:
[DataContract(Name = "Students")]
public class Student
{
[DataMember]
public string Name { get; private set; }
[DataMember]
public double Rating { get; private set; }
public Student(string name, double rating)
{
this.Name = name;
this.Rating = rating;
}
public override string ToString()
{
return $"{Name}, {Rating}";
}
}
我还有WPF项目,其中UI允许添加或显示学生。单击按钮时,我添加了带有服务和显示列表的学生。我正在调试也是主持人所以我看到有学生在收集但是当它回到WPF收集是空的。这是一段WPF代码
//主窗口
public StudentsClient Service { get; set; } = new StudentsClient("BasicHttpBinding_IStudents");
//按钮点击方法:
Service.Add(new Student(NameBox.Text, Convert.ToDouble(RateBox.Text)));
GetStudentsAsync();
GetStudentAsync方法:
private async void GetStudentsAsync()
{
InfoTextBlock.Text = "Getting studs..";
var studs = await Service.GetStudentsAsync(); // studs collection is empty there
Students = new ObservableCollection<Student>(studs);
StudentListView.ItemsSource = Students;
InfoTextBlock.Text = "";
}
有谁知道我哪里弄错了?
PS。如果我的Student模型类具有与Serice相同的属性,是否会出现问题。我是否必须在共享类库中创建它?
编辑:
public class Students : IStudents
{
public List<Student> GetStudents()
{
return Database.AllStudents;
}
public bool Add(Student stud)
{
Database.AllStudents.Add(stud);
return true;
}
}