2

I'm trying since days to get a PropertyChanged event from my wcf-service to a wcf-client if a collection on server side has changed(by client action or server action). There must be a better solution instead of using callbacks and reload the list... or?

on server side: (almost like an example from another post) 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");
        }
    }
}

on client side:

ObservableServiceClient client = new ObservableServiceClient();
Data data = client.getData();

So far its working... i can query the collection at client side, but i don't receive "propertyChanged" when something changes on the servers collection?

What's wrong? Where is my mistake & missunderstanding?

Community
  • 1
  • 1
Vince
  • 21
  • 2
  • 1
    I suppose `ObservableCollection` won't automatically send information through WCF to inform about updates. I suggest you to use a Duplex contract type and invoke callback methods to communicate something to the client. More information here: http://msdn.microsoft.com/en-us/library/ms731064.aspx. Let me know if you need help with the code. – Lukasz M Oct 01 '12 at 18:26

1 Answers1

0

I suggest you take a look at SignalR. It's an awesome open source project which will help you implement realtime messaging in no time. There are libraries for different clients available and they are super easy to use.

Take a look at this quickstart. It really is this simple: https://github.com/SignalR/SignalR/wiki/QuickStart-Hubs

I've used WCF duplex callbacks but depending on the binding, if you want to expose your service over the Internet, it will give you nothing but headaches (Firewalls, NATs and Net.Tcp IIS requirements). I have used the DuplexHttpBinding but it needs some work to get it into a production ready state.

Tiago
  • 737
  • 5
  • 20