title: Call synchronous WCF operation contract methods asynchronously on silverlight
body: <p>We are consuming wcf services on the silverlight application trough creating proxies using ChanellFacotry.</p>
  
  <p>The Operation and Data contracts are exposed to silverlight trough assembly, which is consist of shared files from serverside Data and Operation contract libriary. (omg I hope you understand what I am saying).</p>
  
  <p>So server and client are using same operation and data contracts.</p>
  
  <p>Silverlight wcf client lib has a restriction to not be able to call wcf methods synchronously, as you know, so shared operation contract file has to expose asyn versions of each operation. </p>
  
  <p>Writing async WCF service would make some sense if they were not containing blocking operations, but as we are using EF, asyncrhony is achieved by delegating blocking work to thread pool. This is what WCF do for synch methods anyway. And this fact makes me want to tear my eyes out (#@%!^%!@%).</p>
  
  <p>Our project consultant has power to not allow generate dynamic proxies on the client to call synch operation contract methods (google Yevhen Bobrov Servelat Pieces if you are interested). So we have to write senseles implementations of async methods on serverside, without any performance gain (blocking calls as you remember).</p>
  
  <p>Is it possible to call wcf web service synchronous method from silverlight using its data contract?</p>
  
  <p>Have you ever faced this problem before, if so how did you solve it?</p>
  
  <p>At the momment I am looking forward for generating async contracts for client side only using serverside contracts as a transformation source. Maybe there are some t4 template that can nicely do it for me?</p>
  
  <p>Sorry for the wall of text, just to mix some code to my question, this is how async contract implementation looks at the momment:</p>
  
  <pre><code>    /// &lt;summary&gt;
      /// Subscribes to users of the specified organization.
      /// &lt;/summary&gt;
      /// &lt;param name="organizationId"&gt;The organization id.&lt;/param&gt;
      public void Unsubscribe(int organizationId)
      {
          var clientId = this.OperationContext.GetClientId();
          if (string.IsNullOrEmpty(clientId))
          {
              return;
          }
  
          this.InternalUnsubscribe(organizationId, clientId);
      }
  
      /// &lt;summary&gt;
      /// Begins an asynchronous operation to Unsubscribe.
      /// &lt;/summary&gt;
      /// &lt;param name="organizationId"&gt;The organization id.&lt;/param&gt;
      /// &lt;param name="callback"&gt;The callback.&lt;/param&gt;
      /// &lt;param name="passThroughData"&gt;The pass through data.&lt;/param&gt;
      /// &lt;returns&gt;
      /// An implementation of &lt;see cref="IAsyncResult"/&gt; that provides access to the state or result of the operation.
      /// &lt;/returns&gt;
      public IAsyncResult BeginUnsubscribe(int organizationId, AsyncCallback callback, object passThroughData)
      {
          var clientId = this.OperationContext.GetClientId();
          if (string.IsNullOrEmpty(clientId))
          {
              return null;
          }
  
          var asyncResult = new VoidAsyncResult(callback, passThroughData);
          Task.Factory.StartNew(() =&gt;
              {
                  try
                  {
                      this.InternalUnsubscribe(organizationId, clientId);
                      asyncResult.SetAsCompleted(false);
                  }
                  catch (Exception ex)
                  {
                      asyncResult.SetAsCompleted(ex, false);
                  }
              });
          return asyncResult;
      }
  
      /// &lt;summary&gt;
      /// Ends an existing asynchronous operation to Unsubscribe.
      /// &lt;/summary&gt;
      /// &lt;param name="result"&gt;The &lt;see cref="IAsyncResult"/&gt; provided by the BeginUnsubscribe operation.&lt;/param&gt;
      public void EndUnsubscribe(IAsyncResult result)
      {
          var response = result as VoidAsyncResult;
          if (response != null)
          {
              response.EndInvoke();
          }
      }
  </code></pre>