Blazor - 此框架不支持 'msxsl:script' 元素,因为它不支持 运行 时间代码生成
Blazor - 'msxsl:script' element is not supported on this framework because it does not support run-time code generation
我在 Blazor Webassembly 中使用 XslCompiledTransform 时遇到问题。
blazor.webassembly.js:1 crit:
Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
Unhandled exception rendering component: 'msxsl:script' element is not
supported on this framework because it does not support run-time code
generation
public async Task<string> TransformSomething(string xml, string kluczPakietu, string kodPakietu)
{
var xslFile = await _httpClient.GetByteArrayAsync("transform.xsl");
//change bytearray to string - valid is working here.
string transformataHTML = System.Text.Encoding.Default.GetString(xslFile);
XDocument oldDocument = XDocument.Parse(xml);
var newDocument = new XDocument();
using (var stringReader = new StringReader(transformataHTML))
{
using (XmlReader xsltReader = XmlReader.Create(stringReader))
{
XsltArgumentList argsList = new XsltArgumentList();
argsList.AddParam("SVG", "", "enabled");
argsList.AddParam("klucz", "", kluczPakietu);
argsList.AddParam("kod", "", kodPakietu);
XsltSettings xsltSettings = new XsltSettings();
xsltSettings.EnableDocumentFunction = true;
xsltSettings.EnableScript = true;
var transformer = new XslCompiledTransform();
XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load(xsltReader);
//here is exception
transformer.Load(xsltReader, xsltSettings, new XmlUrlResolver());
//transformer.Load(xsltReader);
using (XmlReader oldDocumentReader = oldDocument.CreateReader())
{
using (XmlWriter newDocumentWriter = newDocument.CreateWriter())
{
transformer.Transform(oldDocumentReader, argsList, newDocumentWriter);
}
}
}
}
return newDocument.ToString();
}
是否可以在 Blazor Webassembly 中实现?在 Blazor 服务器端,脚本有效。
XslCompiledTransform
会尝试在 run-time 生成新的 .NET 代码,并期望 JIT 将其编译为底层机器的本机代码。由于 WebAssembly 不支持这个(就像其他需要 AOT 的平台一样iOS),你不能使用它。
使用 XslTransform
class 而不是 XslCompiledTransform
在这些平台上使用 XSLT。
更新:另请注意,XSLT 中的脚本始终需要 run-time 代码生成,因此即使 XslCompiledTransform
会退回到 non-compiled 转换,脚本也仅针对 [=24] 实现=] 目前代码生成:
The XslCompiledTransform class supports embedded scripts using the
msxsl:script element. When the style sheet is loaded, any defined
functions are compiled to Microsoft intermediate language (MSIL) by
the Code Document Object Model (CodeDOM) and are executed during run
time. The assembly generated from the embedded script block is
separate than the assembly generated for the style sheet.
我在 Blazor Webassembly 中使用 XslCompiledTransform 时遇到问题。
blazor.webassembly.js:1 crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100] Unhandled exception rendering component: 'msxsl:script' element is not supported on this framework because it does not support run-time code generation
public async Task<string> TransformSomething(string xml, string kluczPakietu, string kodPakietu)
{
var xslFile = await _httpClient.GetByteArrayAsync("transform.xsl");
//change bytearray to string - valid is working here.
string transformataHTML = System.Text.Encoding.Default.GetString(xslFile);
XDocument oldDocument = XDocument.Parse(xml);
var newDocument = new XDocument();
using (var stringReader = new StringReader(transformataHTML))
{
using (XmlReader xsltReader = XmlReader.Create(stringReader))
{
XsltArgumentList argsList = new XsltArgumentList();
argsList.AddParam("SVG", "", "enabled");
argsList.AddParam("klucz", "", kluczPakietu);
argsList.AddParam("kod", "", kodPakietu);
XsltSettings xsltSettings = new XsltSettings();
xsltSettings.EnableDocumentFunction = true;
xsltSettings.EnableScript = true;
var transformer = new XslCompiledTransform();
XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load(xsltReader);
//here is exception
transformer.Load(xsltReader, xsltSettings, new XmlUrlResolver());
//transformer.Load(xsltReader);
using (XmlReader oldDocumentReader = oldDocument.CreateReader())
{
using (XmlWriter newDocumentWriter = newDocument.CreateWriter())
{
transformer.Transform(oldDocumentReader, argsList, newDocumentWriter);
}
}
}
}
return newDocument.ToString();
}
是否可以在 Blazor Webassembly 中实现?在 Blazor 服务器端,脚本有效。
XslCompiledTransform
会尝试在 run-time 生成新的 .NET 代码,并期望 JIT 将其编译为底层机器的本机代码。由于 WebAssembly 不支持这个(就像其他需要 AOT 的平台一样iOS),你不能使用它。
使用 XslTransform
class 而不是 XslCompiledTransform
在这些平台上使用 XSLT。
更新:另请注意,XSLT 中的脚本始终需要 run-time 代码生成,因此即使 XslCompiledTransform
会退回到 non-compiled 转换,脚本也仅针对 [=24] 实现=] 目前代码生成:
The XslCompiledTransform class supports embedded scripts using the msxsl:script element. When the style sheet is loaded, any defined functions are compiled to Microsoft intermediate language (MSIL) by the Code Document Object Model (CodeDOM) and are executed during run time. The assembly generated from the embedded script block is separate than the assembly generated for the style sheet.