Geeks With Blogs

News Please visit me at my new blog!!

profile for Aligned at Stack Overflow, Q&A for professional and enthusiast programmers
"free in Christ Jesus from the law of sin and death." Romans 8:2 (ESV) Check out the Falling Plates video on YouTube.
more about the Gospel
And then listen to Francis Chan speaking at LifeLight in SD.

Donate Bitcoins

Programming and Learning from SD

My colleague, Brian, had this problem and posted it on our Slack channel. I suggested he share it out further, and since he isn’t blogging yet (I keep encouraging him to do so), I offered to post it for him.

He thinks this would mainly happen to something other than an MVC site.  In his case it was a console application which didn't have WebApi packages included by default. There was a pretty popular solution that involved by passing the ReadAsAsync<MyModel>() call and instead doing a ReadAsStream() then using Json.Net directly to Deserialize the stream into an object, but he found a better different way.

TIP: If you are having problems consuming a webAPI with ReadAsAsync<SomeClass>() and you get an error with Newtonsoft.Json and System.Net.Http.Formatting, remove the "old" reference to System.Net.Http.Formatting from your project and install this package: https://www.nuget.org/packages/Microsoft.AspNet.WebApi.Client/

Here is the exception he was getting.

System.AggregateException was caught
  HResult=-2146233088
  Message=One or more errors occurred.
  Source=mscorlib
  StackTrace:
       at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
       at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
       at System.Threading.Tasks.Task.Wait()
       at <namespace-was-here>.UserInactivityMonitor.Program.Main(String[] args) in <file-path-was-here>\Program.cs:line 33
  InnerException: System.IO.FileLoadException
       HResult=-2146234304
       Message=Could not load file or assembly 'Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
       Source=System.Net.Http.Formatting
       FileName=Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed
       FusionLog==== Pre-bind state information ===
LOG: DisplayName = Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed
 (Fully-specified)
LOG: Appbase = file:///<file-path-was-here>/bin/Debug/
LOG: Initial PrivatePath = NULL
Calling assembly : System.Net.Http.Formatting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: <config file name was here>
LOG: Using host configuration file: 
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Post-policy reference: Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed
LOG: Attempting download of new URL file:///<application-path-was-here>/bin/Debug/Newtonsoft.Json.DLL.
WRN: Comparing the assembly name resulted in the mismatch: Major Version
ERR: Failed to complete setup of assembly (hr = 0x80131040). Probing terminated.
       StackTrace:
            at System.Net.Http.Formatting.JsonMediaTypeFormatter..ctor()
            at System.Net.Http.Formatting.MediaTypeFormatterCollection.CreateDefaultFormatters()
            at System.Net.Http.HttpContentExtensions.ReadAsAsync[T](HttpContent content)
            at <namespace-was-here>.UserInactivityController.<GetAccessToken>d__1e.MoveNext() in <file-path-was-here>\UserInactivityController.cs:line 130
         --- End of stack trace from previous location where exception was thrown ---
            at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
            at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
            at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
            at <namesapce-was-here>.UserInactivityController.<GetInactiveUsers>d__b.MoveNext() in <file-path-was-here>\UserInactivityController.cs:line 58
         --- End of stack trace from previous location where exception was thrown ---
            at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
            at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
            at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
            at <namesapce-was-here>.UserInactivityController.<InactivateUsers>d__3.MoveNext() in <file-path-was-here>\UserInactivityController.cs:line 36
         --- End of stack trace from previous location where exception was thrown ---
            at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
            at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
            at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
            at <namesapce-was-here>.UserInactivityController.<Run>d__0.MoveNext() in <file-path-was-here>\UserInactivityController.cs:line 29
       InnerException: 
 
Once again, Stack Overflow was very helpful in many questions, but it didn’t give the full answer (which is why I’m posting this). Posted on Tuesday, December 15, 2015 3:18 PM WebApi , JSON | Back to top


Comments on this post: Tip for a WebApi, Json and System.Net.Http.Formatting exception

# re: Tip for a WebApi, Json and System.Net.Http.Formatting exception
Requesting Gravatar...
I had run into this a few times, particularly when mucking about in LinqPad, and forgot what I did to fix it. Thanks for sharing :)
Left by Ian Yates on Dec 17, 2015 7:52 PM

Your comment:
 (will show your gravatar)


Copyright © Aligned | Powered by: GeeksWithBlogs.net