gpt4 book ai didi

c# - MissingMethodException 全局.asax.cs

转载 作者:太空宇宙 更新时间:2023-11-03 12:08:48 24 4
gpt4 key购买 nike

因为这篇博文:

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

我试图在我的 WebApi 中用 ServiceStack.Text 替换 JSON.net 作为 JSON-Serializer。通过本教程:

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

本地主机和 Debug模式一切顺利,直到我将它部署到我们的服务器,它说:

MissingMethodException

[MissingMethodException: Method not found: "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 引起的,它是由我的处理程序引起的:

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: Method not found:"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>

关于c# - MissingMethodException 全局.asax.cs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53483423/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com