找不到方法:'System.Collections.ObjectModel.Collection`1

时间:2019-06-12 18:33:19

标签: c# web-services

嗨,我在发布WebService时遇到此跟随错误,

  • 未找到方法: 'System.Collections.ObjectModel.Collection`1 System.Net.Http.Formatting.MediaTypeFormatter.get_SupportedMediaTypes()'。 enter image description here

但是当我在VS调试模式下使用它时,不会发生。 我已经用谷歌搜索并尝试在webconfig上使用程序集引用,但是它没有用 有任何建议吗?

这是我当前的webconfig文件

<?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>

这是引发错误的类

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 });
    }
}

1 个答案:

答案 0 :(得分:1)

问题不是System.Net.Http.Formatting,而是System.Net.Http。添加以下程序集重定向:

<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>

您的情况下,newVersion可能有所不同。您也可以尝试4.2.0.0