捕获 Nancy 中的序列化错误
Capturing serialization error in Nancy
编辑: Issue on GitHub here.
第一次和 Nancy 一起玩,写了这个简单的端点来测试基于 Accept
header:
的内容协商
public HomeModule()
{
Get["/"] = _ => new { Foo = "Bar" };
}
使用 Postman,我设置 Accept: application/json
并且结果符合预期,而 Accept: text/xml
产生文本:
There was an error generating XML document
经过反复试验,我发现这是由匿名类型引起的,这是一个关于XmlSerializer 的单独问题。但是,我无法弄清楚如何在任何地方捕获此序列化错误。就像是 "swallowed" 在 Nancy 或 ASP.NET 的某个地方。以上消息作为文本返回给请求者,状态代码为 200 OK.
尽管已设置 Visual Studio 中断 所有 异常并连接到 pipelines.OnError
和 Application_OnError
,但我没有得到任何指示发生错误。我不确定这是否是一般序列化程序的问题,ASP.NET 或 Nancy(或者如果我遗漏了一些明显的东西)。
// in bootstrapper's ApplicationStartup method:
pipelines.OnError += (ctx, ex) => {
System.Diagnostics.Trace.WriteLine(ex?.ToString()); // doesn't fire
return null;
};
// in Global.asax:
protected void Application_Error(object sender, EventArgs e)
{
var err = Server.GetLastError();
System.Diagnostics.Trace.WriteLine(err?.Message);
}
为什么这个错误不是thrown/capturable?
Nancy 使用 .Net XmlSerializer
到 XML 序列化。并且 .Net XmlSerliazer
无法序列化匿名类型:
https://github.com/NancyFx/Nancy/wiki/Extending-Serialization-with-Converters
Nancy uses the .NET Framework's own built-in XmlSerializer
infrastructure to handle clients sending and receiving data using XML
as the transport format.
Can I serialize Anonymous Types as xml?
Sorry, you cannot. The XML Serializer pretty much just serializes public read-write types.
您将需要 return 像这样的 POCO(Plain-Old-CSharp-Object)
public class HomeModule:NancyModule
{
public class FooModel
{
public string Foo { get; set; }
}
public HomeApi()
{
Get("/", p =>
{
var r = new F { Foo = "Bar" };
return r;
});
}
}
或实施另一个XmlSerialiser
https://github.com/NancyFx/Nancy/wiki/Extending-Serialization-with-Converters
The design of XmlSerializer is quite
extensible, and the way in which it is extensible is different from
the JavaScriptConverter and JavaScriptPrimitiveConverter types that
JSON serialization employs. XmlSerializer is unaware of JSON
converters, and JavaScriptSerializer ignores XML-specific attributes.
Thus, extensions to XML serialization and to JSON serialization can
coexist in the same project without interfering with one another.
基于 Nancy 的内部结构。来自 Serialiser 的错误消息被写入输出,即 There was an error generating the XML document.
。但是任何地方都没有写异常的细节。
https://github.com/NancyFx/Nancy/blob/master/src/Nancy/Responses/DefaultXmlSerializer.cs
catch (Exception exception)
{
if (this.traceConfiguration.DisplayErrorTraces)
{
var bytes = Encoding.UTF8.GetBytes(exception.Message);
outputStream.Write(bytes, 0, exception.Message.Length);
}
}
为了查看错误,您需要通过在 Visual Studio 中关闭 Just-My-Code 来解决它。您可以通过以下步骤完成此操作:
Debug->Options
然后取消选中 Just-My-Code
然后前往Debug->Windows-Exception Settings (Ctrl+Alt+E)
。检查 Common Language Runtime Exceptions
编辑: Issue on GitHub here.
第一次和 Nancy 一起玩,写了这个简单的端点来测试基于 Accept
header:
public HomeModule()
{
Get["/"] = _ => new { Foo = "Bar" };
}
使用 Postman,我设置 Accept: application/json
并且结果符合预期,而 Accept: text/xml
产生文本:
There was an error generating XML document
经过反复试验,我发现这是由匿名类型引起的,这是一个关于XmlSerializer 的单独问题。但是,我无法弄清楚如何在任何地方捕获此序列化错误。就像是 "swallowed" 在 Nancy 或 ASP.NET 的某个地方。以上消息作为文本返回给请求者,状态代码为 200 OK.
尽管已设置 Visual Studio 中断 所有 异常并连接到 pipelines.OnError
和 Application_OnError
,但我没有得到任何指示发生错误。我不确定这是否是一般序列化程序的问题,ASP.NET 或 Nancy(或者如果我遗漏了一些明显的东西)。
// in bootstrapper's ApplicationStartup method:
pipelines.OnError += (ctx, ex) => {
System.Diagnostics.Trace.WriteLine(ex?.ToString()); // doesn't fire
return null;
};
// in Global.asax:
protected void Application_Error(object sender, EventArgs e)
{
var err = Server.GetLastError();
System.Diagnostics.Trace.WriteLine(err?.Message);
}
为什么这个错误不是thrown/capturable?
Nancy 使用 .Net XmlSerializer
到 XML 序列化。并且 .Net XmlSerliazer
无法序列化匿名类型:
https://github.com/NancyFx/Nancy/wiki/Extending-Serialization-with-Converters
Nancy uses the .NET Framework's own built-in XmlSerializer infrastructure to handle clients sending and receiving data using XML as the transport format.
Can I serialize Anonymous Types as xml?
Sorry, you cannot. The XML Serializer pretty much just serializes public read-write types.
您将需要 return 像这样的 POCO(Plain-Old-CSharp-Object)
public class HomeModule:NancyModule
{
public class FooModel
{
public string Foo { get; set; }
}
public HomeApi()
{
Get("/", p =>
{
var r = new F { Foo = "Bar" };
return r;
});
}
}
或实施另一个XmlSerialiser
https://github.com/NancyFx/Nancy/wiki/Extending-Serialization-with-Converters
The design of XmlSerializer is quite extensible, and the way in which it is extensible is different from the JavaScriptConverter and JavaScriptPrimitiveConverter types that JSON serialization employs. XmlSerializer is unaware of JSON converters, and JavaScriptSerializer ignores XML-specific attributes. Thus, extensions to XML serialization and to JSON serialization can coexist in the same project without interfering with one another.
基于 Nancy 的内部结构。来自 Serialiser 的错误消息被写入输出,即 There was an error generating the XML document.
。但是任何地方都没有写异常的细节。
https://github.com/NancyFx/Nancy/blob/master/src/Nancy/Responses/DefaultXmlSerializer.cs
catch (Exception exception)
{
if (this.traceConfiguration.DisplayErrorTraces)
{
var bytes = Encoding.UTF8.GetBytes(exception.Message);
outputStream.Write(bytes, 0, exception.Message.Length);
}
}
为了查看错误,您需要通过在 Visual Studio 中关闭 Just-My-Code 来解决它。您可以通过以下步骤完成此操作:
Debug->Options
然后取消选中 Just-My-Code
然后前往Debug->Windows-Exception Settings (Ctrl+Alt+E)
。检查 Common Language Runtime Exceptions