代码在VS中有效,但在Azure Functions中失败

时间:2017-02-08 01:41:21

标签: json.net azure-functions

以下代码适用于Visual Studio,但失败并带有

Functions.HttpTriggerCSharp1. Newtonsoft.Json: Type specified in JSON 'JliffModel.Segment, JliffModel, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' is not compatible with 'JliffModel.ISubUnit, JliffModel, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. Path 'units[0].segments[0].$type', line 9, position 28.

在Azure功能中。这是函数运行时的问题吗?

var model2 = new JliffModel.File("en-US", "de-DE",
        new List<JliffModel.Unit>()
        {
            new JliffModel.Unit("1",
                new JliffModel.Segment(
                    new List<JliffModel.IElement>() {new JliffModel.TextElement("New fluent source.")},
                    new List<JliffModel.IElement>() {new JliffModel.TextElement("New fluent target.")}
                )
            ),
            new JliffModel.Unit("2", new List<JliffModel.ISubUnit>() {
                new JliffModel.Segment(
                    new List<JliffModel.IElement>() {new JliffModel.TextElement("Unit 2, Segment 1 source")},
                    new List<JliffModel.IElement>() {new JliffModel.TextElement("Unit 2, Segment 1 target")}
                ),
                new JliffModel.Ignorable()
            })
        });

    var binder = new JliffModel.JliffSerializationBinder("JliffModel.{0}, JliffModel");

    string output = JsonConvert.SerializeObject(model2, 
        Formatting.Indented,
        new JsonSerializerSettings
        {
            ContractResolver = new CamelCasePropertyNamesContractResolver(),
            TypeNameHandling = TypeNameHandling.Auto,
            Binder = binder
        });

    var model = JsonConvert.DeserializeObject<JliffModel.File>(output,
                new JsonSerializerSettings
                {
                    TypeNameHandling = TypeNameHandling.Auto,
                    Binder = binder
                });

1 个答案:

答案 0 :(得分:0)

你可能在天蓝色函数上使用不同版本的Newtonsoft.Json local。在日志中,您可以看到加载了哪个版本,例如:

xxxx-xx-xxT19:02:52.800 Installing Newtonsoft.Json x.x.x.

看看是否有任何差异。您可以在project.json文件中放置特定版本:

{
  "frameworks": {
    "net46":{
      "dependencies": {
        "Newtonsoft.Json": "9.0.1"
      }
    }
   }
}