gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 18:59:24 26 4
gpt4 key购买 nike

我希望有人能帮助我解决这个问题。我正在实现一个 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 但没有任何反应。说实话,我已经没有主意了,因为这段代码实际上是在应用程序控制台中运行的。

我猜是某个东西的一些参数,如果有人能建议我如何修复它,我将非常感激。

谢谢!

最佳答案

此处最好的选择是将您尝试序列化的类分解为单独的类库,并从您的函数中引用该类。

如果您在不同的程序集中实现上面的 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));
}

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

您可以在此处找到有关外部引用的更多信息:https://learn.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();
}
}

关于azure - Azure 函数中标识符 'Submission#0' 不符合 CLS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41697453/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com