为什么 Blazor 应用程序中的 XML 验证会在 localhost 和 Azure 静态 Web 应用程序上提供不同的消息? [英] Why is XML validation in a Blazor application giving different messages on localhost and as an Azure Static Web App?
问题描述
编辑我在 https://github.com/GilShalit/XMLValidation
我正在 Blazor WebAssembly (TargetFramework=net5.0) 中构建 XML 编辑器.部分功能涉及验证 XML 的完整性并根据具有三个包含的复杂 xsd 架构.
I am building an XML editor in Blazor WebAssembly (TargetFramework=net5.0). Part of the functionality involves validating the XML for completeness and according to a complex xsd schema with three includes.
这些是我遵循的步骤:
- 通过为每个 xsd 调用以下方法,构建一个 XmlSchemaSet 并向其中添加 4 个架构:
private async Task loadSchema(string path, string nameSpace)
{
byte[] byteArrayS = await _client.GetByteArrayAsync(path);
Console.WriteLine($"{path}: {byteArrayS.Length}");
MemoryStream streamS = new MemoryStream(byteArrayS);
XmlReader xmlSchemaReader = XmlReader.Create(streamS);
schemaSet.Add(nameSpace, xmlSchemaReader);
}
- 使用以下命令初始化事件处理程序:
ValidationEventHandler eventHandler = new ValidationEventHandler(ValidationEventHandler);
- 将 XML 加载到 XmlDocument 中:
byte[] byteArrayX = Encoding.ASCII.GetBytes(await _editorTarget.GetValue());
MemoryStream streamX = new MemoryStream(byteArrayX);
XmlReader reader = XmlReader.Create(streamX);
XmlDocument document = new XmlDocument();
document.Load(reader);
- 根据 schemaSet 进行验证:
document.Schemas = schemaSet;
document.Validate(eventHandler);
第 3 步和第 4 步在 Try...Catch 块内运行,并在 XML 格式不正确(例如缺少结束标记)时在本地运行,document.Load(reader);
行产生错误,并显示如下消息:
ssteps 3 and 4 are run inside a Try...Catch block and running locally when the XML is not well formed (missing closing tag for example), the document.Load(reader);
line produces an error with a message like the following:
The 'publicationStmt1' start tag on line 9 position 11 does not match the end tag of 'publicationStmt'. Line 11, position 12.
这很棒.但在部署到 Azure 的应用程序中验证类似情况会产生以下错误消息:Xml_MessageWithErrorPosition, Xml_TagMismatchEx, 11, 12
.
which is great. But validating a similar situation in the application deployed to Azure produces the following error message:Xml_MessageWithErrorPosition, Xml_TagMismatchEx, 11, 12
.
当 document.Validate(eventHandler);
行运行时,事件处理程序中会捕获架构验证错误,典型的消息是:
Schema validation errors are caught in the event handler when the line document.Validate(eventHandler);
is run and a typical message is:
The element 'fileDesc' in namespace 'http://www.tei-c.org/ns/1.0' has invalid child element 'publicationStmt1' in namespace 'http://www.tei-c.org/ns/1.0'. List of possible elements expected: 'editionStmt, extent, publicationStmt' in namespace 'http://www.tei-c.org/ns/1.0'.
但在 Azure 上运行时,消息是 Sch_InvalidElementContentExpecting
.
But when run on Azure the message is Sch_InvalidElementContentExpecting
.
在本地运行和在 Azure 中运行的验证结果存在差异的原因是什么?
我试图通过添加来禁用链接:
I tried to disable linking by adding:
<ItemGroup>
<BlazorLinkerDescriptor Include="LinkerConfig.xml" />
</ItemGroup>
但这对部署的应用程序没有影响,使用 Release 而不是 Debug 在本地运行也没有改变任何东西.
But that did not make a difference in the deployed application, and running locally with Release instead of Debug did not change anything either.
我还确保从 Azure 运行时实际加载了 4 个 xsd 文件.
I also made sure the 4 xsd files are actually loaded when running from Azure.
推荐答案
所以这是一个功能而不是一个错误......
So this was a feature not a bug...
一个问题我在 Dev 社区上打开被 dotnet/runtime 团队拾取并添加到 GitHub 问题跟踪器这里.
An issue I opened on Dev community was picked up by the dotnet/runtime team and added to the GitHub issue tracker here.
事实证明,异常消息已被删除以节省大小.
It turns out exception messages are removed to save on size.
使用 <UseSystemResourceKeys>false</UseSystemResourceKeys>
启用异常消息,我必须说我没有看到大小增加.
Using <UseSystemResourceKeys>false</UseSystemResourceKeys>
enables exception messages and I must say I am not seeing an increase in size.
这篇关于为什么 Blazor 应用程序中的 XML 验证会在 localhost 和 Azure 静态 Web 应用程序上提供不同的消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!