标识符 'Submission#0' 在 Azure 函数中不符合 CLS

Identifier 'Submission#0' is not CLS-compliant in 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,但没有它们。我在 project.json 文件中将 buildOptions:warningsAsErrors 添加为 false,但没有任何反应。老实说,我 运行 没有想法,因为这段代码实际上在应用程序控制台中运行。

我猜是某个参数,如果有人能建议我如何修复它,我将不胜感激。

谢谢!

您最好的选择是将您尝试序列化的 class 分解为一个单独的 class 库,并从您的函数中引用它。

如果您在不同的程序集中实现上面的 App class,您的函数代码将如下所示:

#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();
    }
}