- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
因为这篇博文:
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/
我正在开发 ASP.NET Web 应用程序,对于未处理的异常,我正在使用Global.asax 文件 我在其中编写了将错误日志写为 的逻辑 Sub Application_Error(ByVal s
Tell me about the difference between global.asax and global.asax.cs ? 和 If i click the both file in
在我的本地发布文件夹中,我有 Global.asax 和 Global.asax.cs,其中 Global.asax 未更新(日期为一个月前)和 Global.asax .cs 已更新。 我检查了 G
我在 2 台开发计算机上安装了一个 ASP.NET MVC 应用程序。该应用程序在两台计算机上运行良好(使用本地 IIS)。直到最近,我经常从一台或另一台计算机在生产服务器上发布而没有问题。从3周前开
我正在尝试将代码添加到我没有源代码的第 3 方 ASP.NET Web 应用程序的 application_start 事件。我通过从供应商的 Global 类继承来做到这一点。这是代码(注意“new
在我们使用 WebAPI 构建“简单”API 的冒险过程中,我们遇到了与任何项目一样的问题,但是我无法找到可以解释以下行为的任何此类资源:细节 : 带有 Update 2 的 Visual Studi
我有一个仅适用于 Firefox 的登录页面。它不适用于任何其他浏览器。 当我检查事件日志时,我看到“响应在当前上下文中不可用”。它告诉我错误在 global.asax 文件中,在 response.
我构建了一个包含 Global.asax 文件的 WCF 应用程序。我添加了 AspNetCompatibilityRequirements(RequirementsMode = AspNetComp
有一个Global.asax asp.net 应用程序根目录中的文件。 扩展命名背后的原因是什么? 我可以创建其他文件来使用这个扩展吗?出于什么目的? 最佳答案 Global.asax分机 stand
是否可以在 global.asax 中捕获回收事件? 我知道 Application_End 会被触发,但有没有办法知道它是由应用程序池的回收触发的? 谢谢,Lieven Cardoen 又名 Joh
我想创建一个全局选项,当 REST 调用包含 &format=json 以将响应输出为 JSON 字符串时。 如果我在我的方法中输入以下字符串,它会起作用: WebOperationContext.C
我计划使用 System.Data 包的全局导入。在我添加之后 在 global.asax 的标记中 在我的一个类中,我删除了代码顶部的“使用 System.Data”。 我构建了 global.a
我在asp.net和C#中有Web应用程序 我试图处理异常,如果它们在此应用程序内的任何地方发生。 例如,假设行为应为if和发生这种异常 //generate your fictional excep
我无法调试 global.asax 文件! 我在 Application_Start() 方法中有一些代码,但是当我在该方法中设置断点时,它会被忽略! 这正常吗? 最佳答案 中断 Applicatio
我试图从数据库中检查用户是否属于某人的好友列表并相应地重定向他。 我在 Global Asax 调用的路由处理程序中执行此操作。 我只想知道如何在路由处理程序类(或全局 asax)中获取用户名(从登录
我添加了一个 global.asax 文件,后来我决定不需要它,所以我将它从项目中排除,但现在我在尝试运行我的项目时遇到解析器错误。 下面是 View 上显示的错误: Line 1: 发生了什么以
我想在我的 global.asax 中使用带有 response.redirecttoroute 的自定义路由,但它不起作用。我的 RouteConfig 中有以下内容: routes.MapRout
全局 asax 文件有什么用?我希望声明将在整个应用程序页面中使用的特定于用户的对象字典。我在哪里声明这个字典? 谢谢。 最佳答案 Global.asax 包含一个代表整个应用程序的类。如果您的词典是
我正在开发一个 HttpModule 并使用 Global.asax。我开发了一个派生自 HttpApplication 的类,但我的类的 Application_Start 方法从不执行(我在 Ap
是否可以将 Global.asax 文件嵌入到程序集中?当前,它表示为 Web 根目录中的一个文件。但我想将它作为资源存储在程序集中。 最佳答案 我不确定。您是否尝试过将 Build Action 设
我是一名优秀的程序员,十分优秀!