From ccd94c54ebbdf7409d020f4049e74afa63d0c70e Mon Sep 17 00:00:00 2001 From: Julien Chomarat Date: Tue, 20 Feb 2018 16:57:53 +0100 Subject: [PATCH 01/10] Add the http header name holding the correlationID to the CorrelationContext --- src/CorrelationId/CorrelationContext.cs | 11 ++++++++++- src/CorrelationId/CorrelationContextFactory.cs | 4 ++-- src/CorrelationId/CorrelationIdMiddleware.cs | 2 +- src/CorrelationId/ICorrelationContextFactory.cs | 3 ++- 4 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/CorrelationId/CorrelationContext.cs b/src/CorrelationId/CorrelationContext.cs index dab91fd..d8c4d93 100644 --- a/src/CorrelationId/CorrelationContext.cs +++ b/src/CorrelationId/CorrelationContext.cs @@ -7,17 +7,26 @@ namespace CorrelationId /// public class CorrelationContext { - internal CorrelationContext(string correlationId) + internal CorrelationContext(string correlationId, string header) { if (string.IsNullOrEmpty(correlationId)) throw new ArgumentNullException(nameof(correlationId)); + if (string.IsNullOrEmpty(header)) + throw new ArgumentNullException(nameof(header)); + CorrelationId = correlationId; + Header = header; } /// /// The Correlation ID which is applicable to the current request. /// public string CorrelationId { get; } + + /// + /// The header where is stored the Correlation ID + /// + public string Header { get; } } } diff --git a/src/CorrelationId/CorrelationContextFactory.cs b/src/CorrelationId/CorrelationContextFactory.cs index 7e9cb9e..44e777f 100644 --- a/src/CorrelationId/CorrelationContextFactory.cs +++ b/src/CorrelationId/CorrelationContextFactory.cs @@ -11,9 +11,9 @@ public CorrelationContextFactory(ICorrelationContextAccessor correlationContextA } /// - public CorrelationContext Create(string correlationId) + public CorrelationContext Create(string correlationId, string header) { - var correlationContext = new CorrelationContext(correlationId); + var correlationContext = new CorrelationContext(correlationId, header); if (_correlationContextAccessor != null) { diff --git a/src/CorrelationId/CorrelationIdMiddleware.cs b/src/CorrelationId/CorrelationIdMiddleware.cs index 004cfc7..34217a5 100644 --- a/src/CorrelationId/CorrelationIdMiddleware.cs +++ b/src/CorrelationId/CorrelationIdMiddleware.cs @@ -35,7 +35,7 @@ public async Task Invoke(HttpContext context, ICorrelationContextFactory correla context.TraceIdentifier = correlationId; } - correlationContextFactory.Create(context.TraceIdentifier); + correlationContextFactory.Create(context.TraceIdentifier, _options.Value.Header); if (_options.Value.IncludeInResponse) { diff --git a/src/CorrelationId/ICorrelationContextFactory.cs b/src/CorrelationId/ICorrelationContextFactory.cs index 582ae3a..1008d49 100644 --- a/src/CorrelationId/ICorrelationContextFactory.cs +++ b/src/CorrelationId/ICorrelationContextFactory.cs @@ -9,8 +9,9 @@ public interface ICorrelationContextFactory /// Creates a new with the correlation ID set for the current request. /// /// The correlation ID to set on the context. + /// /// The header used to hold the correlation ID. /// A new instance of a . - CorrelationContext Create(string correlationId); + CorrelationContext Create(string correlationId, string header); /// /// Disposes of the for the current request. From 0a561e4d12eb3c4be155b4b09237d7f7ac108c71 Mon Sep 17 00:00:00 2001 From: Steve Gordon Date: Tue, 27 Mar 2018 17:15:41 +0100 Subject: [PATCH 02/10] Minor clarification of the XML comments --- src/CorrelationId/CorrelationContext.cs | 2 +- src/CorrelationId/CorrelationIdOptions.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/CorrelationId/CorrelationContext.cs b/src/CorrelationId/CorrelationContext.cs index d8c4d93..5e18821 100644 --- a/src/CorrelationId/CorrelationContext.cs +++ b/src/CorrelationId/CorrelationContext.cs @@ -25,7 +25,7 @@ internal CorrelationContext(string correlationId, string header) public string CorrelationId { get; } /// - /// The header where is stored the Correlation ID + /// The name of the header from which the Correlation ID is read/written. /// public string Header { get; } } diff --git a/src/CorrelationId/CorrelationIdOptions.cs b/src/CorrelationId/CorrelationIdOptions.cs index de0aec8..21a5bb1 100644 --- a/src/CorrelationId/CorrelationIdOptions.cs +++ b/src/CorrelationId/CorrelationIdOptions.cs @@ -8,7 +8,7 @@ public class CorrelationIdOptions private const string DefaultHeader = "X-Correlation-ID"; /// - /// The header field name where the correlation ID will be stored. + /// The name of the header from which the Correlation ID is read/written. /// public string Header { get; set; } = DefaultHeader; From 765c6acf6c71a3618c3fa24e1c12991e68cd535f Mon Sep 17 00:00:00 2001 From: Steve Gordon Date: Tue, 27 Mar 2018 17:16:07 +0100 Subject: [PATCH 03/10] Adding unit test around newly exposed property on the context --- .../CorrelationIdMiddlewareTests.cs | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/test/CorrelationId.Tests/CorrelationIdMiddlewareTests.cs b/test/CorrelationId.Tests/CorrelationIdMiddlewareTests.cs index 7e4717a..7e54636 100644 --- a/test/CorrelationId.Tests/CorrelationIdMiddlewareTests.cs +++ b/test/CorrelationId.Tests/CorrelationIdMiddlewareTests.cs @@ -6,6 +6,8 @@ using System.Text; using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; using Xunit; @@ -237,6 +239,34 @@ public async Task CorrelationId_ReturnedCorrectlyFromTransientService() Assert.Equal(splitContent2[0], splitContent2[1]); } + [Fact] + public async Task CorrelationContextIncludesHeaderValue_WhichMatchesTheOriginalOptionsValue() + { + var options = new CorrelationIdOptions { Header = "custom-header" }; + + var builder = new WebHostBuilder() + .Configure(app => + { + app.UseCorrelationId(options); + + app.Use(async (ctx, next) => + { + var accessor = ctx.RequestServices.GetService(); + await ctx.Response.WriteAsync(accessor.CorrelationContext.Header); + await next(); + }); + }) + .ConfigureServices(sc => sc.AddCorrelationId()); + + var server = new TestServer(builder); + + var response = await server.CreateClient().GetAsync(""); + + var body = await response.Content.ReadAsStringAsync(); + + Assert.Equal(body, options.Header); + } + private class SingletonClass { private readonly ICorrelationContextAccessor _correlationContext; From 09910520925f37d97ee9bdcbb9dc67599f694686 Mon Sep 17 00:00:00 2001 From: Steve Gordon Date: Wed, 28 Mar 2018 14:42:04 +0100 Subject: [PATCH 04/10] Adding two new configuration options 1. Option which determines whether the TraceIdentifier or a Guid is used for the CorrelationId when an ID is not present on the request header. 2. Option which determines whether th TraceIdentifier is updated to match the CorrelationId --- .../Controllers/ValuesController.cs | 3 +- .../CorrelationContextAccessor.cs | 1 + src/CorrelationId/CorrelationId.csproj | 10 +- src/CorrelationId/CorrelationIdMiddleware.cs | 31 +++-- src/CorrelationId/CorrelationIdOptions.cs | 20 ++++ .../CorrelationIdMiddlewareTests.cs | 113 +++++++++++++++++- 6 files changed, 163 insertions(+), 15 deletions(-) diff --git a/samples/MvcCorrelationIdSample/Controllers/ValuesController.cs b/samples/MvcCorrelationIdSample/Controllers/ValuesController.cs index 8c790f9..ab8c12a 100644 --- a/samples/MvcCorrelationIdSample/Controllers/ValuesController.cs +++ b/samples/MvcCorrelationIdSample/Controllers/ValuesController.cs @@ -32,7 +32,8 @@ public IEnumerable Get() $"DirectAccessor={correlation}", $"Transient={_transient.GetCorrelationFromScoped}", $"Scoped={_scoped.GetCorrelationFromScoped}", - $"Singleton={_singleton.GetCorrelationFromScoped}" + $"Singleton={_singleton.GetCorrelationFromScoped}", + $"TraceIdentifier={HttpContext.TraceIdentifier}" }; } } diff --git a/src/CorrelationId/CorrelationContextAccessor.cs b/src/CorrelationId/CorrelationContextAccessor.cs index 4a79c69..3ef0241 100644 --- a/src/CorrelationId/CorrelationContextAccessor.cs +++ b/src/CorrelationId/CorrelationContextAccessor.cs @@ -7,6 +7,7 @@ public class CorrelationContextAccessor : ICorrelationContextAccessor { private static AsyncLocal _correlationContext = new AsyncLocal(); + /// public CorrelationContext CorrelationContext { get => _correlationContext.Value; diff --git a/src/CorrelationId/CorrelationId.csproj b/src/CorrelationId/CorrelationId.csproj index 9f27662..8bfa477 100644 --- a/src/CorrelationId/CorrelationId.csproj +++ b/src/CorrelationId/CorrelationId.csproj @@ -11,12 +11,20 @@ git aspnetcore;correlationid en - 2.0.1 + 2.1.0 bin\Release\netstandard1.4\CorrelationId.xml https://github.com/stevejgordon/CorrelationId https://github.com/stevejgordon/CorrelationId + + latest + + + + latest + + diff --git a/src/CorrelationId/CorrelationIdMiddleware.cs b/src/CorrelationId/CorrelationIdMiddleware.cs index 34217a5..785b225 100644 --- a/src/CorrelationId/CorrelationIdMiddleware.cs +++ b/src/CorrelationId/CorrelationIdMiddleware.cs @@ -2,13 +2,18 @@ using Microsoft.Extensions.Options; using System; using System.Threading.Tasks; +using Microsoft.Extensions.Primitives; namespace CorrelationId { + /// + /// Middleware which attempts to reads / creates a Correlation ID that can then be used in logs and + /// passed to upstream requests. + /// public class CorrelationIdMiddleware { private readonly RequestDelegate _next; - private readonly IOptions _options; + private readonly CorrelationIdOptions _options; /// /// Creates a new instance of the CorrelationIdMiddleware. @@ -18,7 +23,7 @@ public class CorrelationIdMiddleware public CorrelationIdMiddleware(RequestDelegate next, IOptions options) { _next = next ?? throw new ArgumentNullException(nameof(next)); - _options = options ?? throw new ArgumentNullException(nameof(options)); + _options = options.Value ?? throw new ArgumentNullException(nameof(options)); } /// @@ -30,21 +35,24 @@ public CorrelationIdMiddleware(RequestDelegate next, IOptions public async Task Invoke(HttpContext context, ICorrelationContextFactory correlationContextFactory) { - if (context.Request.Headers.TryGetValue(_options.Value.Header, out var correlationId)) - { - context.TraceIdentifier = correlationId; - } + var correlationIdFoundInRequestHeader = context.Request.Headers.TryGetValue(_options.Header, out var correlationId); - correlationContextFactory.Create(context.TraceIdentifier, _options.Value.Header); + if (!correlationIdFoundInRequestHeader) + correlationId = GenerateCorrelationId(context.TraceIdentifier); + + if (_options.UpdateTraceIdentifier) + context.TraceIdentifier = correlationId; + + correlationContextFactory.Create(correlationId, _options.Header); - if (_options.Value.IncludeInResponse) + if (_options.IncludeInResponse) { // apply the correlation ID to the response header for client side tracking context.Response.OnStarting(() => { - if (!context.Response.Headers.ContainsKey(_options.Value.Header)) + if (!context.Response.Headers.ContainsKey(_options.Header)) { - context.Response.Headers.Add(_options.Value.Header, context.TraceIdentifier); + context.Response.Headers.Add(_options.Header, correlationId); } return Task.CompletedTask; @@ -55,5 +63,8 @@ public async Task Invoke(HttpContext context, ICorrelationContextFactory correla correlationContextFactory.Dispose(); } + + private StringValues GenerateCorrelationId(string traceIdentifier) => + _options.UseGuidForCorrelationId || string.IsNullOrEmpty(traceIdentifier) ? Guid.NewGuid().ToString() : traceIdentifier; } } diff --git a/src/CorrelationId/CorrelationIdOptions.cs b/src/CorrelationId/CorrelationIdOptions.cs index 21a5bb1..dced528 100644 --- a/src/CorrelationId/CorrelationIdOptions.cs +++ b/src/CorrelationId/CorrelationIdOptions.cs @@ -13,8 +13,28 @@ public class CorrelationIdOptions public string Header { get; set; } = DefaultHeader; /// + /// /// Controls whether the correlation ID is returned in the response headers. + /// + /// Default: true /// public bool IncludeInResponse { get; set; } = true; + + /// + /// + /// Controls whether the ASP.NET Core TraceIdentifier will be set to match the CorrelationId. + /// + /// Default: true + /// + public bool UpdateTraceIdentifier { get; set; } = true; + + /// + /// + /// Controls whether a GUID will be used in cases where no correlation ID is retrieved from the request header. + /// When false the TraceIdentifier for the current request will be used. + /// + /// Default: false. + /// + public bool UseGuidForCorrelationId { get; set; } = false; } } diff --git a/test/CorrelationId.Tests/CorrelationIdMiddlewareTests.cs b/test/CorrelationId.Tests/CorrelationIdMiddlewareTests.cs index 7e54636..a1a0fab 100644 --- a/test/CorrelationId.Tests/CorrelationIdMiddlewareTests.cs +++ b/test/CorrelationId.Tests/CorrelationIdMiddlewareTests.cs @@ -10,6 +10,7 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; using Xunit; +using System; namespace CorrelationId.Tests { @@ -52,7 +53,7 @@ public async Task DoesNotThrowException_WhenOptionSetToTrue_IfHeaderIsAlreadySet } [Fact] - public async Task DoesNotReturnCorrelationIdInResponseHeader_WhenOptionSetToFalse() + public async Task DoesNotReturnCorrelationIdInResponseHeader_WhenIncludeInResponseIsFalse() { var options = new CorrelationIdOptions { IncludeInResponse = false }; @@ -70,7 +71,7 @@ public async Task DoesNotReturnCorrelationIdInResponseHeader_WhenOptionSetToFals } [Fact] - public async Task CorrelationIdHeaderFieldName_MatchesOptions() + public async Task CorrelationIdHeaderFieldName_MatchesHeaderOption() { const string customHeader = "X-Test-Header"; @@ -90,7 +91,7 @@ public async Task CorrelationIdHeaderFieldName_MatchesOptions() } [Fact] - public async Task CorrelationIdHeaderFieldName_MatchesStringOverload() + public async Task CorrelationIdHeaderFieldName_MatchesHeaderFromStringOverload() { const string customHeader = "X-Test-Header"; @@ -129,6 +130,46 @@ public async Task CorrelationId_SetToCorrelationIdFromRequestHeader() Assert.Single(header, expectedHeaderValue); } + [Fact] + public async Task CorrelationId_SetToGuid_WhenUseGuidForCorrelationId_IsTrue() + { + var options = new CorrelationIdOptions { UseGuidForCorrelationId = true }; + + var builder = new WebHostBuilder() + .Configure(app => app.UseCorrelationId(options)) + .ConfigureServices(sc => sc.AddCorrelationId()); + + var server = new TestServer(builder); + + var response = await server.CreateClient().GetAsync(""); + + var header = response.Headers.GetValues(new CorrelationIdOptions().Header); + + var isGuid = Guid.TryParse(header.FirstOrDefault(), out _); + + Assert.True(isGuid); + } + + [Fact] + public async Task CorrelationId_NotSetToGuid_WhenUseGuidForCorrelationId_IsFalse() + { + var options = new CorrelationIdOptions { UseGuidForCorrelationId = false }; + + var builder = new WebHostBuilder() + .Configure(app => app.UseCorrelationId(options)) + .ConfigureServices(sc => sc.AddCorrelationId()); + + var server = new TestServer(builder); + + var response = await server.CreateClient().GetAsync(""); + + var header = response.Headers.GetValues(new CorrelationIdOptions().Header); + + var isGuid = Guid.TryParse(header.FirstOrDefault(), out _); + + Assert.False(isGuid); + } + [Fact] public async Task CorrelationId_ReturnedCorrectlyFromSingletonService() { @@ -267,6 +308,72 @@ public async Task CorrelationContextIncludesHeaderValue_WhichMatchesTheOriginalO Assert.Equal(body, options.Header); } + [Fact] + public async Task TraceIdentifier_IsNotUpdated_WhenUpdateTraceIdentifierIsFalse() + { + var options = new CorrelationIdOptions { UpdateTraceIdentifier = false }; + + var expectedHeaderName = new CorrelationIdOptions().Header; + const string expectedHeaderValue = "123456"; + + var builder = new WebHostBuilder() + .Configure(app => + { + app.UseCorrelationId(options); + + app.Use(async (ctx, next) => + { + await ctx.Response.WriteAsync(ctx.TraceIdentifier); + await next(); + }); + }) + .ConfigureServices(sc => sc.AddCorrelationId()); + + var server = new TestServer(builder); + + var request = new HttpRequestMessage(); + request.Headers.Add(expectedHeaderName, expectedHeaderValue); + + var response = await server.CreateClient().SendAsync(request); + + var body = await response.Content.ReadAsStringAsync(); + + Assert.NotEqual(body, expectedHeaderValue); + } + + [Fact] + public async Task TraceIdentifier_IsNotUpdated_WhenUpdateTraceIdentifierIsTrue() + { + var options = new CorrelationIdOptions { UpdateTraceIdentifier = true }; + + var expectedHeaderName = new CorrelationIdOptions().Header; + const string expectedHeaderValue = "123456"; + + var builder = new WebHostBuilder() + .Configure(app => + { + app.UseCorrelationId(options); + + app.Use(async (ctx, next) => + { + await ctx.Response.WriteAsync(ctx.TraceIdentifier); + await next(); + }); + }) + .ConfigureServices(sc => sc.AddCorrelationId()); + + var server = new TestServer(builder); + + var request = new HttpRequestMessage(); + request.Headers.Add(expectedHeaderName, expectedHeaderValue); + + var response = await server.CreateClient().SendAsync(request); + + var body = await response.Content.ReadAsStringAsync(); + + Assert.Equal(body, expectedHeaderValue); + } + private class SingletonClass { private readonly ICorrelationContextAccessor _correlationContext; From 39d440ab8c2192c9a9fcb80d9408865c5dcd5f08 Mon Sep 17 00:00:00 2001 From: Steve Gordon Date: Wed, 28 Mar 2018 15:05:20 +0100 Subject: [PATCH 05/10] Version to 2.1.0-rc --- src/CorrelationId/CorrelationId.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/CorrelationId/CorrelationId.csproj b/src/CorrelationId/CorrelationId.csproj index 8bfa477..0ed3a97 100644 --- a/src/CorrelationId/CorrelationId.csproj +++ b/src/CorrelationId/CorrelationId.csproj @@ -11,7 +11,7 @@ git aspnetcore;correlationid en - 2.1.0 + 2.1.0-rc bin\Release\netstandard1.4\CorrelationId.xml https://github.com/stevejgordon/CorrelationId https://github.com/stevejgordon/CorrelationId From 46cd1a6032feb8142f598a68dfbf12e665542516 Mon Sep 17 00:00:00 2001 From: Steve Gordon Date: Thu, 29 Mar 2018 14:34:16 +0100 Subject: [PATCH 06/10] Fixing case where empty header value on request could lead to ArgumentNullException If an empty value for the correlation header was sent, later when trying to create the CorrelationContext and ArgumentNullException would be thrown. We now check for a null or empty header value and in that case will generate an appropriate correlation id which will then be used. --- src/CorrelationId/CorrelationId.csproj | 2 +- src/CorrelationId/CorrelationIdMiddleware.cs | 23 ++++++++++++++------ 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/CorrelationId/CorrelationId.csproj b/src/CorrelationId/CorrelationId.csproj index 0ed3a97..5320af4 100644 --- a/src/CorrelationId/CorrelationId.csproj +++ b/src/CorrelationId/CorrelationId.csproj @@ -11,7 +11,7 @@ git aspnetcore;correlationid en - 2.1.0-rc + 2.1.0-rc2 bin\Release\netstandard1.4\CorrelationId.xml https://github.com/stevejgordon/CorrelationId https://github.com/stevejgordon/CorrelationId diff --git a/src/CorrelationId/CorrelationIdMiddleware.cs b/src/CorrelationId/CorrelationIdMiddleware.cs index 785b225..e20a4e7 100644 --- a/src/CorrelationId/CorrelationIdMiddleware.cs +++ b/src/CorrelationId/CorrelationIdMiddleware.cs @@ -32,17 +32,13 @@ public CorrelationIdMiddleware(RequestDelegate next, IOptions /// The for the current request. /// The which can create a . - /// public async Task Invoke(HttpContext context, ICorrelationContextFactory correlationContextFactory) { - var correlationIdFoundInRequestHeader = context.Request.Headers.TryGetValue(_options.Header, out var correlationId); + var correlationId = SetCorrelationId(context); - if (!correlationIdFoundInRequestHeader) - correlationId = GenerateCorrelationId(context.TraceIdentifier); - if (_options.UpdateTraceIdentifier) context.TraceIdentifier = correlationId; - + correlationContextFactory.Create(correlationId, _options.Header); if (_options.IncludeInResponse) @@ -54,7 +50,7 @@ public async Task Invoke(HttpContext context, ICorrelationContextFactory correla { context.Response.Headers.Add(_options.Header, correlationId); } - + return Task.CompletedTask; }); } @@ -64,6 +60,19 @@ public async Task Invoke(HttpContext context, ICorrelationContextFactory correla correlationContextFactory.Dispose(); } + private StringValues SetCorrelationId(HttpContext context) + { + var correlationIdFoundInRequestHeader = context.Request.Headers.TryGetValue(_options.Header, out var correlationId); + + if (RequiresGenerationOfCorrelationId(correlationIdFoundInRequestHeader, correlationId)) + correlationId = GenerateCorrelationId(context.TraceIdentifier); + + return correlationId; + } + + private static bool RequiresGenerationOfCorrelationId(bool idInHeader, StringValues idFromHeader) => + !idInHeader || StringValues.IsNullOrEmpty(idFromHeader); + private StringValues GenerateCorrelationId(string traceIdentifier) => _options.UseGuidForCorrelationId || string.IsNullOrEmpty(traceIdentifier) ? Guid.NewGuid().ToString() : traceIdentifier; } From f307969b63ff1a07190f91f06fc2e2394b69e747 Mon Sep 17 00:00:00 2001 From: Steve Gordon Date: Fri, 6 Apr 2018 12:45:26 +0100 Subject: [PATCH 07/10] Improving testability by adding ICorrelationContext This supports easier mocking/faking given that the CorrelationContext implementation has an internal ctor. --- src/CorrelationId/CorrelationContext.cs | 14 ++++---------- .../CorrelationContextAccessor.cs | 4 ++-- src/CorrelationId/CorrelationContextFactory.cs | 2 +- src/CorrelationId/CorrelationId.csproj | 2 +- src/CorrelationId/ICorrelationContext.cs | 18 ++++++++++++++++++ .../ICorrelationContextAccessor.cs | 6 +++--- .../ICorrelationContextFactory.cs | 10 +++++----- 7 files changed, 34 insertions(+), 22 deletions(-) create mode 100644 src/CorrelationId/ICorrelationContext.cs diff --git a/src/CorrelationId/CorrelationContext.cs b/src/CorrelationId/CorrelationContext.cs index 5e18821..43d6815 100644 --- a/src/CorrelationId/CorrelationContext.cs +++ b/src/CorrelationId/CorrelationContext.cs @@ -2,10 +2,8 @@ namespace CorrelationId { - /// - /// Provides access to per request correlation properties. - /// - public class CorrelationContext + /// + public class CorrelationContext : ICorrelationContext { internal CorrelationContext(string correlationId, string header) { @@ -19,14 +17,10 @@ internal CorrelationContext(string correlationId, string header) Header = header; } - /// - /// The Correlation ID which is applicable to the current request. - /// + /// public string CorrelationId { get; } - /// - /// The name of the header from which the Correlation ID is read/written. - /// + /// public string Header { get; } } } diff --git a/src/CorrelationId/CorrelationContextAccessor.cs b/src/CorrelationId/CorrelationContextAccessor.cs index 3ef0241..33ba081 100644 --- a/src/CorrelationId/CorrelationContextAccessor.cs +++ b/src/CorrelationId/CorrelationContextAccessor.cs @@ -5,10 +5,10 @@ namespace CorrelationId /// public class CorrelationContextAccessor : ICorrelationContextAccessor { - private static AsyncLocal _correlationContext = new AsyncLocal(); + private static AsyncLocal _correlationContext = new AsyncLocal(); /// - public CorrelationContext CorrelationContext + public ICorrelationContext CorrelationContext { get => _correlationContext.Value; set => _correlationContext.Value = value; diff --git a/src/CorrelationId/CorrelationContextFactory.cs b/src/CorrelationId/CorrelationContextFactory.cs index 44e777f..2a58bfb 100644 --- a/src/CorrelationId/CorrelationContextFactory.cs +++ b/src/CorrelationId/CorrelationContextFactory.cs @@ -11,7 +11,7 @@ public CorrelationContextFactory(ICorrelationContextAccessor correlationContextA } /// - public CorrelationContext Create(string correlationId, string header) + public ICorrelationContext Create(string correlationId, string header) { var correlationContext = new CorrelationContext(correlationId, header); diff --git a/src/CorrelationId/CorrelationId.csproj b/src/CorrelationId/CorrelationId.csproj index 5320af4..4d91663 100644 --- a/src/CorrelationId/CorrelationId.csproj +++ b/src/CorrelationId/CorrelationId.csproj @@ -11,7 +11,7 @@ git aspnetcore;correlationid en - 2.1.0-rc2 + 2.1.0-rc3 bin\Release\netstandard1.4\CorrelationId.xml https://github.com/stevejgordon/CorrelationId https://github.com/stevejgordon/CorrelationId diff --git a/src/CorrelationId/ICorrelationContext.cs b/src/CorrelationId/ICorrelationContext.cs new file mode 100644 index 0000000..f37f128 --- /dev/null +++ b/src/CorrelationId/ICorrelationContext.cs @@ -0,0 +1,18 @@ +namespace CorrelationId +{ + /// + /// Provides access to per request correlation properties. + /// + public interface ICorrelationContext + { + /// + /// The Correlation ID which is applicable to the current request. + /// + string CorrelationId { get; } + + /// + /// The name of the header from which the Correlation ID is read/written. + /// + string Header { get; } + } +} \ No newline at end of file diff --git a/src/CorrelationId/ICorrelationContextAccessor.cs b/src/CorrelationId/ICorrelationContextAccessor.cs index 1a20860..3e85af9 100644 --- a/src/CorrelationId/ICorrelationContextAccessor.cs +++ b/src/CorrelationId/ICorrelationContextAccessor.cs @@ -1,13 +1,13 @@ namespace CorrelationId { /// - /// Provides access to the for the current request. + /// Provides access to the for the current request. /// public interface ICorrelationContextAccessor { /// - /// The for the current request. + /// The for the current request. /// - CorrelationContext CorrelationContext { get; set; } + ICorrelationContext CorrelationContext { get; set; } } } diff --git a/src/CorrelationId/ICorrelationContextFactory.cs b/src/CorrelationId/ICorrelationContextFactory.cs index 1008d49..7443967 100644 --- a/src/CorrelationId/ICorrelationContextFactory.cs +++ b/src/CorrelationId/ICorrelationContextFactory.cs @@ -1,20 +1,20 @@ namespace CorrelationId { /// - /// A factory for creating and disposing an instance of a . + /// A factory for creating and disposing an instance of a . /// public interface ICorrelationContextFactory { /// - /// Creates a new with the correlation ID set for the current request. + /// Creates a new with the correlation ID set for the current request. /// /// The correlation ID to set on the context. /// /// The header used to hold the correlation ID. - /// A new instance of a . - CorrelationContext Create(string correlationId, string header); + /// A new instance of a . + ICorrelationContext Create(string correlationId, string header); /// - /// Disposes of the for the current request. + /// Disposes of the for the current request. /// void Dispose(); } From 7ac2cac2f1cf2fa668d4b713207bd08dc36a7ddf Mon Sep 17 00:00:00 2001 From: Steve Gordon Date: Fri, 6 Apr 2018 12:51:55 +0100 Subject: [PATCH 08/10] Fixing launchUrls for sample application --- .../Properties/launchSettings.json | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/samples/MvcCorrelationIdSample/Properties/launchSettings.json b/samples/MvcCorrelationIdSample/Properties/launchSettings.json index f159df5..095496b 100644 --- a/samples/MvcCorrelationIdSample/Properties/launchSettings.json +++ b/samples/MvcCorrelationIdSample/Properties/launchSettings.json @@ -9,21 +9,21 @@ }, "profiles": { "IIS Express": { - "commandName": "IISExpress", - "launchBrowser": true, - "launchUrl": "api/values", - "environmentVariables": { - "ASPNETCORE_ENVIRONMENT": "Development" - } + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "http://localhost:59922/api/values", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } }, "MvcCorrelationIdSample": { - "commandName": "Project", - "launchBrowser": true, - "launchUrl": "api/values", - "environmentVariables": { - "ASPNETCORE_ENVIRONMENT": "Development" - }, - "applicationUrl": "http://localhost:59923/" + "commandName": "Project", + "launchBrowser": true, + "launchUrl": "http://localhost:59923/api/values", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + }, + "applicationUrl": "http://localhost:59923/" } } } From 2e90d76244b17d7ffee0640486a2b1e98352133a Mon Sep 17 00:00:00 2001 From: Steve Gordon Date: Sun, 8 Apr 2018 14:23:10 +0100 Subject: [PATCH 09/10] Backing out of adding an interface for CorrelationContext That might prove to be breaking for some consumers of the library which I want to avoid in this release. Testing can be achieved by creating a CorrelationContext via the factory which should be sufficient. Will revisit if necessary. --- src/CorrelationId/CorrelationContext.cs | 14 ++++++++++---- .../CorrelationContextAccessor.cs | 4 ++-- src/CorrelationId/CorrelationContextFactory.cs | 15 +++++++++++++-- .../CorrelationIdServiceExtensions.cs | 3 +++ src/CorrelationId/ICorrelationContext.cs | 18 ------------------ .../ICorrelationContextAccessor.cs | 6 +++--- .../ICorrelationContextFactory.cs | 10 +++++----- 7 files changed, 36 insertions(+), 34 deletions(-) delete mode 100644 src/CorrelationId/ICorrelationContext.cs diff --git a/src/CorrelationId/CorrelationContext.cs b/src/CorrelationId/CorrelationContext.cs index 43d6815..5e18821 100644 --- a/src/CorrelationId/CorrelationContext.cs +++ b/src/CorrelationId/CorrelationContext.cs @@ -2,8 +2,10 @@ namespace CorrelationId { - /// - public class CorrelationContext : ICorrelationContext + /// + /// Provides access to per request correlation properties. + /// + public class CorrelationContext { internal CorrelationContext(string correlationId, string header) { @@ -17,10 +19,14 @@ internal CorrelationContext(string correlationId, string header) Header = header; } - /// + /// + /// The Correlation ID which is applicable to the current request. + /// public string CorrelationId { get; } - /// + /// + /// The name of the header from which the Correlation ID is read/written. + /// public string Header { get; } } } diff --git a/src/CorrelationId/CorrelationContextAccessor.cs b/src/CorrelationId/CorrelationContextAccessor.cs index 33ba081..3ef0241 100644 --- a/src/CorrelationId/CorrelationContextAccessor.cs +++ b/src/CorrelationId/CorrelationContextAccessor.cs @@ -5,10 +5,10 @@ namespace CorrelationId /// public class CorrelationContextAccessor : ICorrelationContextAccessor { - private static AsyncLocal _correlationContext = new AsyncLocal(); + private static AsyncLocal _correlationContext = new AsyncLocal(); /// - public ICorrelationContext CorrelationContext + public CorrelationContext CorrelationContext { get => _correlationContext.Value; set => _correlationContext.Value = value; diff --git a/src/CorrelationId/CorrelationContextFactory.cs b/src/CorrelationId/CorrelationContextFactory.cs index 2a58bfb..cda90fa 100644 --- a/src/CorrelationId/CorrelationContextFactory.cs +++ b/src/CorrelationId/CorrelationContextFactory.cs @@ -4,14 +4,25 @@ public class CorrelationContextFactory : ICorrelationContextFactory { private readonly ICorrelationContextAccessor _correlationContextAccessor; - + + /// + /// Initializes a new instance of the class. + /// + public CorrelationContextFactory() + : this(null) + { } + + /// + /// Initializes a new instance of the class. + /// + /// The through which the will be set. public CorrelationContextFactory(ICorrelationContextAccessor correlationContextAccessor) { _correlationContextAccessor = correlationContextAccessor; } /// - public ICorrelationContext Create(string correlationId, string header) + public CorrelationContext Create(string correlationId, string header) { var correlationContext = new CorrelationContext(correlationId, header); diff --git a/src/CorrelationId/CorrelationIdServiceExtensions.cs b/src/CorrelationId/CorrelationIdServiceExtensions.cs index 58be269..de98d86 100644 --- a/src/CorrelationId/CorrelationIdServiceExtensions.cs +++ b/src/CorrelationId/CorrelationIdServiceExtensions.cs @@ -3,6 +3,9 @@ namespace CorrelationId { + /// + /// Extensions on the . + /// public static class CorrelationIdServiceExtensions { /// diff --git a/src/CorrelationId/ICorrelationContext.cs b/src/CorrelationId/ICorrelationContext.cs deleted file mode 100644 index f37f128..0000000 --- a/src/CorrelationId/ICorrelationContext.cs +++ /dev/null @@ -1,18 +0,0 @@ -namespace CorrelationId -{ - /// - /// Provides access to per request correlation properties. - /// - public interface ICorrelationContext - { - /// - /// The Correlation ID which is applicable to the current request. - /// - string CorrelationId { get; } - - /// - /// The name of the header from which the Correlation ID is read/written. - /// - string Header { get; } - } -} \ No newline at end of file diff --git a/src/CorrelationId/ICorrelationContextAccessor.cs b/src/CorrelationId/ICorrelationContextAccessor.cs index 3e85af9..1a20860 100644 --- a/src/CorrelationId/ICorrelationContextAccessor.cs +++ b/src/CorrelationId/ICorrelationContextAccessor.cs @@ -1,13 +1,13 @@ namespace CorrelationId { /// - /// Provides access to the for the current request. + /// Provides access to the for the current request. /// public interface ICorrelationContextAccessor { /// - /// The for the current request. + /// The for the current request. /// - ICorrelationContext CorrelationContext { get; set; } + CorrelationContext CorrelationContext { get; set; } } } diff --git a/src/CorrelationId/ICorrelationContextFactory.cs b/src/CorrelationId/ICorrelationContextFactory.cs index 7443967..1008d49 100644 --- a/src/CorrelationId/ICorrelationContextFactory.cs +++ b/src/CorrelationId/ICorrelationContextFactory.cs @@ -1,20 +1,20 @@ namespace CorrelationId { /// - /// A factory for creating and disposing an instance of a . + /// A factory for creating and disposing an instance of a . /// public interface ICorrelationContextFactory { /// - /// Creates a new with the correlation ID set for the current request. + /// Creates a new with the correlation ID set for the current request. /// /// The correlation ID to set on the context. /// /// The header used to hold the correlation ID. - /// A new instance of a . - ICorrelationContext Create(string correlationId, string header); + /// A new instance of a . + CorrelationContext Create(string correlationId, string header); /// - /// Disposes of the for the current request. + /// Disposes of the for the current request. /// void Dispose(); } From 459144d23a3135bd153c2a7b3462a4a418886c23 Mon Sep 17 00:00:00 2001 From: Steve Gordon Date: Sun, 8 Apr 2018 14:24:03 +0100 Subject: [PATCH 10/10] Version to 2.1.0 --- src/CorrelationId/CorrelationId.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/CorrelationId/CorrelationId.csproj b/src/CorrelationId/CorrelationId.csproj index 4d91663..8bfa477 100644 --- a/src/CorrelationId/CorrelationId.csproj +++ b/src/CorrelationId/CorrelationId.csproj @@ -11,7 +11,7 @@ git aspnetcore;correlationid en - 2.1.0-rc3 + 2.1.0 bin\Release\netstandard1.4\CorrelationId.xml https://github.com/stevejgordon/CorrelationId https://github.com/stevejgordon/CorrelationId