如果服务器端的集合发生了变化(通过客户端或服务器操作),我会尝试从wcf-service向wcf-client获取PropertyChanged事件。必须有一个更好的解决方案,而不是使用回调和重新加载列表...或?
在服务器端:(几乎像另一篇文章中的示例) ObservableCollection and CollectionChanged event as WCF datacontract
public interface IObservableService
{
[OperationContract(IsOneWay = false)]
Data getData();
}
[DataContract]
public class Data : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void Notify(string propertyName)
{
if (this.PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
Console.WriteLine("Notify()");
}
}
private ObservableCollection<string> list;
internal Data()
{
list = new ObservableCollection<string>();
list.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(list_CollectionChanged);
}
void list_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
Console.WriteLine("list_CollectionChanged");
Notify("DataList");
Notify("Data");
}
[DataMember]
public ObservableCollection<string> DataList
{
get
{
return list;
}
set {
list = value;
Console.WriteLine("set DataList");
Notify("DataList");
Notify("Data");
}
}
}在客户端:
ObservableServiceClient client = new ObservableServiceClient();
Data data = client.getData();到目前为止它是有效的..。我可以在客户端查询集合,但当servers集合上发生变化时,我不会收到"propertyChanged“?
怎么了?我的错误和误解在哪里?
发布于 2012-10-02 05:31:06
我建议你看看SignalR。这是一个很棒的开源项目,它将帮助你在任何时候实现实时消息传递。有针对不同客户端的库可用,并且它们非常容易使用。
看一下这个快速入门。它真的很简单:https://github.com/SignalR/SignalR/wiki/QuickStart-Hubs
我已经使用了WCF双工回调,但是根据绑定的不同,如果你想在互联网上公开你的服务,它只会让你头疼(防火墙,NAT和Net.Tcp IIS要求)。我使用过DuplexHttpBinding,但它需要做一些工作才能使其进入生产就绪状态。
https://stackoverflow.com/questions/12676113
复制相似问题