When using XML validation in Blazor (with .NET 5), if an error is generated, the error message is reported as expected in a local development environment (debug or release modes) but incorrectly in a production environment (i.e. published to a static blob - no ASP.NET server)
In the code below, I'm intentionally generating an error to illustrate the problem.
protected override async Task OnAfterRenderAsync(bool firstRender)
{
var xml = "I am not XML";
// Create the XmlReader object from the string
using (var reader = XmlReader.Create(new XmlTextReader(new StringReader(xml)), new XmlReaderSettings
{
ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings
}))
{
try
{
while (reader.Read()) ;
}
catch (XmlException exception)
{
Console.WriteLine (exception.Message);
}
}
The expected error message is:
Data at the root level is invalid. Line 1, position 1.
However, in a published/production environment this is:
Xml_MessageWithErrorPosition, Xml_InvalidRootData, 1, 1
I am guessing that these Xml_ references are internal Microsoft string references that are not available in the published environment and that Blazor doesn't include these to keep the download light, but I am wondering:
Why is there a different behaviour here? How can I reproduce the published environment behaviour locally? Is it possible to deploy the required modules required to get these error messages to be displayed. How can I identify what these are and ensure they are deployed?
Issue explained at https://github.com/dotnet/runtime/issues/49308