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

v1: Migrate AspNetCore project #892

Draft
wants to merge 3 commits into
base: develop-v1
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions EventFlow.sln
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EventFlow.MsSql", "Source\E
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EventFlow.MsSql.Tests", "Source\EventFlow.MsSql.Tests\EventFlow.MsSql.Tests.csproj", "{CE19355C-6355-405F-A640-908AE4F83C2C}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EventFlow.AspNetCore", "Source\EventFlow.AspNetCore\EventFlow.AspNetCore.csproj", "{4E39F832-B280-465B-823B-FB5470F511CF}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "AspNetCore", "AspNetCore", "{C3E24CD7-CD44-433E-9902-D5C97A5FEA3F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -71,6 +75,10 @@ Global
{CE19355C-6355-405F-A640-908AE4F83C2C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CE19355C-6355-405F-A640-908AE4F83C2C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CE19355C-6355-405F-A640-908AE4F83C2C}.Release|Any CPU.Build.0 = Release|Any CPU
{4E39F832-B280-465B-823B-FB5470F511CF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4E39F832-B280-465B-823B-FB5470F511CF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4E39F832-B280-465B-823B-FB5470F511CF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4E39F832-B280-465B-823B-FB5470F511CF}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -84,6 +92,7 @@ Global
{983EFD08-2256-4361-A869-23EB872328CB} = {88359036-4F35-487C-BF2C-4F31C7BC92D8}
{96A39EA3-6772-409E-AF0B-C5A35A1A13CC} = {88359036-4F35-487C-BF2C-4F31C7BC92D8}
{CE19355C-6355-405F-A640-908AE4F83C2C} = {88359036-4F35-487C-BF2C-4F31C7BC92D8}
{4E39F832-B280-465B-823B-FB5470F511CF} = {C3E24CD7-CD44-433E-9902-D5C97A5FEA3F}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {17607E2C-4E8E-45A2-85BD-0A5808E1C0F3}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ public void Configure(MvcOptions options)
}
}
}
}
}
16 changes: 11 additions & 5 deletions Source/EventFlow.AspNetCore/EventFlow.AspNetCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,27 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="3.1.5" />
<PackageReference Include="Microsoft.Extensions.Options" Version="3.1.5" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Formatters.Json" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="2.1.1" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp3.0'">
<FrameworkReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="3.0.3" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.0.3" />
<PackageReference Include="Microsoft.Extensions.Options" Version="3.0.3" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp3.1'">
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.1.6" />
<FrameworkReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="3.1.10" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.1.10" />
<PackageReference Include="Microsoft.Extensions.Options" Version="3.1.10" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,68 +23,53 @@

using System;
using EventFlow.AspNetCore.Configuration;
using EventFlow.AspNetCore.Logging;
using EventFlow.AspNetCore.MetadataProviders;
using EventFlow.AspNetCore.ServiceProvider;
using EventFlow.EventStores;
using EventFlow.Extensions;
using EventFlow.Logs;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;

namespace EventFlow.AspNetCore.Extensions
{
public class AspNetCoreEventFlowOptions
public class EventFlowOptionsAspNetCoreExtensions
{
private readonly IEventFlowOptions _options;

public AspNetCoreEventFlowOptions(IEventFlowOptions options)
public EventFlowOptionsAspNetCoreExtensions(IEventFlowOptions options)
{
_options = options;
}

public AspNetCoreEventFlowOptions AddUriMetadata()
public EventFlowOptionsAspNetCoreExtensions AddUriMetadata()
{
return RegisterMetadataProvider<AddUriMetadataProvider>();
}

public AspNetCoreEventFlowOptions AddRequestHeadersMetadata()
public EventFlowOptionsAspNetCoreExtensions AddRequestHeadersMetadata()
{
return RegisterMetadataProvider<AddRequestHeadersMetadataProvider>();
}

public AspNetCoreEventFlowOptions AddUserHostAddressMetadata()
public EventFlowOptionsAspNetCoreExtensions AddUserHostAddressMetadata()
{
return RegisterMetadataProvider<AddUserHostAddressMetadataProvider>();
}

public AspNetCoreEventFlowOptions AddUserClaimsMetadata(params string[] includedClaimTypes)
public EventFlowOptionsAspNetCoreExtensions AddUserClaimsMetadata(params string[] includedClaimTypes)
{
var options = new DefaultUserClaimsMetadataOptions(includedClaimTypes);
_options.RegisterServices(s => s.Register<IUserClaimsMetadataOptions>(_ => options));
_options.RegisterServices(s => s.AddTransient<IUserClaimsMetadataOptions>(_ => options));
return RegisterMetadataProvider<AddUserClaimsMetadataProvider>();
}

public AspNetCoreEventFlowOptions UseLogging()
public EventFlowOptionsAspNetCoreExtensions UseDefaults()
{
_options.RegisterServices(s => s.Register<ILog, AspNetCoreLoggerLog>());
return this;
}

public AspNetCoreEventFlowOptions UseDefaults()
{
return RunBootstrapperOnHostStartup().AddDefaultMetadataProviders();
}

public AspNetCoreEventFlowOptions RunBootstrapperOnHostStartup()
{
_options.RegisterServices(s => s.Register<IHostedService, HostedBootstrapper>(Lifetime.Singleton));
return this;
return AddDefaultMetadataProviders();
}

public AspNetCoreEventFlowOptions AddDefaultMetadataProviders()
public EventFlowOptionsAspNetCoreExtensions AddDefaultMetadataProviders()
{
AddRequestHeadersMetadata();
AddUriMetadata();
Expand All @@ -93,37 +78,36 @@ public AspNetCoreEventFlowOptions AddDefaultMetadataProviders()
}

#if NETSTANDARD2_0
public AspNetCoreEventFlowOptions UseMvcJsonOptions()
public EventFlowOptionsAspNetCoreExtensions UseMvcJsonOptions()
{
_options.RegisterServices(s =>
s.Register<IConfigureOptions<MvcJsonOptions>, EventFlowJsonOptionsMvcConfiguration>());
s.AddTransient<IConfigureOptions<MvcJsonOptions>, EventFlowJsonOptionsMvcConfiguration>());
return this;
}
#endif
#if (NETCOREAPP3_0 || NETCOREAPP3_1)
public AspNetCoreEventFlowOptions UseMvcJsonOptions()
public EventFlowOptionsAspNetCoreExtensions UseMvcJsonOptions()
{
_options.RegisterServices(s =>
s.Register<IConfigureOptions<MvcNewtonsoftJsonOptions>, EventFlowJsonOptionsMvcConfiguration>());
s.AddTransient<IConfigureOptions<MvcNewtonsoftJsonOptions>, EventFlowJsonOptionsMvcConfiguration>());
return this;
}
#endif

