Skip to content
Open
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
50 changes: 37 additions & 13 deletions CorrelationId.sln
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,19 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "build", "build", "{5961B376
azure-pipelines.yml = azure-pipelines.yml
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "3.1", "3.1", "{E28C5481-A68F-44AF-983F-EA127E70621A}"
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "net6", "net6", "{E28C5481-A68F-44AF-983F-EA127E70621A}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MvcSample", "samples\3.1\MvcSample\MvcSample.csproj", "{9393676B-BE08-44C9-B556-7BE31CFA5B86}"
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "net48", "net48", "{0D3CBD88-90B9-4364-9DE4-CA1FCC9CB023}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MvcSampleTests", "samples\3.1\MvcSampleTests\MvcSampleTests.csproj", "{0E2E2678-4DBB-4560-AF0E-86ECAF8D9B6F}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Net48WebApiSample", "samples\net48\Net48WebApiSample\Net48WebApiSample.csproj", "{DED5BFD9-29F6-4291-B863-1995C33D686B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CorrelationId.Net48.Tests", "test\CorrelationId.Net48.Tests\CorrelationId.Net48.Tests.csproj", "{A4137EAE-590B-476C-838F-A2D4A45AAE4A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Net48CorrelationId", "samples\net48\Net48CorrelationId\Net48CorrelationId.csproj", "{29F51512-04FE-40CA-826F-0DDAD9BF5750}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MvcSample", "samples\net6\MvcSample\MvcSample.csproj", "{52ED4758-2A42-415B-9423-51D84BDE1708}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MvcSampleTests", "samples\net6\MvcSampleTests\MvcSampleTests.csproj", "{CF0F76FD-9AEF-4616-A93E-23B62692FADA}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -46,14 +54,26 @@ Global
{41035337-9829-4A6A-8EA9-42CC94734CE6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{41035337-9829-4A6A-8EA9-42CC94734CE6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{41035337-9829-4A6A-8EA9-42CC94734CE6}.Release|Any CPU.Build.0 = Release|Any CPU
{9393676B-BE08-44C9-B556-7BE31CFA5B86}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9393676B-BE08-44C9-B556-7BE31CFA5B86}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9393676B-BE08-44C9-B556-7BE31CFA5B86}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9393676B-BE08-44C9-B556-7BE31CFA5B86}.Release|Any CPU.Build.0 = Release|Any CPU
{0E2E2678-4DBB-4560-AF0E-86ECAF8D9B6F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0E2E2678-4DBB-4560-AF0E-86ECAF8D9B6F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0E2E2678-4DBB-4560-AF0E-86ECAF8D9B6F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0E2E2678-4DBB-4560-AF0E-86ECAF8D9B6F}.Release|Any CPU.Build.0 = Release|Any CPU
{DED5BFD9-29F6-4291-B863-1995C33D686B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DED5BFD9-29F6-4291-B863-1995C33D686B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DED5BFD9-29F6-4291-B863-1995C33D686B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DED5BFD9-29F6-4291-B863-1995C33D686B}.Release|Any CPU.Build.0 = Release|Any CPU
{A4137EAE-590B-476C-838F-A2D4A45AAE4A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A4137EAE-590B-476C-838F-A2D4A45AAE4A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A4137EAE-590B-476C-838F-A2D4A45AAE4A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A4137EAE-590B-476C-838F-A2D4A45AAE4A}.Release|Any CPU.Build.0 = Release|Any CPU
{29F51512-04FE-40CA-826F-0DDAD9BF5750}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{29F51512-04FE-40CA-826F-0DDAD9BF5750}.Debug|Any CPU.Build.0 = Debug|Any CPU
{29F51512-04FE-40CA-826F-0DDAD9BF5750}.Release|Any CPU.ActiveCfg = Release|Any CPU
{29F51512-04FE-40CA-826F-0DDAD9BF5750}.Release|Any CPU.Build.0 = Release|Any CPU
{52ED4758-2A42-415B-9423-51D84BDE1708}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{52ED4758-2A42-415B-9423-51D84BDE1708}.Debug|Any CPU.Build.0 = Debug|Any CPU
{52ED4758-2A42-415B-9423-51D84BDE1708}.Release|Any CPU.ActiveCfg = Release|Any CPU
{52ED4758-2A42-415B-9423-51D84BDE1708}.Release|Any CPU.Build.0 = Release|Any CPU
{CF0F76FD-9AEF-4616-A93E-23B62692FADA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CF0F76FD-9AEF-4616-A93E-23B62692FADA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CF0F76FD-9AEF-4616-A93E-23B62692FADA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CF0F76FD-9AEF-4616-A93E-23B62692FADA}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -62,8 +82,12 @@ Global
{865886FC-EBBE-49E1-8F49-FECD0408EF6E} = {9A14C73A-D8FC-40C3-81FC-D0687F43FEAF}
{41035337-9829-4A6A-8EA9-42CC94734CE6} = {EAF27B74-0B27-4BEE-9F82-DD5812B21B17}
{E28C5481-A68F-44AF-983F-EA127E70621A} = {34C0F65A-8BF2-40DA-B0E7-844930EE2A7B}
{9393676B-BE08-44C9-B556-7BE31CFA5B86} = {E28C5481-A68F-44AF-983F-EA127E70621A}
{0E2E2678-4DBB-4560-AF0E-86ECAF8D9B6F} = {E28C5481-A68F-44AF-983F-EA127E70621A}
{0D3CBD88-90B9-4364-9DE4-CA1FCC9CB023} = {34C0F65A-8BF2-40DA-B0E7-844930EE2A7B}
{DED5BFD9-29F6-4291-B863-1995C33D686B} = {0D3CBD88-90B9-4364-9DE4-CA1FCC9CB023}
{A4137EAE-590B-476C-838F-A2D4A45AAE4A} = {EAF27B74-0B27-4BEE-9F82-DD5812B21B17}
{29F51512-04FE-40CA-826F-0DDAD9BF5750} = {0D3CBD88-90B9-4364-9DE4-CA1FCC9CB023}
{52ED4758-2A42-415B-9423-51D84BDE1708} = {E28C5481-A68F-44AF-983F-EA127E70621A}
{CF0F76FD-9AEF-4616-A93E-23B62692FADA} = {E28C5481-A68F-44AF-983F-EA127E70621A}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {C0A37404-C50B-472E-9491-DDE2A4BDA882}
Expand Down
45 changes: 0 additions & 45 deletions samples/3.1/MvcSample/Controllers/WeatherForecastController.cs

