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()'.
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>
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With