标识符“提交#0”在Azure功能中不符合CLS

时间:2017-01-17 12:40:02

标签: azure azure-functions

我希望有人可以帮我解决这个问题。我正在实现一个Azure功能,我试图将XML消息序列化为.Net对象。这是我目前使用的代码:

public static void Run(string input, TraceWriter log) 
{
    System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(App));
    // more code here....
}
public class App
{
    public string DataB { get; set; }
}

但是,我总是遇到这个错误:

2017-01-17T12:21:35.173 Exception while executing function: Functions.ManualXmlToJson. mscorlib: Exception has been thrown by the target of an invocation. System.Xml: Identifier 'Submission#0' is not CLS-compliant.

参数名称:ident。

我尝试过没有它们的XmlAttributes。我在buildOptions:warningsAsErrors文件中添加了false project.json但没有任何反应。说实话,我没有想法,因为这段代码实际上是在App Console中运行。

我猜是某些参数,如果有人可以建议我如何修复它,我真的很感激。

谢谢!

1 个答案:

答案 0 :(得分:5)

这里你最好的选择是将你尝试序列化到一个单独的类库并从你的函数中引用它的类考虑因素。

如果您在不同的程序集中实现上面的App类,则您的功能代码如下所示:

#r "<yourassemblyname>.dll"

using System;
using <YourClassNamespace>;

public static void Run(string input, TraceWriter log) 
{
    System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(App));
}

上面的代码假设一个私有程序集引用,您可以将程序集上传到函数文件夹内的bin文件夹。

您可以在此处找到有关外部参考的更多信息:https://docs.microsoft.com/en-us/azure/azure-functions/functions-reference-csharp#referencing-external-assemblies

我打开一个问题来解决符合CLS的名称,所以这不会让人感到困惑: https://github.com/Azure/azure-webjobs-sdk-script/issues/1123

另一个值得尝试的选项(可以最大限度地减少您需要对代码进行的更改)是使用DataContractSerializer。您可以找到更多信息here

以下是使用DataContractSerializer(上面的类型)的函数的快速示例:

#r "System.Runtime.Serialization"

using System;
using System.Xml;
using System.Runtime.Serialization;

public static void Run(string input, TraceWriter log) 
{
   string xml = WriteObject(new App { DataB = "Test"});
   log.Info(xml);
}


[DataContract(Name = "App")]
public class App
{
    [DataMember]
    public string DataB { get; set; }
}




public static string WriteObject(App app)
{
    using (var output = new StringWriter())
    using (var writer = new XmlTextWriter(output) { Formatting = Formatting.Indented })
    {
        var serializer = new DataContractSerializer(typeof(App));
        serializer.WriteObject(writer, app);

        return output.GetStringBuilder().ToString();
    }
}
相关问题