Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method not found: 'System.Net.Http.Headers.MediaTypeHeaderValue System.Net.Http.Formatting.JsonMediaTypeFormatter.get_DefaultMediaType()'

I have a method named GetJSONResponse(...) and here's an extract:

var response = new HttpResponseMessage(httpResponseCode)
            {
                Content = objectToSerialize != null ?
                    new ObjectContent(objectToSerialize.GetType(), objectToSerialize, new JsonMediaTypeFormatter(), JsonMediaTypeFormatter.DefaultMediaType.MediaType)
                    : null
            };

Everything runs fine right up to when this method is called, then I get this error before the method is entered (perhaps ASP.NET compiles methods as needed?):

Method not found: 'System.Net.Http.Headers.MediaTypeHeaderValue System.Net.Http.Formatting.JsonMediaTypeFormatter.get_DefaultMediaType()'.

Originally I thought that a name space was missing from the web.config file (for the views), which I tried to add, but it didn't solve anything - then I realized I'm actually calling it via a WebAPI controller 'Get' method (not a view). The DLLs needed exist (copy to local is true), but this still seems to fail. It's quite obvious the error means a method cannot be found, but it compiles and launches fine since the c# file correctly references the type without issues ... any ideas?

like image 659
James Wilkins Avatar asked Apr 26 '16 01:04

James Wilkins


1 Answers

Found the issue: I had a duplicate dependent reference in the main web.config file:

Wrong One:

<dependentAssembly>
    <assemblyIdentity name="System.Net.Http"
        publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-2.2.29.0" newVersion="2.2.29.0" />
</dependentAssembly>

Right One:

<dependentAssembly>
    <assemblyIdentity name="System.Net.Http"
        publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
</dependentAssembly>

The correct one was much farther down the list so I didn't notice it was a duplicate. ;)

... come to think of it, it's also why the API Help Page had an issue. ApiDescriptionExtensions.GetFriendlyId() failed to be compiled and run due to this issue as well, but it works now. ;) Caused this error: Method not found: 'System.Net.Http.HttpMethod System.Web.Http.Description.ApiDescription.get_HttpMethod()'.

like image 162
James Wilkins Avatar answered Sep 28 '22 06:09

James Wilkins