From 58d9d525d2720b63ddc0494e8522147ea47bdbd5 Mon Sep 17 00:00:00 2001 From: "Jeremy D. Miller" Date: Thu, 15 Aug 2024 19:53:31 -0500 Subject: [PATCH] added support for Host.CreateApplicationBuilder() for .NET 8, added codegen support for Wolverine too --- .../integrating_with_HostBuilder.cs | 8 +++ ...tegration_with_host_application_builder.cs | 52 +++++++++++++++++++ .../Lamar.Diagnostics.csproj | 2 +- .../HostBuilderExtensions.cs | 34 +++++++++++- ...Lamar.Microsoft.DependencyInjection.csproj | 2 +- src/Lamar/Lamar.csproj | 2 +- 6 files changed, 95 insertions(+), 5 deletions(-) create mode 100644 src/Lamar.AspNetCoreTests/integration_with_host_application_builder.cs diff --git a/src/Lamar.AspNetCoreTests/integrating_with_HostBuilder.cs b/src/Lamar.AspNetCoreTests/integrating_with_HostBuilder.cs index 00505936..6501fdb0 100644 --- a/src/Lamar.AspNetCoreTests/integrating_with_HostBuilder.cs +++ b/src/Lamar.AspNetCoreTests/integrating_with_HostBuilder.cs @@ -3,7 +3,9 @@ using System.Linq; using System.Threading; using System.Threading.Tasks; +using JasperFx.CodeGeneration.Model; using JasperFx.Core; +using Lamar.IoC.Frames; using Lamar.Microsoft.DependencyInjection; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; @@ -43,6 +45,12 @@ public void use_lamar_with_HostBuilder() using (var host = builder.Start()) { var container = host.Services.ShouldBeOfType(); + + container.GetInstance() + .ShouldBeSameAs(container); + + container.GetInstance() + .ShouldBeOfType(); } } diff --git a/src/Lamar.AspNetCoreTests/integration_with_host_application_builder.cs b/src/Lamar.AspNetCoreTests/integration_with_host_application_builder.cs new file mode 100644 index 00000000..4fc44065 --- /dev/null +++ b/src/Lamar.AspNetCoreTests/integration_with_host_application_builder.cs @@ -0,0 +1,52 @@ +using System; +using JasperFx.CodeGeneration.Model; +using Lamar.IoC.Frames; +using Lamar.Microsoft.DependencyInjection; +using Microsoft.AspNetCore.Http.Features; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using Shouldly; +using Xunit; + +namespace Lamar.AspNetCoreTests; + +#if NET8_0_OR_GREATER +public class integration_with_host_application_builder : IDisposable +{ + private readonly IHost theHost; + + public integration_with_host_application_builder() + { + theHost = Host.CreateApplicationBuilder() + .UseLamar() + .Build(); + + theHost.Start(); + } + + public void Dispose() + { + theHost?.Dispose(); + } + + [Fact] + public void should_be_using_lamar_as_the_container() + { + theHost.Services.ShouldBeOfType(); + } + + [Fact] + public void should_register_IServiceProviderIsService() + { + theHost.Services.GetRequiredService() + .ShouldBeOfType(); + } + + [Fact] + public void should_register_IServiceVariableSource() + { + theHost.Services.GetRequiredService() + .ShouldBeOfType(); + } +} +#endif \ No newline at end of file diff --git a/src/Lamar.Diagnostics/Lamar.Diagnostics.csproj b/src/Lamar.Diagnostics/Lamar.Diagnostics.csproj index 04b7d181..a01d734d 100644 --- a/src/Lamar.Diagnostics/Lamar.Diagnostics.csproj +++ b/src/Lamar.Diagnostics/Lamar.Diagnostics.csproj @@ -2,7 +2,7 @@ Adds diagnostic checks to the command line of your Lamar-enabled ASP.Net Core app - 13.0.4 + 13.1.0 Jeremy D. Miller net6.0;net7.0;net8.0 portable diff --git a/src/Lamar.Microsoft.DependencyInjection/HostBuilderExtensions.cs b/src/Lamar.Microsoft.DependencyInjection/HostBuilderExtensions.cs index 4a5da6df..73f9b04f 100644 --- a/src/Lamar.Microsoft.DependencyInjection/HostBuilderExtensions.cs +++ b/src/Lamar.Microsoft.DependencyInjection/HostBuilderExtensions.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using JasperFx.CodeGeneration.Model; using JasperFx.Core; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; @@ -22,8 +23,31 @@ public static IHostBuilder OverrideServices(this IHostBuilder builder, Action x.OverrideServices(overrides)); } - +#if NET8_0_OR_GREATER + /// + /// Use Lamar as the DI/IoC container for this application + /// + /// + /// + /// + public static HostApplicationBuilder UseLamar(this HostApplicationBuilder builder, + Action configure = null) + { + builder.Services.AddSingleton(c => + c.GetRequiredService().CreateServiceVariableSource()); + + // This enables the usage of implicit services in Minimal APIs + builder.Services.AddSingleton(s => (IServiceProviderIsService) s.GetRequiredService()); + + builder.ConfigureContainer(new LamarServiceProviderFactory(), x => + { + configure?.Invoke(x); + }); + + return builder; + } +#endif /// /// Shortcut to replace the built in DI container with Lamar using service registrations @@ -46,6 +70,9 @@ public static IHostBuilder UseLamar(this IHostBuilder builder, Action + c.GetRequiredService().CreateServiceVariableSource()); + #if NET6_0_OR_GREATER // This enables the usage of implicit services in Minimal APIs services.AddSingleton(s => (IServiceProviderIsService) s.GetRequiredService()); @@ -68,7 +95,7 @@ public static IHostBuilder UseLamar(this IHostBuilder builder, Action /// Overrides the internal DI container with Lamar, optionally using a Lamar ServiceRegistry @@ -86,6 +113,9 @@ public static IServiceCollection AddLamar(this IServiceCollection services, Serv )); services.AddSingleton, LamarServiceProviderFactory>(); services.AddSingleton, LamarServiceProviderFactory>(); + + services.AddSingleton(c => + c.GetRequiredService().CreateServiceVariableSource()); #if NET6_0_OR_GREATER services.AddSingleton(s => (IServiceProviderIsService) s.GetRequiredService()); diff --git a/src/Lamar.Microsoft.DependencyInjection/Lamar.Microsoft.DependencyInjection.csproj b/src/Lamar.Microsoft.DependencyInjection/Lamar.Microsoft.DependencyInjection.csproj index 0a282c7d..6b3bd1af 100644 --- a/src/Lamar.Microsoft.DependencyInjection/Lamar.Microsoft.DependencyInjection.csproj +++ b/src/Lamar.Microsoft.DependencyInjection/Lamar.Microsoft.DependencyInjection.csproj @@ -1,7 +1,7 @@  Lamar Adapter for HostBuilder Integration - 13.0.4 + 13.1.0 Jeremy D. Miller net6.0;net7.0;net8.0 portable diff --git a/src/Lamar/Lamar.csproj b/src/Lamar/Lamar.csproj index f464c2b7..06d72395 100644 --- a/src/Lamar/Lamar.csproj +++ b/src/Lamar/Lamar.csproj @@ -1,7 +1,7 @@  Fast ASP.Net Core compatible IoC Tool, Successor to StructureMap - 13.0.4 + 13.1.0 Jeremy D. Miller net6.0;net7.0;net8.0 portable