Web Api 2 run-time error when trying to use .netst

2019-05-27 02:37发布

问题:

I have an Asp.net WebApi 2 site referencing .net 4.6.1. Up till now the site was referencing a PCL assembly (portable class library), for compatibility with UWP & Xamarin, without issues. Now I am trying to change that PCL to a NetStandard2.0 assembly, as recommended, but am receiving an error at site start up.

The error is below -

Method not found: 'System.Collections.ObjectModel.Collection`1 System.Web.Http.HttpConfiguration.get_MessageHandlers()'.

Any ideas? Thanks

回答1:

I was getting the same thing. I added this to my web.config and it worked. <dependentAssembly> <assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-4.2.0.0" newVersion="4.2.0.0" /> </dependentAssembly>

Oh the joys of Binding Redirects.