public AspNetCoreEventFlowOptions UseModelBinding(
public EventFlowOptionsAspNetCoreExtensions UseModelBinding(
Action<EventFlowModelBindingMvcConfiguration> configureModelBinding = null)
{
var modelBindingOptions = new EventFlowModelBindingMvcConfiguration();
configureModelBinding?.Invoke(modelBindingOptions);
_options.RegisterServices(s => s.Register<IConfigureOptions<MvcOptions>>(c => modelBindingOptions));
_options.RegisterServices(s => s.AddTransient<IConfigureOptions<MvcOptions>>(c => modelBindingOptions));
return this;
}

private AspNetCoreEventFlowOptions RegisterMetadataProvider<T>() where T : class, IMetadataProvider
private EventFlowOptionsAspNetCoreExtensions RegisterMetadataProvider<T>() where T : class, IMetadataProvider
{
_options
.AddMetadataProvider<T>()
.RegisterServices(s =>
s.Register<IHttpContextAccessor, HttpContextAccessor>(Lifetime.Singleton, true));
.RegisterServices(s => s.AddSingleton<IHttpContextAccessor, HttpContextAccessor>());

return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,17 @@ public static class EventFlowOptionsExtensions
{
public static IEventFlowOptions AddAspNetCore(
this IEventFlowOptions options,
Action<AspNetCoreEventFlowOptions> configuratioAction = null)
Action<EventFlowOptionsAspNetCoreExtensions> configurationAction = null)
{
var aspNetCoreOptions = new AspNetCoreEventFlowOptions(options);
if (configuratioAction == null) configuratioAction = o => o.UseDefaults();
var aspNetCoreOptions = new EventFlowOptionsAspNetCoreExtensions(options);
if (configurationAction == null)
{
configurationAction = o => o.UseDefaults();
}

configuratioAction(aspNetCoreOptions);
configurationAction(aspNetCoreOptions);

return options;
}

[Obsolete("Use AddAspNetCore(o => o...)")]
public static IEventFlowOptions AddAspNetCoreMetadataProviders(
this IEventFlowOptions eventFlowOptions)
{
return eventFlowOptions.AddAspNetCore();
}
}
}
56 changes: 0 additions & 56 deletions Source/EventFlow.AspNetCore/Logging/AspNetCoreLoggerLog.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ namespace EventFlow.AspNetCore.MetadataProviders
{
public class AddRequestHeadersMetadataProvider : IMetadataProvider
{
private static readonly ISet<string> RequestHeadersToSkip = new HashSet<string>
{
"Authorization",
"Cookie"
};
private static readonly ISet<string> RequestHeadersToSkip = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
{
"Authorization",
"Cookie"
};

private readonly IHttpContextAccessor _httpContextAccessor;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,13 @@ public class AddUriMetadataProvider : IMetadataProvider
where TIdentity : IIdentity
{
var httpContext = _httpContextAccessor.HttpContext;
if (httpContext == null)
yield break;
if (httpContext == null)
{
yield break;
}

var request = httpContext.Request;

yield return new KeyValuePair<string, string>("request_uri", request.Path.ToString());
yield return new KeyValuePair<string, string>("request_proto", request.Protocol.ToUpperInvariant());
yield return new KeyValuePair<string, string>("request_method", request.Method.ToUpperInvariant());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ public class AddUserClaimsMetadataProvider : IMetadataProvider
if (user == null)
return Enumerable.Empty<KeyValuePair<string, string>>();

return from claim in user.Claims
return
from claim in user.Claims
where _options.IsIncluded(claim.Type)
group claim by claim.Type
into claimGroup
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
using EventFlow.Core;
using EventFlow.EventStores;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Primitives;

namespace EventFlow.AspNetCore.MetadataProviders
{
Expand Down Expand Up @@ -58,7 +57,9 @@ public class AddUserHostAddressMetadataProvider : IMetadataProvider
{
var httpContext = _httpContextAccessor.HttpContext;
if (httpContext == null)
{
yield break;
}

yield return new KeyValuePair<string, string>("remote_ip_address", httpContext.Connection.RemoteIpAddress?.ToString());

Expand Down