从Azure ServiceFabric服务读取XML文件的字符无效

时间:2018-12-31 13:00:26

标签: azure asp.net-core swagger azure-service-fabric

当我在Azure ServiceFabric群集的任何Web Api服务上添加Swagger(v2.5),并且swagger文档文件包含重音符号(á,é,í...)时,出现此错误:

Invalid character in the given encoding

我正在将xml文件的uri(路径)传递给大张旗鼓:

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

1 个答案:

答案 0 :(得分:0)

我终于找到了问题。在Azure环境中这不是问题。我在VSTS Replace Tokens中进行了发布,并在Files Encoding中使用了auto选项。我将其更改为utf-8,它可以正常工作!

enter image description here