Skip to content

Commit

Permalink
Merge pull request #1086 from andersjonsson/develop
Browse files Browse the repository at this point in the history
Hopefully fix issues with resolving scoped dependencies
  • Loading branch information
andersjonsson authored Oct 3, 2024
2 parents 6f49042 + ee5cf53 commit 6d9a2d4
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 19 deletions.
11 changes: 8 additions & 3 deletions src/SoapCore.Tests/InvalidXMLTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ public async Task MissingNamespace()

var serviceCollection = new ServiceCollection();
serviceCollection.AddSingleton<DenialOfServiceProofOfConcept>();
serviceCollection.AddSoapCore();

var serviceProvider = serviceCollection.BuildServiceProvider();

var options = new SoapOptions()
{
Expand Down Expand Up @@ -63,7 +66,7 @@ public async Task MissingNamespace()
context.Request.Body = new MemoryStream(Encoding.UTF8.GetBytes(request), false);
context.Request.ContentType = "text/xml; charset=utf-8";

await soapCore.Invoke(context);
await soapCore.Invoke(context, serviceProvider.CreateScope().ServiceProvider);

// Assert
Assert.IsTrue(context.Response.Body.Length > 0);
Expand Down Expand Up @@ -119,7 +122,7 @@ public async Task DuplicatedElement()
context.Request.Body = new MemoryStream(Encoding.UTF8.GetBytes(request), false);
context.Request.ContentType = "text/xml; charset=utf-8";

await soapCore.Invoke(context);
await soapCore.Invoke(context, serviceProvider.CreateScope().ServiceProvider);

// Assert
context.Response.Body.Seek(0, SeekOrigin.Begin);
Expand Down Expand Up @@ -147,6 +150,8 @@ public async Task InvalidSoapActionContentType(string contentType)
serviceCollection.AddSingleton<DuplicatedElementService>();
serviceCollection.AddSoapCore();

var serviceProvider = serviceCollection.BuildServiceProvider();

var options = new SoapOptions()
{
Path = "/Service.asmx",
Expand Down Expand Up @@ -184,7 +189,7 @@ public async Task InvalidSoapActionContentType(string contentType)
context.Request.Body = new MemoryStream(Encoding.UTF8.GetBytes(request), false);
context.Request.ContentType = contentType;

await soapCore.Invoke(context);
await soapCore.Invoke(context, serviceProvider.CreateScope().ServiceProvider);

// Assert
context.Response.Body.Seek(0, SeekOrigin.Begin);
Expand Down
4 changes: 2 additions & 2 deletions src/SoapCore.Tests/SerializerProviderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public async Task UsingDefaultSerializer()
context.Request.Body = new MemoryStream(Encoding.UTF8.GetBytes(request), false);
context.Request.ContentType = "text/xml; charset=utf-8";

await soapCore.Invoke(context);
await soapCore.Invoke(context, serviceCollection.BuildServiceProvider().CreateScope().ServiceProvider);

// Assert
Assert.IsTrue(context.Response.Body.Length > 0);
Expand Down Expand Up @@ -122,7 +122,7 @@ public async Task UsingCustomXMLSerializer()
context.Request.Body = new MemoryStream(Encoding.UTF8.GetBytes(request), false);
context.Request.ContentType = "text/xml; charset=utf-8";

await soapCore.Invoke(context);
await soapCore.Invoke(context, serviceCollection.BuildServiceProvider().CreateScope().ServiceProvider);

// Assert
Assert.IsTrue(context.Response.Body.Length > 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ public async Task TrailingServicePath_WritesMessage_True()
SoapSerializer = SoapSerializer.DataContractSerializer
};

SoapEndpointMiddleware<CustomMessage> soapCore = new SoapEndpointMiddleware<CustomMessage>(logger, (innerContext) => Task.CompletedTask, options, new MockServiceProvider(true));
var serviceProvider = new MockServiceProvider(true);

SoapEndpointMiddleware<CustomMessage> soapCore = new SoapEndpointMiddleware<CustomMessage>(logger, (innerContext) => Task.CompletedTask, options, serviceProvider);

var context = new DefaultHttpContext();
context.Request.Path = new PathString("/DynamicPath/Service.svc");
Expand All @@ -50,7 +52,7 @@ public async Task TrailingServicePath_WritesMessage_True()

// Act
// MockServiceProvider(false) simulates registering the TrailingServicePathTuner in app startup
await soapCore.Invoke(context);
await soapCore.Invoke(context, serviceProvider);

// Assert
Assert.IsTrue(context.Response.Body.Length > 0);
Expand Down Expand Up @@ -92,7 +94,7 @@ public async Task TrailingServicePath_WritesMessage_False()

// Act
// MockServiceProvider(false) simulates registering the TrailingServicePathTuner in app startup
await soapCore.Invoke(context);
await soapCore.Invoke(context, new MockServiceProvider(true));

// Assert
Assert.IsFalse(context.Response.Body.Length > 0);
Expand Down Expand Up @@ -126,7 +128,9 @@ public async Task FullPath_WritesMessage_True()
SoapSerializer = SoapSerializer.DataContractSerializer
};

SoapEndpointMiddleware<CustomMessage> soapCore = new SoapEndpointMiddleware<CustomMessage>(logger, (innerContext) => Task.CompletedTask, options, new MockServiceProvider(false));
var serviceProvider = new MockServiceProvider(false);

SoapEndpointMiddleware<CustomMessage> soapCore = new SoapEndpointMiddleware<CustomMessage>(logger, (innerContext) => Task.CompletedTask, options, serviceProvider);

var context = new DefaultHttpContext();
context.Request.Path = new PathString("/v1/Service.svc");
Expand All @@ -135,7 +139,7 @@ public async Task FullPath_WritesMessage_True()

// Act
// MockServiceProvider(false) simulates not registering the TrailingServicePathTuner in app startup
await soapCore.Invoke(context);
await soapCore.Invoke(context, serviceProvider);

// Assert
Assert.IsTrue(context.Response.Body.Length > 0);
Expand Down
2 changes: 1 addition & 1 deletion src/SoapCore/SoapCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<Description>SOAP protocol middleware for ASP.NET Core</Description>
<Version>1.1.0.50</Version>
<Version>1.1.0.51</Version>
<Authors>Digital Design</Authors>
<TargetFrameworks>netstandard2.0;netstandard2.1;netcoreapp3.1;net5.0;net6.0</TargetFrameworks>
<PackageId>SoapCore</PackageId>
Expand Down
16 changes: 8 additions & 8 deletions src/SoapCore/SoapEndpointMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ public class SoapEndpointMiddleware<T_MESSAGE>
private readonly ILogger<SoapEndpointMiddleware<T_MESSAGE>> _logger;
private readonly RequestDelegate _next;
private readonly SoapOptions _options;
private readonly IServiceProvider _serviceProvider;
private readonly ServiceDescription _service;
private readonly StringComparison _pathComparisonStrategy;
private readonly SoapMessageEncoder[] _messageEncoders;
Expand Down Expand Up @@ -73,9 +72,8 @@ public SoapEndpointMiddleware(
_logger = logger;
_next = next;
_options = options;
_serviceProvider = serviceProvider;

var serializerResolver = _serviceProvider.GetService<IXmlSerializationHandlerResolver>();
var serializerResolver = serviceProvider.GetService<IXmlSerializationHandlerResolver>();
if (serializerResolver != null && _options.SerializerIdentifier != null)
{
_serializerHandler = serializerResolver(_options.SerializerIdentifier);
Expand All @@ -98,9 +96,9 @@ public SoapEndpointMiddleware(
}
}

public async Task Invoke(HttpContext httpContext)
public async Task Invoke(HttpContext httpContext, IServiceProvider scopedServiceProvider)
{
var trailPathTuner = _serviceProvider.GetService<TrailingServicePathTuner>();
var trailPathTuner = scopedServiceProvider.GetService<TrailingServicePathTuner>();

trailPathTuner?.ConvertPath(httpContext);

Expand Down Expand Up @@ -134,7 +132,7 @@ public async Task Invoke(HttpContext httpContext)
{
if (!string.IsNullOrWhiteSpace(remainingPath))
{
await ProcessHttpOperation(httpContext, _serviceProvider, remainingPath.Value.Trim('/'));
await ProcessHttpOperation(httpContext, scopedServiceProvider, remainingPath.Value.Trim('/'));
}
else if (httpContext.Request.Query.ContainsKey("xsd") && _options.WsdlFileOptions != null)
{
Expand Down Expand Up @@ -169,11 +167,11 @@ public async Task Invoke(HttpContext httpContext)
return;
}

await ProcessHttpOperation(httpContext, _serviceProvider, remainingPath.Value.Trim('/'));
await ProcessHttpOperation(httpContext, scopedServiceProvider, remainingPath.Value.Trim('/'));
}
else
{
await ProcessOperation(httpContext, _serviceProvider);
await ProcessOperation(httpContext, scopedServiceProvider);
}
}
}
Expand Down Expand Up @@ -498,6 +496,8 @@ private async Task<Message> ProcessMessage(Message requestMessage, SoapMessageEn
}

var correlationObjects2 = default(List<(IMessageInspector2 inspector, object correlationObject)>);

//Not sure that this extra scope is needed...
using (IServiceScope scope = serviceProvider.CreateScope())
{
var messageInspector2s = scope.ServiceProvider.GetServices<IMessageInspector2>();
Expand Down

0 comments on commit 6d9a2d4

Please sign in to comment.