title: MissingMethodException from Async serialisation operation
body: <p>I have written a small Async library wrapper to <a href="http://james.newtonking.com/projects/json-net.aspx" rel="nofollow noreferrer">Json.Net</a> v5.06 from NuGet and after updating a number of Telerik packages my UnitTests started to fail with a <a href="http://msdn.microsoft.com/en-us/library/system.missingmethodexception.aspx" rel="nofollow noreferrer">MissingMethodException</a>. </p>
  
  <p>I have created a dummy non Async method which works so I am confused where the error lies.</p>
  
  <p><strong>Project Target Platform</strong></p>
  
  <p>.Net 4.5</p>
  
  <p>x86 CPU</p>
  
  <p><strong>Async Operation</strong></p>
  
  <pre><code>    public class JsonSerialiser : ISerialiser
      {
  
          [InjectionConstructor]
          public JsonSerialiser(IStringCompression streamCompressor, ILog logger)
          {
              if (streamCompressor == null) throw new ArgumentNullException("streamCompressor");
              if (logger == null) throw new ArgumentNullException("logger");
  
              XmlConfigurator.Configure();
  
              this.streamCompressor = streamCompressor;
              this.logger = logger;
          }
  
  
          public async Task&lt;string&gt; SerialiseAsync&lt;T&gt;(T serialseObject) where T : class
          {
              if (serialseObject == null) throw new ArgumentNullException("serialseObject");
  
              try
              {
                  return await JsonConvert.SerializeObjectAsync(serialseObject);
              }
              catch (JsonSerializationException ex)
              {
                  logger.Error(ex);
                  throw new SerialisationException("Could Not Serialse The Object", ex);
              }
          }
     }
  </code></pre>
  
  <p><strong>Async Example</strong></p>
  
  <p>Now this code was throw together just to test the basic serialisation where I step over the null checks in the class constructor.</p>
  
  <pre><code>    private async void button1_Click(object sender, EventArgs e)
      {
          List&lt;Part&gt; parts = new List&lt;Part&gt; { new Part() { AbstractType = typeof(IOpcController), ConcreteType = typeof(OpcController) },
                                                  new Part() { AbstractType = typeof(ISerialiser), ConcreteType = typeof(JsonSerialiser) },
                                                  new Part() { AbstractType = typeof(IStringCompression), ConcreteType = typeof(StringGZipCompression)}};
  
          string serialisedResult = string.Empty;
          JsonSerialiser serialiser = new JsonSerialiser(null, null);
          serialisedResult = await serialiser.SerialiseAsync&lt;List&lt;Part&gt;&gt;(parts);
      }
  </code></pre>
  
  <p><strong>Async Result</strong></p>
  
  <p>This generation an MissingMethodException</p>
  
  <p><img src="https://i.stack.imgur.com/TwOrH.png" alt="enter image description here"></p>
  
  <pre><code>Method not found: 'System.Threading.Tasks.Task`1&lt;System.String&gt; Newtonsoft.Json.JsonConvert.SerializeObjectAsync(System.Object)'.
  
     at Helper.Core.Serialisation.Json.JsonSerialiser.&lt;SerialiseAsync&gt;d__0`1.MoveNext()
     at System.Runtime.CompilerServices.AsyncMethodBuilderCore.Start[TStateMachine](TStateMachine&amp; stateMachine)
     at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1.Start[TStateMachine](TStateMachine&amp; stateMachine)
     at Helper.Core.Serialisation.Json.JsonSerialiser.SerialiseAsync[T](T serialseObject)
     at OpcTester.Form1.&lt;button1_Click&gt;d__9.MoveNext() in c:\Users\phil.murray\Desktop\tmp\OpcTester\Form1.cs:line 44
  </code></pre>
  
  <p><strong>None Async operation</strong></p>
  
  <p>Just to check if it was the asynchronous part of the method that was causing the problem I wrote a none asynchronous implementation.</p>
  
  <pre><code>public string Serialise&lt;T&gt;(T serialseObject) where T : class
  {
      if (serialseObject == null) throw new ArgumentNullException("serialseObject");
  
      try
      {
          return  JsonConvert.SerializeObject(serialseObject);
      }
      catch (JsonSerializationException ex)
      {
          logger.Error(ex);
          throw new SerialisationException("Could Not Serialse The Object", ex);
      }
  }
  </code></pre>
  
  <p><strong>None Async Implementation</strong></p>
  
  <pre><code>private async void button1_Click(object sender, EventArgs e)
  {
      List&lt;Part&gt; parts = new List&lt;Part&gt; { new Part() { AbstractType = typeof(IOpcController), ConcreteType = typeof(OpcController) },
                                              new Part() { AbstractType = typeof(ISerialiser), ConcreteType = typeof(JsonSerialiser) },
                                              new Part() { AbstractType = typeof(IStringCompression), ConcreteType = typeof(StringGZipCompression)}};
  
      string serialisedResult = string.Empty;
      JsonSerialiser serialiser = new JsonSerialiser(null, null);
      serialisedResult = serialiser.Serialise&lt;List&lt;Part&gt;&gt;(parts);
  }
  </code></pre>
  
  <p><strong>None Async Result</strong></p>
  
  <p>The method completes and serialises the List to a string.</p>
  
  <p><strong>Failed Test Example</strong></p>
  
  <pre><code>    Test Name:  SerialiserSerialiseObjectExists
      Test FullName:  Helper.Tests.SerialiserTests.SerialiserSerialiseObjectExists
      Test Source:    c:\Perforce\Development\SharedAPIs\Helper.Core\Helper.Tests\SerialiserTests.cs : line 38
      Test Outcome:   Failed
      Test Duration:  0:00:00.0116216
  
      Result Message: 
      Test method Helper.Tests.SerialiserTests.SerialiserSerialiseObjectExists threw exception: 
      System.MissingMethodException: Method not found: 'System.Threading.Tasks.Task`1&lt;System.String&gt; Newtonsoft.Json.JsonConvert.SerializeObjectAsync(System.Object)'.
      Result StackTrace:  
      at Helper.Core.Serialisation.Json.JsonSerialiser.&lt;SerialiseAsync&gt;d__0`1.MoveNext()
         at System.Runtime.CompilerServices.AsyncMethodBuilderCore.Start[TStateMachine](TStateMachine&amp; stateMachine)
         a
  
  t System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1.Start[TStateMachine](TStateMachine&amp; stateMachine)
     at Helper.Core.Serialisation.Json.JsonSerialiser.SerialiseAsync[T](T serialseObject)
     at Helper.Tests.SerialiserTests.&lt;SerialiserSerialiseObjectExists&gt;d__3.MoveNext() in c:\Perforce\Development\SharedAPIs\Helper.Core\Helper.Tests\SerialiserTests.cs:line 40
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
  </code></pre>
  
  <p><strong>Async Test without Json.Net</strong></p>
  
  <p>Just for arguments sake I replaced the Json.Net call with a dummy Task and it worked so it looks like the problem is with calling Json.net using Await\Async. Strange as this was working and the version has not been updated.</p>
  
  <pre><code>public async Task&lt;string&gt; SerialiseAsync&lt;T&gt;(T serialseObject) where T : class
  {
      if (serialseObject == null) throw new ArgumentNullException("serialseObject");
  
      try
      {
          //return await JsonConvert.SerializeObjectAsync(serialseObject);
          return await Task.Run(() =&gt; string.Empty);
      }
      catch (JsonSerializationException ex)
      {
          logger.Error(ex);
          throw new SerialisationException("Could Not Serialse The Object", ex);
      }
  }
  </code></pre>
  
  <p><strong>Question</strong></p>
  
  <p>Now the asynchronous method UnitTests were previously working before I updated the Telerik controls suite and I did test the operation in a number of real world instances. I am not saying the Telerik update caused the problem as it may be a coincidence. A number of other asynchronous tests cases pass when testing other classes (not related to Json.Net).</p>
  
  <p>Any idea what's wrong with the Async method and how to resolve the problem?</p>
  
  <p><strong>Possible Resolution</strong></p>
  
  <p>As I continued to investigate the problem it occurred to me that the problem may be in the Async call in the Json.Net library so I wrappered the none Async call in a Task, as below, which worked</p>
  
  <pre><code>    public async Task&lt;string&gt; SerialiseAsync&lt;T&gt;(T serialseObject) where T : class
      {
          if (serialseObject == null) throw new ArgumentNullException("serialseObject");
  
          try
          {
              //return await JsonConvert.SerializeObjectAsync(serialseObject);
              return await Task.Run&lt;string&gt;(() =&gt; JsonConvert.SerializeObject(serialseObject));
          }
          catch (JsonSerializationException ex)
          {
              logger.Error(ex);
              throw new SerialisationException("Could Not Serialse The Object", ex);
          }
      }
  </code></pre>
  
  <p>Out of interest I downloaded the source for Json.Net and checked out the JsonConvert.SerializeObjectAsync call and it was doing the same thing so I am again unsure of the underlying problem.</p>
  
  <pre><code>public static Task&lt;string&gt; SerializeObjectAsync(object value, Formatting formatting, JsonSerializerSettings settings)
  {
    return Task.Factory.StartNew(() =&gt; SerializeObject(value, formatting, settings));
  }
  </code></pre>