Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(wsdl-meta): add soap and soap12 namespaces if WSDL contains both #1047

Merged
merged 2 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/SoapCore.Tests/MessageContract/RawRequestSoap11And12.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using System.Drawing;
using System.Linq;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;
using System.Xml.XPath;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SoapCore.Tests.MessageContract.Models;

namespace SoapCore.Tests.MessageContract
{
[TestClass]
public class RawRequestSoap11And12
{
[TestMethod]
public async Task Soap11And12MessageContractGetWSDL_ShouldContainSoap11AndSoap12Namespaces()
{
using var host = CreateTestHost(typeof(TestService));
using var client = host.CreateClient();
using var res = host.CreateRequest("/Service11And12.asmx?wsdl").GetAsync().Result;

res.EnsureSuccessStatusCode();

var response = await res.Content.ReadAsStringAsync();
var root = XDocument.Parse(response);
Assert.AreEqual("http://schemas.xmlsoap.org/wsdl/soap/", root.Root.Attributes().FirstOrDefault(t => t.Name.LocalName == "soap").Value);
Assert.AreEqual("http://schemas.xmlsoap.org/wsdl/soap12/", root.Root.Attributes().FirstOrDefault(t => t.Name.LocalName == "soap12").Value);
}

private TestServer CreateTestHost(Type serviceType)
{
var webHostBuilder = new WebHostBuilder()
.UseStartup<Startup>()
.ConfigureServices(services => services.AddSingleton<IStartupConfiguration>(new StartupConfiguration(serviceType)));
return new TestServer(webHostBuilder);
}
}
}
28 changes: 28 additions & 0 deletions src/SoapCore.Tests/MessageContract/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Text;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
Expand Down Expand Up @@ -44,6 +45,33 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerF
{
x.UseSoapEndpoint(_serviceType, "/Service.svc", new SoapEncoderOptions(), SoapSerializer.DataContractSerializer);
x.UseSoapEndpoint(_serviceType, "/Service.asmx", new SoapEncoderOptions(), SoapSerializer.XmlSerializer);

x.UseSoapEndpoint(_serviceType, opt =>
{
opt.Path = "/Service11And12.asmx";
opt.SoapSerializer = SoapSerializer.XmlSerializer;
opt.CaseInsensitivePath = true;

opt.EncoderOptions =
[
new SoapEncoderOptions
{
BindingName = "Soap11",
PortName = "Soap11",
WriteEncoding = Encoding.UTF8,
MessageVersion = MessageVersion.Soap11WSAddressingAugust2004,
},
new SoapEncoderOptions
{
BindingName = "Soap12",
PortName = "Soap12",
WriteEncoding = Encoding.UTF8,
MessageVersion = MessageVersion.Soap12WSAddressingAugust2004,
}

];
});

x.UseSoapEndpoint(_serviceType, opt =>
{
opt.Path = "/ServiceWithAdditionalEnvelopeXmlnsAttributes.asmx";
Expand Down
1 change: 1 addition & 0 deletions src/SoapCore.Tests/SoapCore.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<IsPackable>false</IsPackable>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<WarningsAsErrors />
<LangVersion>latest</LangVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
8 changes: 7 additions & 1 deletion src/SoapCore.Tests/Wsdl/WsdlTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1183,7 +1183,13 @@ private async Task<string> GetWsdlFromMetaBodyWriter<T>(SoapSerializer serialize
: new MetaBodyWriter(service, baseUrl, xmlNamespaceManager, defaultBindingName, new[] { new SoapBindingInfo(MessageVersion.None, bindingName, portName) }, useMicrosoftGuid) as BodyWriter;
var encoder = new SoapMessageEncoder(MessageVersion.Soap12WSAddressingAugust2004, Encoding.UTF8, false, XmlDictionaryReaderQuotas.Max, false, false, null, bindingName, portName, true);
var responseMessage = Message.CreateMessage(encoder.MessageVersion, null, bodyWriter);
responseMessage = new MetaMessage(responseMessage, service, xmlNamespaceManager, defaultBindingName, false);
responseMessage = new MetaMessage(
responseMessage,
service,
xmlNamespaceManager,
defaultBindingName,
false,
[responseMessage.Version]);

var memoryStream = new MemoryStream();
await encoder.WriteMessageAsync(responseMessage, null, memoryStream, true);
Expand Down
22 changes: 15 additions & 7 deletions src/SoapCore/Meta/MetaMessage.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;
using System.ServiceModel.Channels;
using System.Xml;
using SoapCore.ServiceModel;
Expand All @@ -12,20 +13,23 @@ public class MetaMessage : Message
private readonly XmlNamespaceManager _xmlNamespaceManager;
private readonly string _bindingName;
private readonly bool _hasBasicAuthentication;
private readonly MessageVersion[] _soapVersions;

[Obsolete]
public MetaMessage(Message message, ServiceDescription service, Binding binding, XmlNamespaceManager xmlNamespaceManager)
: this(message, service, xmlNamespaceManager, binding?.Name, binding.HasBasicAuth())
: this(message, service, xmlNamespaceManager, binding?.Name, binding.HasBasicAuth(), [message.Version])
{
}

public MetaMessage(Message message, ServiceDescription service, XmlNamespaceManager xmlNamespaceManager, string bindingName, bool hasBasicAuthentication)
public MetaMessage(Message message, ServiceDescription service, XmlNamespaceManager xmlNamespaceManager,
string bindingName, bool hasBasicAuthentication, MessageVersion[] soapVersions)
{
_xmlNamespaceManager = xmlNamespaceManager;
_message = message;
_service = service;
_bindingName = bindingName;
_hasBasicAuthentication = hasBasicAuthentication;
_soapVersions = soapVersions;
}

public override MessageHeaders Headers => _message.Headers;
Expand All @@ -42,18 +46,22 @@ protected override void OnWriteStartEnvelope(XmlDictionaryWriter writer)
{
writer.WriteStartElement(_xmlNamespaceManager.LookupPrefix(Namespaces.WSDL_NS), "definitions", Namespaces.WSDL_NS);

// Soap11
if (Version == MessageVersion.Soap11 || Version == MessageVersion.Soap11WSAddressingAugust2004 || Version == MessageVersion.Soap11WSAddressingAugust2004)
var wroteSoapNamespace = false;
if (_soapVersions.Contains(MessageVersion.Soap11) ||
_soapVersions.Contains(MessageVersion.Soap11WSAddressingAugust2004))
{
WriteXmlnsAttribute(writer, Namespaces.SOAP11_NS);
wroteSoapNamespace = true;
}

// Soap12
else if (Version == MessageVersion.Soap12WSAddressing10 || Version == MessageVersion.Soap12WSAddressingAugust2004)
if (_soapVersions.Contains(MessageVersion.Soap12WSAddressing10) ||
_soapVersions.Contains(MessageVersion.Soap12WSAddressingAugust2004))
{
WriteXmlnsAttribute(writer, Namespaces.SOAP12_NS);
wroteSoapNamespace = true;
}
else

if(!wroteSoapNamespace)
{
throw new ArgumentOutOfRangeException(nameof(Version), "Unsupported MessageVersion encountered while writing envelope.");
}
Expand Down
4 changes: 3 additions & 1 deletion src/SoapCore/SoapEndpointMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -249,13 +249,15 @@ private async Task ProcessMeta(HttpContext httpContext, bool showDocumentation)

//assumption that you want soap12 if your service supports that
var messageEncoder = _messageEncoders.FirstOrDefault(me => me.MessageVersion == MessageVersion.Soap12WSAddressing10 || me.MessageVersion == MessageVersion.Soap12WSAddressingAugust2004) ?? _messageEncoders[0];
var soapVersions = _messageEncoders.Select(me => me.MessageVersion).Distinct().ToArray();

using var responseMessage = new MetaMessage(
Message.CreateMessage(messageEncoder.MessageVersion, null, bodyWriter),
_service,
GetXmlNamespaceManager(messageEncoder),
bindingName,
_options.UseBasicAuthentication);
_options.UseBasicAuthentication,
soapVersions);

if (showDocumentation)
{
Expand Down
Loading