I am self-hosting a WCF service in a WPF client. I want to show the data the service receives in the user interface. Every time some data is received the user interface should be updated.
The code in "App.xaml.cs" looks like
private ServiceHost _host = new ServiceHost(typeof(MyService));
private void Application_Startup(object sender, StartupEventArgs e)
{
_host.Open();
}
private void Application_Exit(object sender, ExitEventArgs e)
{
_host.Close();
}
How can I get the object instance(s) implementing the service contract from the hosting WPF application?
Thanks everybody for the answers.
What I didn't see was that the constructor of ServiceHost allows to pass an instance of the service instead of its type.
So what I do now is:
- Use an ObservableCollection in the service implementation
- Configure the service to be a singleton (see theburningmonk's comment)
- Bind to the ObservableCollection in my WPF application
- Get an instance of the service using the databinding property DataContext
- Pass it to the constructor of ServiceHost
Result: Every update in the singleton WCF service is reflected in the UI.
Happy!