Skip to content

Commit 58d9d52

Browse files
committed
added support for Host.CreateApplicationBuilder() for .NET 8, added codegen support for Wolverine too
1 parent fe74771 commit 58d9d52

File tree

6 files changed

+95
-5
lines changed

6 files changed

+95
-5
lines changed

src/Lamar.AspNetCoreTests/integrating_with_HostBuilder.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
using System.Linq;
44
using System.Threading;
55
using System.Threading.Tasks;
6+
using JasperFx.CodeGeneration.Model;
67
using JasperFx.Core;
8+
using Lamar.IoC.Frames;
79
using Lamar.Microsoft.DependencyInjection;
810
using Microsoft.Extensions.DependencyInjection;
911
using Microsoft.Extensions.Hosting;
@@ -43,6 +45,12 @@ public void use_lamar_with_HostBuilder()
4345
using (var host = builder.Start())
4446
{
4547
var container = host.Services.ShouldBeOfType<Container>();
48+
49+
container.GetInstance<IServiceProviderIsService>()
50+
.ShouldBeSameAs(container);
51+
52+
container.GetInstance<IServiceVariableSource>()
53+
.ShouldBeOfType<ServiceVariableSource>();
4654
}
4755
}
4856

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using System;
2+
using JasperFx.CodeGeneration.Model;
3+
using Lamar.IoC.Frames;
4+
using Lamar.Microsoft.DependencyInjection;
5+
using Microsoft.AspNetCore.Http.Features;
6+
using Microsoft.Extensions.DependencyInjection;
7+
using Microsoft.Extensions.Hosting;
8+
using Shouldly;
9+
using Xunit;
10+
11+
namespace Lamar.AspNetCoreTests;
12+
13+
#if NET8_0_OR_GREATER
14+
public class integration_with_host_application_builder : IDisposable
15+
{
16+
private readonly IHost theHost;
17+
18+
public integration_with_host_application_builder()
19+
{
20+
theHost = Host.CreateApplicationBuilder()
21+
.UseLamar()
22+
.Build();
23+
24+
theHost.Start();
25+
}
26+
27+
public void Dispose()
28+
{
29+
theHost?.Dispose();
30+
}
31+
32+
[Fact]
33+
public void should_be_using_lamar_as_the_container()
34+
{
35+
theHost.Services.ShouldBeOfType<Container>();
36+
}
37+
38+
[Fact]
39+
public void should_register_IServiceProviderIsService()
40+
{
41+
theHost.Services.GetRequiredService<IServiceProviderIsService>()
42+
.ShouldBeOfType<Container>();
43+
}
44+
45+
[Fact]
46+
public void should_register_IServiceVariableSource()
47+
{
48+
theHost.Services.GetRequiredService<IServiceVariableSource>()
49+
.ShouldBeOfType<ServiceVariableSource>();
50+
}
51+
}
52+
#endif

src/Lamar.Diagnostics/Lamar.Diagnostics.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<Description>Adds diagnostic checks to the command line of your Lamar-enabled ASP.Net Core app</Description>
5-
<Version>13.0.4</Version>
5+
<Version>13.1.0</Version>
66
<Authors>Jeremy D. Miller</Authors>
77
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
88
<DebugType>portable</DebugType>

src/Lamar.Microsoft.DependencyInjection/HostBuilderExtensions.cs

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using JasperFx.CodeGeneration.Model;
34
using JasperFx.Core;
45
using Microsoft.Extensions.DependencyInjection;
56
using Microsoft.Extensions.Hosting;
@@ -22,8 +23,31 @@ public static IHostBuilder OverrideServices(this IHostBuilder builder, Action<Se
2223
{
2324
return builder.ConfigureServices(x => x.OverrideServices(overrides));
2425
}
25-
2626

27+
#if NET8_0_OR_GREATER
28+
/// <summary>
29+
/// Use Lamar as the DI/IoC container for this application
30+
/// </summary>
31+
/// <param name="builder"></param>
32+
/// <param name="configure"></param>
33+
/// <returns></returns>
34+
public static HostApplicationBuilder UseLamar(this HostApplicationBuilder builder,
35+
Action<ServiceRegistry> configure = null)
36+
{
37+
builder.Services.AddSingleton(c =>
38+
c.GetRequiredService<IContainer>().CreateServiceVariableSource());
39+
40+
// This enables the usage of implicit services in Minimal APIs
41+
builder.Services.AddSingleton(s => (IServiceProviderIsService) s.GetRequiredService<IContainer>());
42+
43+
builder.ConfigureContainer<ServiceRegistry>(new LamarServiceProviderFactory(), x =>
44+
{
45+
configure?.Invoke(x);
46+
});
47+
48+
return builder;
49+
}
50+
#endif
2751

2852
/// <summary>
2953
/// 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<HostBuilde
4670
services.Clear();
4771
services.AddRange(registry);
4872

73+
services.AddSingleton(c =>
74+
c.GetRequiredService<IContainer>().CreateServiceVariableSource());
75+
4976
#if NET6_0_OR_GREATER
5077
// This enables the usage of implicit services in Minimal APIs
5178
services.AddSingleton(s => (IServiceProviderIsService) s.GetRequiredService<IContainer>());
@@ -68,7 +95,7 @@ public static IHostBuilder UseLamar(this IHostBuilder builder, Action<ServiceReg
6895

6996

7097

71-
98+
7299

73100
/// <summary>
74101
/// Overrides the internal DI container with Lamar, optionally using a Lamar ServiceRegistry
@@ -86,6 +113,9 @@ public static IServiceCollection AddLamar(this IServiceCollection services, Serv
86113
));
87114
services.AddSingleton<IServiceProviderFactory<ServiceRegistry>, LamarServiceProviderFactory>();
88115
services.AddSingleton<IServiceProviderFactory<IServiceCollection>, LamarServiceProviderFactory>();
116+
117+
services.AddSingleton<IServiceVariableSource>(c =>
118+
c.GetRequiredService<IContainer>().CreateServiceVariableSource());
89119

90120
#if NET6_0_OR_GREATER
91121
services.AddSingleton<IServiceProviderIsService>(s => (IServiceProviderIsService) s.GetRequiredService<IContainer>());

src/Lamar.Microsoft.DependencyInjection/Lamar.Microsoft.DependencyInjection.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<Description>Lamar Adapter for HostBuilder Integration</Description>
4-
<Version>13.0.4</Version>
4+
<Version>13.1.0</Version>
55
<Authors>Jeremy D. Miller</Authors>
66
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
77
<DebugType>portable</DebugType>

src/Lamar/Lamar.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<Description>Fast ASP.Net Core compatible IoC Tool, Successor to StructureMap</Description>
4-
<Version>13.0.4</Version>
4+
<Version>13.1.0</Version>
55
<Authors>Jeremy D. Miller</Authors>
66
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
77
<DebugType>portable</DebugType>

0 commit comments

Comments
 (0)