title: Self-hosted WCF service: How to access the object(s) implementing the service contract from the hosting application?
body: <p>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.</p>
  
  <p>The code in "App.xaml.cs" looks like</p>
  
  <pre><code>    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();
      }
  </code></pre>
  
  <p>How can I get the object instance(s) implementing the service contract from the hosting WPF application?</p>
  
  <hr>
  
  <p>Thanks everybody for the answers.</p>
  
  <p>What I didn't see was that the constructor of ServiceHost allows to pass an <b>instance</b> of the service instead of its <b>type</b>.</p>
  
  <p>So what I do now is:</p>
  
  <ul>
  <li>Use an ObservableCollection in the service implementation</li>
  <li>Configure the service to be a singleton (see theburningmonk's comment)</li>
  <li>Bind to the ObservableCollection in my WPF application</li>
  <li>Get an instance of the service using the databinding property <a href="http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.datacontext.aspx" rel="noreferrer">DataContext</a></li>
  <li>Pass it to the constructor of ServiceHost</li>
  </ul>
  
  <p>Result: Every update in the singleton WCF service is reflected in the UI.</p>
  
  <p>Happy!</p>