【问题标题】:Method not found: 'System.Collections.ObjectModel.Collection`1找不到方法:'System.Collections.ObjectModel.Collection`1
【发布时间】:2019-06-13 03:59:45
【问题描述】:

您好,当我发布它时,我的 WebService 上出现此跟随错误,

  • 找不到方法: 'System.Collections.ObjectModel.Collection`1 System.Net.Http.Formatting.MediaTypeFormatter.get_SupportedMediaTypes()'。

但是当我在 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 });
    }
}

【问题讨论】:

  • 你能显示 web.config 添加“程序集参考”吗? (我不知道你的意思)
  • @CiubotariuFlorin 我认为 OP 指的是binding redirect。 OP:您可能需要添加版本号(visual studio、.net 框架、dll 版本)
  • 嘿!我刚刚更新了 web.config 代码,我正在使用的版本是程序集 System.Net.Http.Formatting, Version=5.2.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35

标签: c# web-services


【解决方案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>

在您的情况下,新版本可能会有所不同。你也可以试试4.2.0.0

【讨论】:

  • hmmm 它也不起作用,我删除了这一行并且它起作用了我不确定,但是当我发布它时它似乎只发生在我的本地 iis 中,在它工作正常的服务上提前致谢
  • 您应该尝试在本地环境中修复它。这是您将再次遇到的问题,如果您知道如何解决它会更好。您能告诉我们 System.Net.Http 的版本吗? (进入web项目>参考,搜索System.Net.Http>属性)
  • 是的,版本是4.2.0.0,我也查了nuget的版本,是最新安装的
猜你喜欢
  • 2014-02-15
  • 2020-11-25
  • 1970-01-01
  • 2019-07-26
  • 1970-01-01
  • 2021-10-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多