title: Mocking generic WCF ClientChannelWrapper async calls
body: <p>I recently developed a Silverlight application which uses <a href="http://www.developmentalmadness.com/archive/2010/03/08/mvvm-with-prism-101-ndash-part-6b-wrapping-iclientchannel.aspx" rel="nofollow noreferrer">Mark J Millers <code>ClientChannelWrapper&lt;T&gt;</code></a> to communicate with the WCF service layer (effectively killing the service reference and wrapping <code>IClientChannel</code> and <code>ClientChannelFactory</code>).
  Here is the interface:</p>
  
  <pre><code>public interface IClientChannelWrapper&lt;T&gt; where T : class
  {
      IAsyncResult BeginInvoke(Func&lt;T, IAsyncResult&gt; function);
      void Dispose();
      void EndInvoke(Action&lt;T&gt; action);
      TResult EndInvoke&lt;TResult&gt;(Func&lt;T, TResult&gt; function);
  }
  </code></pre>
  
  <p>The Wrapper basically takes a generic async service interface (which might have been generated by slsvcutil or hand crafted after the WCF <code>ServiceContract</code>) and wraps the calls to ensure that in case of a channel fault, a new channel gets created.
  Typical usage looks like this:</p>
  
  <pre><code> public WelcomeViewModel(IClientChannelWrapper&lt;IMyWCFAsyncService&gt; service)
      {
          this.service = service;
          this.synchronizationContext = SynchronizationContext.Current ?? new SynchronizationContext();
          this.isBusy = true;
          this.service.BeginInvoke(m =&gt; m.BeginGetCurrentUser(new AsyncCallback(EndGetCurrentUser), null));
      }
  
  private void EndGetCurrentUser(IAsyncResult result)
      {
          string strResult = "";
          service.EndInvoke(m =&gt; strResult = m.EndGetCurrentUser(result));
          this.synchronizationContext.Send(
                      s =&gt;
                      {
                          this.CurrentUserName = strResult;
                          this.isBusy = false;
                      }, null);
      }
  </code></pre>
  
  <p>It all works fine but now I'd like to unit test the view models which use the <code>ClientChannelWrapper</code>.
  I've set up a simple unit test using Moq:</p>
  
  <pre><code>[TestMethod]
      public void WhenCreated_ThenRequestUserName()
      {
          var serviceMock = new Mock&lt;IClientChannelWrapper&lt;IMyWCFAsyncService&gt;&gt;();
          var requested = false;
  
          //the following throws an exception
          serviceMock.Setup(svc =&gt; svc.BeginInvoke(p =&gt; p.BeginGetCurrentUser(It.IsAny&lt;AsyncCallback&gt;(), null))).Callback(() =&gt; requested = true);
  
  
          var viewModel = new ViewModels.WelcomeViewModel(serviceMock.Object);
          Assert.IsTrue(requested);
      }
  </code></pre>
  
  <p>I get a NotSupportedException: </p>
  
  <blockquote>
    <p>Unsupported expression: p => p.BeginGetCurrentUser(IsAny(), null).</p>
  </blockquote>
  
  <p>I'm pretty new to Moq but I guess there is some problem with the <code>ClientChannelWrapper</code> using generic Service interfaces. Trying to wrap my head around this for quite some time now, maybe someone has an idea. Thank you.</p>