From 9021db8af1ea232ebccbef52e3a2da413cc62124 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20M=C3=B6rner?= Date: Thu, 3 Oct 2024 17:12:21 +0200 Subject: [PATCH 1/2] roll back some changes regarding IServiceProvider. We need a scoped service provider (we could define our own scope in the middleware, but why should we?) --- src/SoapCore.Tests/InvalidXMLTests.cs | 11 ++++++++--- src/SoapCore.Tests/SerializerProviderTests.cs | 4 ++-- .../TrailingServicePathTunerTests.cs | 14 +++++++++----- src/SoapCore/SoapEndpointMiddleware.cs | 16 ++++++++-------- 4 files changed, 27 insertions(+), 18 deletions(-) diff --git a/src/SoapCore.Tests/InvalidXMLTests.cs b/src/SoapCore.Tests/InvalidXMLTests.cs index ace93c76..1f3d1840 100644 --- a/src/SoapCore.Tests/InvalidXMLTests.cs +++ b/src/SoapCore.Tests/InvalidXMLTests.cs @@ -25,6 +25,9 @@ public async Task MissingNamespace() var serviceCollection = new ServiceCollection(); serviceCollection.AddSingleton(); + serviceCollection.AddSoapCore(); + + var serviceProvider = serviceCollection.BuildServiceProvider(); var options = new SoapOptions() { @@ -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); @@ -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); @@ -147,6 +150,8 @@ public async Task InvalidSoapActionContentType(string contentType) serviceCollection.AddSingleton(); serviceCollection.AddSoapCore(); + var serviceProvider = serviceCollection.BuildServiceProvider(); + var options = new SoapOptions() { Path = "/Service.asmx", @@ -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); diff --git a/src/SoapCore.Tests/SerializerProviderTests.cs b/src/SoapCore.Tests/SerializerProviderTests.cs index 5bcf6c64..4b3162df 100644 --- a/src/SoapCore.Tests/SerializerProviderTests.cs +++ b/src/SoapCore.Tests/SerializerProviderTests.cs @@ -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); @@ -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); diff --git a/src/SoapCore.Tests/TrailingServicePathTuner/TrailingServicePathTunerTests.cs b/src/SoapCore.Tests/TrailingServicePathTuner/TrailingServicePathTunerTests.cs index 32a08fa1..672d349b 100644 --- a/src/SoapCore.Tests/TrailingServicePathTuner/TrailingServicePathTunerTests.cs +++ b/src/SoapCore.Tests/TrailingServicePathTuner/TrailingServicePathTunerTests.cs @@ -41,7 +41,9 @@ public async Task TrailingServicePath_WritesMessage_True() SoapSerializer = SoapSerializer.DataContractSerializer }; - SoapEndpointMiddleware soapCore = new SoapEndpointMiddleware(logger, (innerContext) => Task.CompletedTask, options, new MockServiceProvider(true)); + var serviceProvider = new MockServiceProvider(true); + + SoapEndpointMiddleware soapCore = new SoapEndpointMiddleware(logger, (innerContext) => Task.CompletedTask, options, serviceProvider); var context = new DefaultHttpContext(); context.Request.Path = new PathString("/DynamicPath/Service.svc"); @@ -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); @@ -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); @@ -126,7 +128,9 @@ public async Task FullPath_WritesMessage_True() SoapSerializer = SoapSerializer.DataContractSerializer }; - SoapEndpointMiddleware soapCore = new SoapEndpointMiddleware(logger, (innerContext) => Task.CompletedTask, options, new MockServiceProvider(false)); + var serviceProvider = new MockServiceProvider(false); + + SoapEndpointMiddleware soapCore = new SoapEndpointMiddleware(logger, (innerContext) => Task.CompletedTask, options, serviceProvider); var context = new DefaultHttpContext(); context.Request.Path = new PathString("/v1/Service.svc"); @@ -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); diff --git a/src/SoapCore/SoapEndpointMiddleware.cs b/src/SoapCore/SoapEndpointMiddleware.cs index 786a855d..8347f78f 100644 --- a/src/SoapCore/SoapEndpointMiddleware.cs +++ b/src/SoapCore/SoapEndpointMiddleware.cs @@ -37,7 +37,6 @@ public class SoapEndpointMiddleware private readonly ILogger> _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; @@ -73,9 +72,8 @@ public SoapEndpointMiddleware( _logger = logger; _next = next; _options = options; - _serviceProvider = serviceProvider; - var serializerResolver = _serviceProvider.GetService(); + var serializerResolver = serviceProvider.GetService(); if (serializerResolver != null && _options.SerializerIdentifier != null) { _serializerHandler = serializerResolver(_options.SerializerIdentifier); @@ -98,9 +96,9 @@ public SoapEndpointMiddleware( } } - public async Task Invoke(HttpContext httpContext) + public async Task Invoke(HttpContext httpContext, IServiceProvider scopedServiceProvider) { - var trailPathTuner = _serviceProvider.GetService(); + var trailPathTuner = scopedServiceProvider.GetService(); trailPathTuner?.ConvertPath(httpContext); @@ -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) { @@ -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); } } } @@ -498,6 +496,8 @@ private async Task 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(); From ee5cf53c8e295f4055af6a4401956fed10eec3e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20M=C3=B6rner?= Date: Thu, 3 Oct 2024 17:18:57 +0200 Subject: [PATCH 2/2] v1.1.0.51 --- src/SoapCore/SoapCore.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SoapCore/SoapCore.csproj b/src/SoapCore/SoapCore.csproj index 4ecab157..cf02574b 100644 --- a/src/SoapCore/SoapCore.csproj +++ b/src/SoapCore/SoapCore.csproj @@ -2,7 +2,7 @@ SOAP protocol middleware for ASP.NET Core - 1.1.0.50 + 1.1.0.51 Digital Design netstandard2.0;netstandard2.1;netcoreapp3.1;net5.0;net6.0 SoapCore