简体   繁体   English

MissingMethodException Global.asax.cs

[英]MissingMethodException Global.asax.cs

Because of this blog-post:因为这篇博文:

https://www.radenkozec.com/8-ways-improve-asp-net-web-api-performance/ https://www.radenkozec.com/8-ways-improve-asp-net-web-api-performance/

I´ve tried to replace JSON.net with ServiceStack.Text as JSON-Serializer in my WebApi.我试图在我的 WebApi 中用 ServiceStack.Text 作为 JSON-Serializer 替换 JSON.net。 With this tutorial:通过本教程:

https://www.strathweb.com/2013/01/replace-json-net-with-servicestack-text-in-asp-net-web-api/ https://www.strathweb.com/2013/01/replace-json-net-with-servicestack-text-in-asp-net-web-api/

Localhost and in debug-mode all went well, until I deployed it to our server, it says:本地主机和调试模式一切顺利,直到我将它部署到我们的服务器,它说:

MissingMethodException缺少方法异常

[MissingMethodException: Method not found: "System.Collections.ObjectModel.Collection<System.Net.Http.DelegatingHandler> System.Web.Http.HttpConfiguration.get_MessageHandlers()".] [MissingMethodException:找不到方法:“System.Collections.ObjectModel.Collection<System.Net.Http.DelegatingHandler> System.Web.Http.HttpConfiguration.get_MessageHandlers()”。]

It happens at Application_Start().它发生在 Application_Start()。

protected void Application_Start()
{
    GlobalConfiguration.Configure(WebApiConfig.Register);

}

Thats my replacement:那是我的替代品:

public class ServiceStackTextFormatter : JsonMediaTypeFormatter
{
    public ServiceStackTextFormatter()
    {
        JsConfig.DateHandler = DateHandler.ISO8601;
        SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/json"));

        SupportedEncodings.Add(new UTF8Encoding(encoderShouldEmitUTF8Identifier: false, throwOnInvalidBytes: true));
        SupportedEncodings.Add(new UnicodeEncoding(bigEndian: false, byteOrderMark: true, throwOnInvalidBytes: true));
    }

    public override bool CanReadType(Type type)
    {
        if (type == null) throw new ArgumentNullException("type");
        return true;
    }

    public override bool CanWriteType(Type type)
    {
        if (type == null) throw new ArgumentNullException("type");
        return true;
    }

    public override Task<object> ReadFromStreamAsync(Type type, Stream readStream, System.Net.Http.HttpContent content, IFormatterLogger formatterLogger)
    {
        var task = Task<object>.Factory.StartNew(() => JsonSerializer.DeserializeFromStream(type, readStream));
        return task;
    }

    public override Task WriteToStreamAsync(Type type, object value, Stream writeStream, System.Net.Http.HttpContent content, TransportContext transportContext)
    {
        var task = Task.Factory.StartNew(() => JsonSerializer.SerializeToStream(value, type, writeStream));
        return task;
    }
}

And my Register method:还有我的注册方法:

public static void Register(HttpConfiguration config)
    {
        // see this: https://www.strathweb.com/2013/01/replace-json-net-with-servicestack-text-in-asp-net-web-api/
        // and this: https://www.radenkozec.com/8-ways-improve-asp-net-web-api-performance/
        // ServiceStackText is much faster than JSON.NET
        config.Formatters.RemoveAt(0);
        config.Formatters.Insert(0, new ServiceStackTextFormatter());

        // add Handler to send data chunked
        config.MessageHandlers.Add(new Handler());

        // Web API configuration and services
        config.SuppressDefaultHostAuthentication();
        config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));

        // Web API routes
        config.MapHttpAttributeRoutes();

        config.EnableCors();      // needed to disable this, otherwise we do not get a access-origin-header in the client
        
        config.Formatters[0].SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/json"));
        config.Formatters[0].SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/plain"));
        config.Formatters[0].SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));

        (config.Formatters[0] as ServiceStackTextFormatter).SerializerSettings.TypeNameHandling = TypeNameHandling.Auto;
    }

So I made some progress.所以我取得了一些进展。 The problem was not caused by the ServiceStack-JSON-Serializer, it's caused by my Handler which is:问题不是由 ServiceStack-JSON-Serializer 引起的,而是由我的 Handler 引起的,它是:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using System.Web;

namespace akiliBase.Rest.RestAPI.Models
{
    public class Handler : DelegatingHandler
    {
        protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request,
            CancellationToken cancellationToken)
        {
            var response = base.SendAsync(request, cancellationToken);

            response.Result.Headers.TransferEncodingChunked = true; // Here!

            return response;
        }
    }
}

So I deleted this line and will ask another question about this.所以我删除了这一行,并会就此提出另一个问题。

Now I get the following error:现在我收到以下错误:

[MissingMethodException: Method not found: "System.Collections.ObjectModel.Collection`1<System.Net.Http.Headers.MediaTypeHeaderValue> System.Net.Http.Formatting.MediaTypeFormatter.get_SupportedMediaTypes()".] [MissingMethodException:找不到方法:“System.Collections.ObjectModel.Collection`1<System.Net.Http.Headers.MediaTypeHeaderValue> System.Net.Http.Formatting.MediaTypeFormatter.get_SupportedMediaTypes()”。]

which is caused by the following lines:这是由以下几行引起的:

config.Formatters[0].SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/json"));
config.Formatters[0].SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/plain"));
config.Formatters[0].SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));

So it seams in System.Net.Http.Formatting.MediaTypeFormatter the getter for SupportedMediaTypes is missing.所以它在System.Net.Http.Formatting.MediaTypeFormatter接缝,缺少 SupportedMediaTypes 的 getter。 I also think the whole problem is caused in Web.config where some wrong assemblies are referenced or something.我也认为整个问题是在 Web.config 中引起的,其中引用了一些错误的程序集或其他东西。 So here is my web.config-runtime-tag:所以这是我的 web.config-runtime-tag:

<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
  <dependentAssembly>
    <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-11.0.0.0" newVersion="11.0.0.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="Microsoft.Build.Framework" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-15.1.0.0" newVersion="15.1.0.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System" publicKeyToken="b77a5c561934e089" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Net.Http.Formatting" publicKeyToken="31bf3856ad364e35" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-5.2.4.0" newVersion="5.2.4.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-5.2.4.0" newVersion="5.2.4.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="Microsoft.Owin.Security" publicKeyToken="31bf3856ad364e35" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="Microsoft.Owin.Security.OAuth" publicKeyToken="31bf3856ad364e35" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Web.Cors" publicKeyToken="31bf3856ad364e35" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-5.2.6.0" newVersion="5.2.6.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="Microsoft.Owin.Security.Cookies" publicKeyToken="31bf3856ad364e35" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="ZedGraph" publicKeyToken="02a83cbd123fcd60" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-5.1.7.430" newVersion="5.1.7.430" />
  </dependentAssembly>
</assemblyBinding>
</runtime>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM