为什么我不能在Nancy中注册自定义JSON.Net实现? [英] Why can I not register a custom JSON.Net implementation in Nancy?

查看:123
本文介绍了为什么我不能在Nancy中注册自定义JSON.Net实现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经安装了以下软件:

I have the following installed:

  <package id="Nancy" version="1.4.2" targetFramework="net4" />
  <package id="Nancy.Owin" version="1.4.1" targetFramework="net4" />
  <package id="Nancy.Serialization.JsonNet" version="1.4.1" targetFramework="net4" />
  <package id="Newtonsoft.Json" version="7.0.1" targetFramework="net4" />
  <package id="Owin" version="1.0" targetFramework="net4" />

还有:

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<JsonSerializer, CustomJsonNetSerializer>();
}

最后到达路线:

public sealed class ApiBase : NancyModule
{
    public ApiBase() : base("api/")
    {
        Get["user/"] = o => Response.AsJson(new { Context.CurrentUser, Time = DateTime.UtcNow});
    }
}

但是,当运行此应用程序时,CustomJsonNetSerializer永远不会实例化,看起来Nancy正在使用其他实现,可能是ISerializer.

However when running this app, the CustomJsonNetSerializer is never instantiated, it looks like Nancy is using a different implementation possibly the ISerializer.

请注意,我要遵循官方的 doc a>而不是执行其他实现,即实现ISerializer.

Please note I want to know why this solution is failing as I am following the official doc rather than doing other implementation i.e implementing an ISerializer.

有什么主意吗?

[更新] 我不知道出了什么问题,所以我得出了结论:

[Update] I could not figure out what was wrong so I ended up with:

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<TModel>(string contentType, TModel model, Stream outputStream)
    {
        using (var streamWriter = new StreamWriter(outputStream))
        using(var jsonWriter = new JsonTextWriter(streamWriter))
        {
            Serialize(jsonWriter, model);
        }
    }

    public IEnumerable<string> Extensions { get { yield return "json"; } }
}

并且:

protected override void ConfigureApplicationContainer(TinyIoCContainer container)
{
    base.ConfigureApplicationContainer(container);

    container.Register<ISerializer, CustomJsonNetSerializer>();
}

推荐答案

您为什么不使用自定义序列化程序?

Why do you think that your custom serializer is not used?

我看到设置为CustomJsonNetSerializer的断点被击中.

I can see that breakpoint set to the CustomJsonNetSerializer is hit.

响应包含格式正确的JSON文件:

The response contains correctly formatted JSON file:

{
  "currentUser": null,
  "time": "2015-11-29T15:01:55.4669533Z"
}

如果我更改DateFormatHandling

DateFormatHandling = DateFormatHandling.MicrosoftDateFormat;

更改将按预期反映:

{
  "currentUser": null,
  "time": "\/Date(1448809002853)\/"
}

您可以确保已正确配置您的应用程序:

您的应用程序的根名称空间中有Startup类(它是OWIN吗?).使用OwinStartup注释对Startup类进行注释.像这样:

Could you make sure that your application is configured correctly:

You have Startup class in the root namespace of your application (is it OWIN?). Annotate Startup class with OwinStartup annotation. Something like:

[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<JsonSerializer, CustomJsonNetSerializer>();
        }
    }

验证软件包的所有版本:

Verify all the package's versions:

<packages>
  <package id="Microsoft.Owin" version="2.1.0" targetFramework="net4" />
  <package id="Microsoft.Owin.Host.SystemWeb" version="2.1.0" targetFramework="net4" />
  <package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net4" />
  <package id="Nancy" version="1.4.2" targetFramework="net4" />
  <package id="Nancy.Owin" version="1.4.1" targetFramework="net4" />
  <package id="Nancy.Serialization.JsonNet" version="1.4.1" targetFramework="net4" />
  <package id="Newtonsoft.Json" version="7.0.1" targetFramework="net4" />
  <package id="Owin" version="1.0" targetFramework="net4" />
</packages>

这篇关于为什么我不能在Nancy中注册自定义JSON.Net实现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