Search code examples
azureasp.net-coreswaggerazure-service-fabric

Invalid Character reading xml file from Azure ServiceFabric Service


When I add Swagger (v2.5) on any Web Api service of my Azure ServiceFabric cluster, and the swagger documentation file contains accent marks (á, é, í...), I get this error:

Invalid character in the given encoding

I was passing the uri (path) of the xml file to swagger:

services.AddSwaggerGen(options =>
{
    ...
    options.IncludeXmlComments(myxmlpath);
    ...
}

But according to this post I should pass a StreamReader to specify the encoding, so I swapped the code for this:

services.AddSwaggerGen(options =>
{
    ...
    options.IncludeXmlComments(() => new XPathDocument(
                    new StreamReader(myxmlpath, Encoding.UTF8)));
    ...
}

In this way I managed to avoid the error, but in the swagger UI I see strange characters. For example, instead of áéíóú it shows:

����������

I don't understand why it works in my local cluster but it gives me encoding issues in my Azure cluster. I have not managed to reproduce the error in my local cluster.

Why the file parsing depends in any way on the node where the service is deployed (local or Azure)?

This is my entire stack trace before swapping the code:

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

Solution

  • I finally found the problem. It was not a problem in the Azure environment. I had a release step in VSTS Replace Tokens with the Files Encoding option in auto. I changed it to utf-8 and it works!

    enter image description here