SoapCore icon indicating copy to clipboard operation
SoapCore copied to clipboard

XmlNamespaceOverrides doesn't apply for Message Header

Open wwdenis opened this issue 3 years ago • 0 comments

Current Behavior

  • The configuraiton on XmlNamespaceOverrides does not work on all elements of the Xml Response.
  • The element Header keeps the default prefixes (e.g. "s").
  • The elements Envelope and Body uses the configured prefixes.
<MY-CUSTOM-PREFIX:Envelope
  xmlns:MY-CUSTOM-PREFIX="http://schemas.xmlsoap.org/soap/envelope/"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

  <s:Header xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
    <ExtraInfo xmlns="http://tempuri.org/">Time: 15.07.2022 20:16:59</ExtraInfo>
  </s:Header>

  <MY-CUSTOM-PREFIX:Body>
    <MainInfo xmlns="http://tempuri.org/">This is the request</MainInfo>
  </MY-CUSTOM-PREFIX:Body>
</MY-CUSTOM-PREFIX:Envelope>

Expected Behavior

  • XmlNamespaceOverrides should apply for the elements Envelope, Body and Header

Proposed Solution

  • On CustomMessage, override OnWriteStartHeaders the same way done on OnWriteStartBody
<MY-CUSTOM-PREFIX:Envelope
  xmlns:MY-CUSTOM-PREFIX="http://schemas.xmlsoap.org/soap/envelope/"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

  <MY-CUSTOM-PREFIX:Header>
    <ExtraInfo xmlns="http://tempuri.org/">Time: 15.07.2022 20:33:26</ExtraInfo>
  </MY-CUSTOM-PREFIX:Header>

  <MY-CUSTOM-PREFIX:Body>
     <MainInfo xmlns="http://tempuri.org/">This is the request</MainInfo>
  </MY-CUSTOM-PREFIX:Body>
</MY-CUSTOM-PREFIX:Envelope>

Hot to Reproduce

Startup.cs

public void ConfigureServices(IServiceCollection services)
{
    services.AddSoapCore();
    services.TryAddSingleton<ISampleService, SampleService>();
    services.AddMvc();
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    var nsOverrides = new XmlNamespaceManager(new NameTable());
    nsOverrides.AddNamespace("MY-CUSTOM-PREFIX", "http://schemas.xmlsoap.org/soap/envelope/");

    var options = new SoapEncoderOptions()
    {
        XmlNamespaceOverrides = nsOverrides,
    };

    app.UseMvc();
    app.UseSoapEndpoint<ISampleService>($"/Sample.asmx", options, SoapSerializer.XmlSerializer);
}

ISampleService.cs

[ServiceContract]
public interface ISampleService
{
    [OperationContract]
    [return: MessageParameter(Name = "Sample")]
    SampleMessage Process(string info);
}

SampleService.cs

public class SampleService : ISampleService
{
    public SampleMessage Process(string info)
    {
        return new SampleMessage
        {
            Header = $"Time: {DateTime.Now}",
            Body = info,
        };
    }
}

SampleMessage.cs

[MessageContract(IsWrapped = false)]
public class SampleMessage
{
    [MessageHeader(Name = "ExtraInfo")]
    public string Header { get; set; }

    [MessageBodyMember(Name = "MainInfo")]
    public string Body { get; set; }
}

SOAP Request

<soapenv:Envelope 
 xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
 xmlns="http://tempuri.org/">
 <soapenv:Header/>
   <soapenv:Body>
     <Process>
       <info>This is the request</info>
     </Process>
   </soapenv:Body>
</soapenv:Envelope>

wwdenis avatar Jul 15 '22 18:07 wwdenis