簡體   English   中英

MissingMethodException Global.asax.cs

[英]MissingMethodException Global.asax.cs

因為這篇博文:

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

我試圖在我的 WebApi 中用 ServiceStack.Text 作為 JSON-Serializer 替換 JSON.net。 通過本教程:

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

本地主機和調試模式一切順利,直到我將它部署到我們的服務器,它說:

缺少方法異常

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

它發生在 Application_Start()。

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

}

那是我的替代品:

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

還有我的注冊方法:

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

所以我取得了一些進展。 問題不是由 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;
        }
    }
}

所以我刪除了這一行,並會就此提出另一個問題。

現在我收到以下錯誤:

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

這是由以下幾行引起的:

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

所以它在System.Net.Http.Formatting.MediaTypeFormatter接縫,缺少 SupportedMediaTypes 的 getter。 我也認為整個問題是在 Web.config 中引起的,其中引用了一些錯誤的程序集或其他東西。 所以這是我的 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