4

Hi I'm getting this follow error on my WebService when I publish it,

  • Method not found: 'System.Collections.ObjectModel.Collection`1 System.Net.Http.Formatting.MediaTypeFormatter.get_SupportedMediaTypes()'. enter image description here

but when I'm using it on VS debug mode it doesn't happen. I already googled it and tried to use assembly reference on webconfig but it didn't work any sugestion ?

This is my current webconfig file

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
<system.web>
    <customErrors mode="Off"/>
</system.web>
<system.webServer>
    <httpErrors errorMode="Detailed" />
</system.webServer>

  <system.web>
    <httpRuntime executionTimeout="3000000" maxRequestLength="1048576" />
    <compilation debug="true" targetFramework="4.0">
      <assemblies>
        <add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      </assemblies>
    </compilation>
    <authentication mode="Windows" />
    <pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID" />
  </system.web>
  <runtime>
       <dependentAssembly>
           <assemblyIdentity name="System.Net.Http.Formatting" publicKeyToken="B03F5F7F11D50A3A" culture="neutral"/>
           <bindingRedirect oldVersion="0.0.0.0-5.2.7.0" newVersion="5.2.7.0"/>
        </dependentAssembly>
  </runtime>
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="basicHttp" allowCookies="true" maxReceivedMessageSize="20000000" maxBufferSize="20000000" maxBufferPoolSize="20000000">
          <readerQuotas maxDepth="32" maxArrayLength="200000000" maxStringContentLength="200000000" />
        </binding>
      </basicHttpBinding>
    </bindings>
  </system.serviceModel>


</configuration>

and this is the class that is throwing the error

class WebApiConfig
{
    public static void Register(HttpConfiguration configuration)
    {
        configuration.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));

        configuration.Routes.MapHttpRoute("API Default", "api/{controller}/{id}",
            new { id = RouteParameter.Optional });
    }
}
  • Can you show the web.config adding also the "assembly reference"? (I do not know what you mean by that) – Florin-Constantin Ciubotariu Jun 12 '19 at 19:10
  • @CiubotariuFlorin I think OP is referring to a [binding redirect](https://stackoverflow.com/questions/49117030/binding-redirect-for-system-net-http-doesnt-work-why). OP: you may want to add version numbers (visual studio, .net framework, dll version) – Lennart Stoop Jun 12 '19 at 19:26
  • Hey! I just updated the web.config code the version that I'm using is assembly System.Net.Http.Formatting, Version=5.2.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 – Marcos Brinner pikatoons Jun 12 '19 at 19:34

1 Answers1

10

The problem is not with System.Net.Http.Formatting but with System.Net.Http. Add the following assembly redirect:

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

The newVersion may be different in your case. You can try 4.2.0.0 also.

  • hmmm it didn't work too, I removed this line and it worked I'm not sure but it seems to be happening only in my local iis when I publish it, on the serve it worked properly thanks in advance – Marcos Brinner pikatoons Jun 12 '19 at 20:08
  • You should try to fix it in your local environment. This is the kind of issue you will experience again and it will be better if you know how to tackle it. Can you tell us the version of System.Net.Http? (Go to the web project > References, search for System.Net.Http > Properties) – Florin-Constantin Ciubotariu Jun 12 '19 at 20:12
  • Yeah that's true, the version is 4.2.0.0, I also check the nuget version and it is installed at the lastest – Marcos Brinner pikatoons Jun 12 '19 at 20:20