c# – 为什么我不能在南希注册自定义JSON.Net实现?

我安装了以下内容:

  <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.

请注意我想知道为什么这个解决方案失败了,因为我遵循官方doc而不是做其他实现,即实现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<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>();
}

最佳答案 为什么您认为未使用自定义序列化程序?

我可以看到设置为CustomJsonNetSerializer的断点被命中.

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

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

如果我更改DateFormatHandling

DateFormatHandling = DateFormatHandling.MicrosoftDateFormat;

变更将按预期反映:

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

你能确保你的应用程序配置正确:

您的应用程序的根命名空间中有Startup类(是OWIN吗?).使用OwinStartup批注注释Startup类.就像是:

[组件: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>();
        }
    }

验证所有包的版本:

<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>
点赞