Skip to content

Commit

Permalink
added support for Host.CreateApplicationBuilder() for .NET 8, added c…
Browse files Browse the repository at this point in the history
…odegen support for Wolverine too
  • Loading branch information
jeremydmiller committed Aug 16, 2024
1 parent fe74771 commit 58d9d52
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 5 deletions.
8 changes: 8 additions & 0 deletions src/Lamar.AspNetCoreTests/integrating_with_HostBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -43,6 +45,12 @@ public void use_lamar_with_HostBuilder()
using (var host = builder.Start())
{
var container = host.Services.ShouldBeOfType<Container>();

container.GetInstance<IServiceProviderIsService>()
.ShouldBeSameAs(container);

container.GetInstance<IServiceVariableSource>()
.ShouldBeOfType<ServiceVariableSource>();
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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<Container>();
}

[Fact]
public void should_register_IServiceProviderIsService()
{
theHost.Services.GetRequiredService<IServiceProviderIsService>()
.ShouldBeOfType<Container>();
}

[Fact]
public void should_register_IServiceVariableSource()
{
theHost.Services.GetRequiredService<IServiceVariableSource>()
.ShouldBeOfType<ServiceVariableSource>();
}
}
#endif
2 changes: 1 addition & 1 deletion src/Lamar.Diagnostics/Lamar.Diagnostics.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<Description>Adds diagnostic checks to the command line of your Lamar-enabled ASP.Net Core app</Description>
<Version>13.0.4</Version>
<Version>13.1.0</Version>
<Authors>Jeremy D. Miller</Authors>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<DebugType>portable</DebugType>
Expand Down
34 changes: 32 additions & 2 deletions src/Lamar.Microsoft.DependencyInjection/HostBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -22,8 +23,31 @@ public static IHostBuilder OverrideServices(this IHostBuilder builder, Action<Se
{
return builder.ConfigureServices(x => x.OverrideServices(overrides));
}


#if NET8_0_OR_GREATER
/// <summary>
/// Use Lamar as the DI/IoC container for this application
/// </summary>
/// <param name="builder"></param>
/// <param name="configure"></param>
/// <returns></returns>
public static HostApplicationBuilder UseLamar(this HostApplicationBuilder builder,
Action<ServiceRegistry> configure = null)
{
builder.Services.AddSingleton(c =>
c.GetRequiredService<IContainer>().CreateServiceVariableSource());

// This enables the usage of implicit services in Minimal APIs
builder.Services.AddSingleton(s => (IServiceProviderIsService) s.GetRequiredService<IContainer>());

builder.ConfigureContainer<ServiceRegistry>(new LamarServiceProviderFactory(), x =>
{
configure?.Invoke(x);
});

return builder;
}
#endif

/// <summary>
/// Shortcut to replace the built in DI container with Lamar using service registrations
Expand All @@ -46,6 +70,9 @@ public static IHostBuilder UseLamar(this IHostBuilder builder, Action<HostBuilde
services.Clear();
services.AddRange(registry);

services.AddSingleton(c =>
c.GetRequiredService<IContainer>().CreateServiceVariableSource());

#if NET6_0_OR_GREATER
// This enables the usage of implicit services in Minimal APIs
services.AddSingleton(s => (IServiceProviderIsService) s.GetRequiredService<IContainer>());
Expand All @@ -68,7 +95,7 @@ public static IHostBuilder UseLamar(this IHostBuilder builder, Action<ServiceReg





/// <summary>
/// Overrides the internal DI container with Lamar, optionally using a Lamar ServiceRegistry
Expand All @@ -86,6 +113,9 @@ public static IServiceCollection AddLamar(this IServiceCollection services, Serv
));
services.AddSingleton<IServiceProviderFactory<ServiceRegistry>, LamarServiceProviderFactory>();
services.AddSingleton<IServiceProviderFactory<IServiceCollection>, LamarServiceProviderFactory>();

services.AddSingleton<IServiceVariableSource>(c =>
c.GetRequiredService<IContainer>().CreateServiceVariableSource());

#if NET6_0_OR_GREATER
services.AddSingleton<IServiceProviderIsService>(s => (IServiceProviderIsService) s.GetRequiredService<IContainer>());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Description>Lamar Adapter for HostBuilder Integration</Description>
<Version>13.0.4</Version>
<Version>13.1.0</Version>
<Authors>Jeremy D. Miller</Authors>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<DebugType>portable</DebugType>
Expand Down
2 changes: 1 addition & 1 deletion src/Lamar/Lamar.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Description>Fast ASP.Net Core compatible IoC Tool, Successor to StructureMap</Description>
<Version>13.0.4</Version>
<Version>13.1.0</Version>
<Authors>Jeremy D. Miller</Authors>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<DebugType>portable</DebugType>
Expand Down

0 comments on commit 58d9d52

Please sign in to comment.