MissingMethodException Global.asax.cs

时间:2018-11-26 14:37:09

标签: c# asp.net-web-api2 servicestack servicestack-text missingmethodexception

由于此博客文章:

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/

Localhost并处于调试模式,一切正常,直到我将其部署到服务器上,它说:

  

MissingMethodException

     

[MissingMethodException:找不到方法:“ System.Collections.ObjectModel.Collection 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;
    }
}

还有我的Register方法:

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

1 个答案:

答案 0 :(得分:1)

所以我取得了一些进展。该问题不是由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.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的吸气剂。我还认为,整个问题是由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>