title: Decoupling Silverlight client from service reference generated class
body: <p>I am researching Prism v2 by going thru the quickstarts. And I have created a WCF service with the following signature:</p>
  
  <pre><code>namespace HelloWorld.Silverlight.Web
  {
  [ServiceContract(Namespace = "http://helloworld.org/messaging")]
  [AspNetCompatibilityRequirements(RequirementsMode =
                                   AspNetCompatibilityRequirementsMode.Allowed)]
    public class HelloWorldMessageService
    {
      private string message = "Hello from WCF";
  
      [OperationContract]
      public void UpdateMessage(string message)
      {
        this.message = message;
      }
  
      [OperationContract]
      public string GetMessage()
      {
        return message;
      }
    }
  }
  </code></pre>
  
  <p>When I add a service reference to this service in my silverlight project it generates an interface and a class:</p>
  
  <pre><code>[System.ServiceModel.ServiceContractAttribute
          (Namespace="http://helloworld.org/messaging",
           ConfigurationName="Web.Services.HelloWorldMessageService")]
  public interface HelloWorldMessageService {
  
    [System.ServiceModel.OperationContractAttribute
            (AsyncPattern=true,
        Action="http://helloworld.org/messaging/HelloWorldMessageService/UpdateMessage", 
  ReplyAction="http://helloworld.org/messaging/HelloWorldMessageService/UpdateMessageResponse")]
      System.IAsyncResult BeginUpdateMessage(string message, System.AsyncCallback callback, object asyncState);
  
      void EndUpdateMessage(System.IAsyncResult result);
  
      [System.ServiceModel.OperationContractAttribute(AsyncPattern=true, Action="http://helloworld.org/messaging/HelloWorldMessageService/GetMessage", ReplyAction="http://helloworld.org/messaging/HelloWorldMessageService/GetMessageResponse")]
      System.IAsyncResult BeginGetMessage(System.AsyncCallback callback, object asyncState);
  
      string EndGetMessage(System.IAsyncResult result);
  }
  
  public partial class HelloWorldMessageServiceClient : System.ServiceModel.ClientBase&lt;HelloWorld.Core.Web.Services.HelloWorldMessageService&gt;, HelloWorld.Core.Web.Services.HelloWorldMessageService {
  {
      // implementation
  }
  </code></pre>
  
  <p>I'm trying to decouple my application by passing around the interface instead of the concrete class. But I'm having difficulty finding examples of how to do this. When I try and call EndGetMessage and then update my UI I get an exception about updating the UI on the wrong thread. How can I update the UI from a background thread? </p>
  
  <hr>
  
  <p>I tried but I get <code>UnauthorizedAccessException : Invalid cross-thread access</code>. </p>
  
  <pre><code>string messageresult = _service.EndGetMessage(result);
  
  Application.Current.RootVisual.Dispatcher.BeginInvoke(() =&gt; this.Message = messageresult );
  </code></pre>
  
  <p>The exception is thrown by <code>Application.Current.RootVisual</code>.</p>