This file was deleted.

22 changes: 0 additions & 22 deletions samples/3.1/MvcSampleTests/MvcSampleTests.csproj

This file was deleted.

118 changes: 118 additions & 0 deletions samples/net48/Net48CorrelationId/CorrelationIdMiddlewareWrapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
using System;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using CorrelationId;
using CorrelationId.Abstractions;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using AspnetHttpContext = Microsoft.AspNetCore.Http.HttpContext;
using AspnetDefaultHttpContext = Microsoft.AspNetCore.Http.DefaultHttpContext;

namespace Net48CorrelationId
{
/// <summary>
/// We don't have any middleware in .net framework so we wrap it in a HttpMessageHandler instead
/// </summary>
public class CorrelationIdMiddlewareWrapper : DelegatingHandler
{
private readonly ICorrelationContextFactory _correlationContextFactory;
private readonly IOptions<CorrelationIdOptions> _correlationIdOptions;
private readonly ILogger<CorrelationIdMiddlewareWrapper> _correlationIdMiddlewareLoggerWrapper;
private readonly ILogger<CorrelationIdMiddleware> _correlationIdMiddlewareLogger;
private readonly ICorrelationIdProvider _correlationIdProvider;
private readonly ICorrelationContextAccessor _correlationContextAccessor;

public CorrelationIdMiddlewareWrapper(ICorrelationContextFactory correlationContextFactory,
IOptions<CorrelationIdOptions> correlationIdOptions,
ILogger<CorrelationIdMiddlewareWrapper> correlationIdMiddlewareLoggerWrapper,
// ReSharper disable once ContextualLoggerProblem
ILogger<CorrelationIdMiddleware> correlationIdMiddlewareLogger,
ICorrelationIdProvider correlationIdProvider, ICorrelationContextAccessor correlationContextAccessor)
{
_correlationContextFactory = correlationContextFactory;
_correlationIdOptions = correlationIdOptions;
_correlationIdMiddlewareLoggerWrapper = correlationIdMiddlewareLoggerWrapper;
_correlationIdMiddlewareLogger = correlationIdMiddlewareLogger;
_correlationIdProvider = correlationIdProvider;
_correlationContextAccessor = correlationContextAccessor;
}

protected override async Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request,
CancellationToken cancellationToken)
{
HttpResponseMessage response = null;
var correlationIdMiddleware = new CorrelationIdMiddleware(
context => MiddlewareActionAsync(context, request, x => response = x), _correlationIdMiddlewareLogger,
_correlationIdOptions, _correlationIdProvider);
var aspnetHttpContext = GetAspnetHttpContext();

if (_correlationContextAccessor.CorrelationContext == null)
{
// we should just do this to setup the context if missing
await correlationIdMiddleware.Invoke(aspnetHttpContext, _correlationContextFactory);
}
else
{
await MiddlewareActionAsync(aspnetHttpContext, request, x => response = x);
}

return response;
}

private async Task MiddlewareActionAsync(AspnetHttpContext aspnetHttpContext,
HttpRequestMessage request, Action<HttpResponseMessage> responseSetter)
{
var correlationId = _correlationContextAccessor?.CorrelationContext?.CorrelationId;
if (!string.IsNullOrEmpty(correlationId)
&& !request.Headers.Contains(_correlationContextAccessor.CorrelationContext.Header))
{
request.Headers.Add(_correlationContextAccessor.CorrelationContext.Header,
_correlationContextAccessor.CorrelationContext.CorrelationId);
}

var response = await base.SendAsync(request, aspnetHttpContext.RequestAborted);

// context.Response.OnStarting is not executed on .net FW
if (_correlationIdOptions.Value.IncludeInResponse &&
!string.IsNullOrEmpty(correlationId) &&
!response.Headers.Contains(_correlationIdOptions.Value.ResponseHeader))
{
_correlationIdMiddlewareLoggerWrapper.LogDebug(
"Writing correlation ID response header {ResponseHeader} with value {CorrelationId}",
_correlationIdOptions.Value.ResponseHeader, correlationId);
response.Headers.Add(_correlationIdOptions.Value.ResponseHeader, correlationId);
}

responseSetter.Invoke(response);
}

private AspnetHttpContext GetAspnetHttpContext()
{
var httpContext = new AspnetDefaultHttpContext();

if (System.Web.HttpContext.Current == null)
{
return httpContext;
}

var correlationIds =
System.Web.HttpContext.Current.Request.Headers
.GetValues(_correlationIdOptions.Value.RequestHeader);

if (correlationIds == null)
{
return httpContext;
}

foreach (var correlationId in correlationIds)
{
httpContext.Request.Headers
.Add(_correlationIdOptions.Value.RequestHeader, correlationId);
}

return httpContext;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Microsoft.Extensions.DependencyInjection;

namespace Net48CorrelationId
{
public static class CorrelationIdServiceCollectionExtensions
{
public static void UseCorrelationIdMiddleware(this IServiceCollection serviceCollection)
{
serviceCollection.AddSingleton<CorrelationIdMiddlewareWrapper>();
}
}
}
69 changes: 69 additions & 0 deletions samples/net48/Net48CorrelationId/Net48CorrelationId.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{29F51512-04FE-40CA-826F-0DDAD9BF5750}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Net48CorrelationId</RootNamespace>
<AssemblyName>Net48CorrelationId</AssemblyName>
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="mscorlib" />
<Reference Include="System" />
<Reference Include="System.ComponentModel.DataAnnotations" />
<Reference Include="System.Core" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Numerics" />
<Reference Include="System.Web" />
<Reference Include="System.Web.Services" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="CorrelationIdMiddlewareWrapper.cs" />
<Compile Include="CorrelationIdServiceCollectionExtensions.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\src\CorrelationId\CorrelationId.csproj">
<Project>{865886fc-ebbe-49e1-8f49-fecd0408ef6e}</Project>
<Name>CorrelationId</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Http" Version="7.0.0" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->

</Project>
35 changes: 35 additions & 0 deletions samples/net48/Net48CorrelationId/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System.Reflection;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Net48CorrelationId")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Net48CorrelationId")]
[assembly: AssemblyCopyright("Copyright © 2023")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("29F51512-04FE-40CA-826F-0DDAD9BF5750")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
Loading