来自 Azure ServiceFabric 服务的无效字符读取 xml 文件
Invalid Character reading xml file from Azure ServiceFabric Service
当我在我的 Azure ServiceFabric 集群的任何 Web Api 服务上添加 Swagger (v2.5) 并且 swagger 文档文件包含重音符号(á、é、í...)时,我得到此错误:
Invalid character in the given encoding
我正在将 xml 文件的 uri(路径)传递给 swagger:
services.AddSwaggerGen(options =>
{
...
options.IncludeXmlComments(myxmlpath);
...
}
但根据 this post 我应该通过 StreamReader 来指定编码,所以我为此交换了代码:
services.AddSwaggerGen(options =>
{
...
options.IncludeXmlComments(() => new XPathDocument(
new StreamReader(myxmlpath, Encoding.UTF8)));
...
}
这样我设法避免了错误,但是大摇大摆UI我看到了奇怪的字符。例如,它显示的不是 áéíóú
:
����������
我不明白为什么它在我的本地集群中有效,但它在我的 Azure 集群中给我带来了编码问题。我没有设法在我的本地集群中重现错误。
为什么文件解析在任何方面都取决于部署服务的节点(本地或 Azure)?
这是我在交换代码之前的整个堆栈跟踪:
Unhealthy event: SourceId='System.RA', Property='ReplicaOpenStatus', HealthState='Warning', ConsiderWarningAsError=false.
Replica had multiple failures during open on mynode01. API call: IStatelessServiceInstance.Open(); Error = System.Xml.XmlException (-2146232000)
Invalid character in the given encoding. Line 35, position 12.
at System.Xml.XmlTextReaderImpl.Throw(Exception e)
at System.Xml.XmlTextReaderImpl.Throw(String res, String arg)
at System.Xml.XmlTextReaderImpl.InvalidCharRecovery(Int32& bytesCount, Int32& charsCount)
at System.Xml.XmlTextReaderImpl.GetChars(Int32 maxCharsCount)
at System.Xml.XmlTextReaderImpl.ReadData()
at System.Xml.XmlTextReaderImpl.ParseText(Int32& startPos, Int32& endPos, Int32& outOrChars)
at System.Xml.XmlTextReaderImpl.ParseText()
at System.Xml.XmlTextReaderImpl.ParseElementContent()
at System.Xml.XmlTextReaderImpl.Read()
at System.Xml.XPath.XPathDocument.LoadFromReader(XmlReader reader, XmlSpace space)
at System.Xml.XPath.XPathDocument..ctor(String uri, XmlSpace space)
at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenOptions.<>c__DisplayClass31_0.b__0()
at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenOptions.CreateSwaggerProvider(IServiceProvider serviceProvider)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitTransient(TransientCallSite transientCallSite, ServiceProviderEngineScope scope)
at Microsoft.Extensions.Internal.ActivatorUtilities.ConstructorMatcher.CreateInstance(IServiceProvider provider)
at Microsoft.AspNetCore.Builder.UseMiddlewareExtensions.<>c__DisplayClass4_0.b__0(RequestDelegate next)
at Microsoft.AspNetCore.Builder.Internal.ApplicationBuilder.Build()
at Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()
at Microsoft.AspNetCore.Hosting.Internal.WebHost.StartAsync(CancellationToken cancellationToken)
at Microsoft.AspNetCore.Hosting.Internal.WebHost.Start()
at Microsoft.ServiceFabric.Services.Communication.AspNetCore.AspNetCoreCommunicationListener.OpenAsync(CancellationToken cancellationToken)
at Microsoft.ServiceFabric.Services.Runtime.StatelessServiceInstanceAdapter.OpenCommunicationListenersAsync(CancellationToken cancellationToken)
at Microsoft.ServiceFabric.Services.Runtime.StatelessServiceInstanceAdapter.System.Fabric.IStatelessServiceInstance.OpenAsync(IStatelessServicePartition partition, CancellationToken cancellationToken)
For more information see: http://aka.ms/sfhealth
终于找到问题了。这在 Azure 环境中不是问题。我在 VSTS Replace Tokens
中有一个发布步骤,在 auto
中有 Files Encoding
选项。我将其更改为 utf-8
并且有效!
当我在我的 Azure ServiceFabric 集群的任何 Web Api 服务上添加 Swagger (v2.5) 并且 swagger 文档文件包含重音符号(á、é、í...)时,我得到此错误:
Invalid character in the given encoding
我正在将 xml 文件的 uri(路径)传递给 swagger:
services.AddSwaggerGen(options =>
{
...
options.IncludeXmlComments(myxmlpath);
...
}
但根据 this post 我应该通过 StreamReader 来指定编码,所以我为此交换了代码:
services.AddSwaggerGen(options =>
{
...
options.IncludeXmlComments(() => new XPathDocument(
new StreamReader(myxmlpath, Encoding.UTF8)));
...
}
这样我设法避免了错误,但是大摇大摆UI我看到了奇怪的字符。例如,它显示的不是 áéíóú
:
����������
我不明白为什么它在我的本地集群中有效,但它在我的 Azure 集群中给我带来了编码问题。我没有设法在我的本地集群中重现错误。
为什么文件解析在任何方面都取决于部署服务的节点(本地或 Azure)?
这是我在交换代码之前的整个堆栈跟踪:
Unhealthy event: SourceId='System.RA', Property='ReplicaOpenStatus', HealthState='Warning', ConsiderWarningAsError=false.
Replica had multiple failures during open on mynode01. API call: IStatelessServiceInstance.Open(); Error = System.Xml.XmlException (-2146232000)
Invalid character in the given encoding. Line 35, position 12.
at System.Xml.XmlTextReaderImpl.Throw(Exception e)
at System.Xml.XmlTextReaderImpl.Throw(String res, String arg)
at System.Xml.XmlTextReaderImpl.InvalidCharRecovery(Int32& bytesCount, Int32& charsCount)
at System.Xml.XmlTextReaderImpl.GetChars(Int32 maxCharsCount)
at System.Xml.XmlTextReaderImpl.ReadData()
at System.Xml.XmlTextReaderImpl.ParseText(Int32& startPos, Int32& endPos, Int32& outOrChars)
at System.Xml.XmlTextReaderImpl.ParseText()
at System.Xml.XmlTextReaderImpl.ParseElementContent()
at System.Xml.XmlTextReaderImpl.Read()
at System.Xml.XPath.XPathDocument.LoadFromReader(XmlReader reader, XmlSpace space)
at System.Xml.XPath.XPathDocument..ctor(String uri, XmlSpace space)
at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenOptions.<>c__DisplayClass31_0.b__0()
at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenOptions.CreateSwaggerProvider(IServiceProvider serviceProvider)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitTransient(TransientCallSite transientCallSite, ServiceProviderEngineScope scope)
at Microsoft.Extensions.Internal.ActivatorUtilities.ConstructorMatcher.CreateInstance(IServiceProvider provider)
at Microsoft.AspNetCore.Builder.UseMiddlewareExtensions.<>c__DisplayClass4_0.b__0(RequestDelegate next)
at Microsoft.AspNetCore.Builder.Internal.ApplicationBuilder.Build()
at Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()
at Microsoft.AspNetCore.Hosting.Internal.WebHost.StartAsync(CancellationToken cancellationToken)
at Microsoft.AspNetCore.Hosting.Internal.WebHost.Start()
at Microsoft.ServiceFabric.Services.Communication.AspNetCore.AspNetCoreCommunicationListener.OpenAsync(CancellationToken cancellationToken)
at Microsoft.ServiceFabric.Services.Runtime.StatelessServiceInstanceAdapter.OpenCommunicationListenersAsync(CancellationToken cancellationToken)
at Microsoft.ServiceFabric.Services.Runtime.StatelessServiceInstanceAdapter.System.Fabric.IStatelessServiceInstance.OpenAsync(IStatelessServicePartition partition, CancellationToken cancellationToken)
For more information see: http://aka.ms/sfhealth
终于找到问题了。这在 Azure 环境中不是问题。我在 VSTS Replace Tokens
中有一个发布步骤,在 auto
中有 Files Encoding
选项。我将其更改为 utf-8
并且有效!