Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webapi methodnotfound exception get_MessageHandlers

I have added two custom message handlers in my web api 2 application as shown here https://learn.microsoft.com/en-us/aspnet/web-api/overview/advanced/http-message-handlers

Then registeted them in Register method as,

config.MessageHandlers.Add(new LogHandler());
config.MessageHandlers.Add(new HeaderHandler());

Getting this error on startup:

Method not found: 'System.Collections.ObjectModel.Collection`1 
System.Web.Http.HttpConfiguration.get_MessageHandlers()'.
like image 505
Swanand Pangam Avatar asked Mar 03 '18 18:03

Swanand Pangam


2 Answers

I found the root cause of this issue to be .net standard references often bring in references to different versions of packages like System.Net.Http as a dependency for the package.

I ran into this when I updated my StackExchange.Redis. It ran fine locally but when deployed to the server it would crash.

For me to fix this I added a binding redirect for System.Net.Http

<dependentAssembly>
   <assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" />
   <bindingRedirect oldVersion="0.0.0.0-4.2.0.0" newVersion="4.2.0.0" />
</dependentAssembly>
like image 51
user1689716 Avatar answered Oct 21 '22 00:10

user1689716


After searching for hours I realised this happened due to adding a reference to .NET standard library project.

As soon as I removed the other project this error disappeared and application is running fine.

Also important point to note is, the issue simulates only when I add message handlers of my own and reference to a .Net standard library.

like image 27
Swanand Pangam Avatar answered Oct 20 '22 23:10

Swanand Pangam