为什么我不能在南希注册自定义JSON.Net实现?
我安装了以下内容:
并且:
public sealed class Startup { public void Configuration(IAppBuilder app) { var options = new NancyOptions(); app.UseNancy(options); } } public sealed class CustomJsonNetSerializer : JsonSerializer { public CustomJsonNetSerializer() { ContractResolver = new CamelCasePropertyNamesContractResolver(); DateFormatHandling = DateFormatHandling.IsoDateFormat; Formatting = Formatting.Indented; } }
然后在我的Bootstrapper
:
protected override void ConfigureApplicationContainer(TinyIoCContainer container) { base.ConfigureApplicationContainer(container); container.Register(); }
最后在路线上:
public sealed class ApiBase : NancyModule { public ApiBase() : base("api/") { Get["user/"] = o => Response.AsJson(new { Context.CurrentUser, Time = DateTime.UtcNow}); } }
但是,在运行此应用程序时, CustomJsonNetSerializer
从未实例化,看起来Nancy正在使用不同的实现,可能是ISerializer
。
请注意我想知道为什么这个解决方案失败了,因为我遵循官方文档而不是执行其他实现,即实现ISerializer
。
任何的想法?
[更新]我无法弄清楚出了什么问题所以我最终得到了:
public sealed class CustomJsonNetSerializer : JsonSerializer, ISerializer { public CustomJsonNetSerializer() { ContractResolver = new CamelCasePropertyNamesContractResolver(); DateFormatHandling = DateFormatHandling.IsoDateFormat; Formatting = Formatting.None; } public bool CanSerialize(string contentType) { return contentType.Equals("application/json", StringComparison.OrdinalIgnoreCase); } public void Serialize(string contentType, TModel model, Stream outputStream) { using (var streamWriter = new StreamWriter(outputStream)) using(var jsonWriter = new JsonTextWriter(streamWriter)) { Serialize(jsonWriter, model); } } public IEnumerable Extensions { get { yield return "json"; } } }
和:
protected override void ConfigureApplicationContainer(TinyIoCContainer container) { base.ConfigureApplicationContainer(container); container.Register(); }
为什么您认为未使用自定义序列化程序?
我可以看到设置为CustomJsonNetSerializer
断点被命中。
响应包含格式正确的JSON文件:
{ "currentUser": null, "time": "2015-11-29T15:01:55.4669533Z" }
如果我将DateFormatHandling
更改为
DateFormatHandling = DateFormatHandling.MicrosoftDateFormat;
变更将按预期反映:
{ "currentUser": null, "time": "/Date(1448809002853)/" }
你能确保你的应用程序配置正确:
您的应用程序的根命名空间中有Startup
类(是OWIN吗?)。 使用OwinStartup批注注释Startup
类。 就像是:
[assembly:OwinStartup(typeof(WebApplication3.Startup))]
public class Startup { public void Configuration(IAppBuilder app) { app.UseNancy(); } }
你只有一个Bootstrapper
public class Bootstrapper : DefaultNancyBootstrapper { protected override void ConfigureApplicationContainer(TinyIoCContainer container) { base.ConfigureApplicationContainer(container); container.Register(); } }
validation所有包的版本:
我遇到了同样的问题,基本上似乎CustomSerializer注册但是响应.AsJson它没有使用它。 我在模块中的构造函数中添加了注册的序列化程序,如果我手动序列化对象并发送AsText响应,它将起作用。 难道问题是方法AsJson没有使用自定义序列化程序吗?
所以,在引导程序中我会像你一样
protected override void ConfigureApplicationContainer(Nancy.TinyIoc.TinyIoCContainer container) { base.ConfigureApplicationContainer(container); container.Register(CustomJsonSerializer); }
但随后在模块中
public class SimpleModule : Nancy.NancyModule { Get["/State/"] = parameters => { string Json = TheSerializer.Serialize(myobject); return Response.AsText(Json); // return Response.AsJson(myobject); }; }
切换对响应的评论将以2个不同的JSON响应结束。 使用AsJson方法时出错。
上述就是C#学习教程:为什么我不能在南希注册自定义JSON.Net实现?分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/951930.html