来自 Android 设备的一些请求使我们的 servicestack.net 服务失败,并出现以下异常:
System.Runtime.Serialization.SerializationException: Could not deserialize 'application/json' request using CSharpRequestMappedObject'
Error: System.Threading.ThreadAbortException: Thread was being aborted.
at System.Web.Hosting.UnsafeIISMethods.MgdSyncReadRequest(IntPtr pHandler, Byte[] pBuffer, Int32 offset, Int32 cbBuffer, Int32& pBytesRead)
at System.Web.Hosting.IIS7WorkerRequest.ReadEntityCoreSync(Byte[] buffer, Int32 offset, Int32 size)
at System.Web.HttpRequest.GetEntireRawContent()
at System.Web.HttpRequest.get_InputStream()
at ServiceStack.WebHost.Endpoints.Support.EndpointHandlerBase.CreateContentTypeRequest(IHttpRequest httpReq, Type requestType, String contentType)
at ServiceStack.WebHost.Endpoints.Support.EndpointHandlerBase.CreateContentTypeRequest(IHttpRequest httpReq, Type requestType, String contentType)
at ServiceStack.WebHost.Endpoints.RestHandler.GetRequest(IHttpRequest httpReq, IRestPath restPath)
at ServiceStack.WebHost.Endpoints.RestHandler.ProcessRequest(IHttpRequest httpReq, IHttpResponse httpRes, String operationName)
我知道它与无法将 Post'ed json 数据解析为 CSharpRequestMappedObject 有关。
像这样映射:
[Route("/RequestPath/", Verbs = "POST"), UsedImplicitly]
public class CSharpRequestMappedObject
{
public string Name{ get; set; }
public IList<SomeClass> Items{ get; set; }
public bool State { get; set; }
public string SpecialType { get; set; } //Not required
}
我的问题是如何找出请求有什么问题?
它只是有时会发生 - 我在我的服务器日志中看到它,但我无法重现它。
我在 global.asax 中试过这个:
public override void Configure(Container container)
{
ServiceExceptionHandler = (request, ex) =>
{
//Enrich exceptions with request body data.
var propertyInfo = HttpContext.Current.Request.GetType().GetProperty("EntityBody", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
var postBody = Encoding.UTF8.GetString((byte[])propertyInfo.GetValue(HttpContext.Current.Request, null));
throw new Exception("Request body: " + postBody, ex);
};
}
它在其他异常上添加请求正文数据 - 但该异常并未在此处捕获。(我有一些其他的日志可以捕捉到后者的异常 - elmah)