Skip to content

Commit a942d59

Browse files
author
Dustin Masters
committed
.net 8 support
1 parent 2bc155f commit a942d59

9 files changed

Lines changed: 108 additions & 166 deletions

File tree

README.md

Lines changed: 2 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,7 @@
1-
# .NET Core 3.1 Structuremap Integration
1+
# .NET 8 Structuremap Integration
22

33
Available on Nuget here: https://www.nuget.org/packages/StructureMap.Microsoft.DependencyInjection.Forked/
44

55
This was forked from [this underlying library](https://github.com/structuremap/StructureMap.Microsoft.DependencyInjection) due to [this bug](https://github.com/dustinsoftware/StructureMap.Microsoft.DependencyInjection/commit/0f1d8e445bfc430e1cdc7792045f5bb3356b68af) around Disposing objects.
66

7-
## Integration guide
8-
9-
Program.cs:
10-
```cs
11-
public static IHostBuilder CreateHostBuilder(string[] args) =>
12-
Host.CreateDefaultBuilder(args)
13-
.UseServiceProviderFactory(new StructureMapContainerBuilderFactory())
14-
.ConfigureWebHostDefaults(webBuilder =>
15-
{
16-
webBuilder.UseStartup<Startup>();
17-
});
18-
```
19-
20-
Startup.cs:
21-
```cs
22-
public void ConfigureServices(IServiceCollection services)
23-
{
24-
services.AddControllers();
25-
}
26-
27-
public void ConfigureContainer(Container builder)
28-
{
29-
builder.Configure(config =>
30-
{
31-
// Your services here
32-
config.AddRegistry(new MyRegistry());
33-
});
34-
}
35-
```
36-
37-
The registry:
38-
```cs
39-
public class MyRegistry : Registry
40-
{
41-
public MyRegistry()
42-
{
43-
For<Something>().Singleton().Use<Something>();
44-
}
45-
}
46-
```
47-
48-
StructureMapContainerBuilderFactory.cs
49-
```cs
50-
public class StructureMapContainerBuilderFactory : IServiceProviderFactory<Container>
51-
{
52-
private IServiceCollection _services;
53-
54-
public Container CreateBuilder(IServiceCollection services)
55-
{
56-
_services = services;
57-
return new Container();
58-
}
59-
60-
public IServiceProvider CreateServiceProvider(Container builder)
61-
{
62-
builder.Configure(config =>
63-
{
64-
config.Populate(_services);
65-
});
66-
67-
return builder.GetInstance<IServiceProvider>();
68-
}
69-
}
70-
```
7+
See the [sample app](./sample) for an example of how to integrate this in a .NET 8 app.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System.Collections.Generic;
2+
using Microsoft.AspNetCore.Mvc;
3+
4+
namespace StructureMap.AspNetCore.Sample.Controllers
5+
{
6+
[Route("")]
7+
[ApiController]
8+
public class SampleController : ControllerBase
9+
{
10+
private readonly ISampleService _sampleService;
11+
12+
public SampleController(ISampleService sampleService)
13+
{
14+
_sampleService = sampleService;
15+
}
16+
17+
[HttpGet("")]
18+
public ActionResult<IEnumerable<string>> Get()
19+
{
20+
return new string[] { _sampleService.GetMessage() };
21+
}
22+
}
23+
}

sample/StructureMap.AspNetCore.Sample/Controllers/ValuesController.cs

Lines changed: 0 additions & 45 deletions
This file was deleted.
Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,39 @@
1-
using Microsoft.AspNetCore;
1+
using Microsoft.AspNetCore.Builder;
22
using Microsoft.AspNetCore.Hosting;
3+
using Microsoft.Extensions.DependencyInjection;
4+
using Microsoft.Extensions.Hosting;
5+
using StructureMap;
6+
using StructureMap.AspNetCore.Sample;
37

4-
namespace StructureMap.AspNetCore.Sample
8+
var builder = WebApplication.CreateBuilder(args);
9+
builder.Services.AddControllers();
10+
builder.Services.AddMvc(options =>
511
{
6-
public static class Program
12+
options.EnableEndpointRouting = false;
13+
});
14+
15+
builder.Host
16+
.UseServiceProviderFactory(new StructureMapContainerBuilderFactory())
17+
.ConfigureContainer<Container>(container =>
718
{
8-
public static void Main(string[] args)
19+
container.Configure(config =>
920
{
10-
CreateWebHostBuilder(args).Build().Run();
11-
}
21+
config.AddRegistry(new SampleRegistry());
22+
});
23+
});
24+
25+
var app = builder.Build();
26+
app.UseRouting();
1227

13-
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
14-
WebHost.CreateDefaultBuilder(args)
15-
.UseStructureMap() // Add support for StructureMap
16-
.UseStartup<Startup>();
17-
}
28+
#pragma warning disable ASP0014
29+
app.UseEndpoints(endpoints =>
30+
{
31+
endpoints.MapControllers();
32+
});
33+
#pragma warning restore ASP0014
34+
35+
if (app.Environment.IsDevelopment())
36+
{
37+
app.UseDeveloperExceptionPage();
1838
}
39+
app.Run();
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using StructureMap;
2+
3+
public class SampleRegistry : Registry
4+
{
5+
public SampleRegistry()
6+
{
7+
// Add your StructureMap configuration here
8+
For<ISampleService>().Singleton().Use<SampleService>();
9+
}
10+
}
11+
12+
public class SampleService : ISampleService
13+
{
14+
public string GetMessage()
15+
{
16+
return "Hello from SampleService!";
17+
}
18+
}
19+
20+
public interface ISampleService
21+
{
22+
string GetMessage();
23+
}

sample/StructureMap.AspNetCore.Sample/Startup.cs

Lines changed: 0 additions & 40 deletions
This file was deleted.

sample/StructureMap.AspNetCore.Sample/StructureMap.AspNetCore.Sample.csproj

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
<Project Sdk="Microsoft.NET.Sdk.Web">
22
<PropertyGroup>
3-
<TargetFramework>netcoreapp2.2</TargetFramework>
4-
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
3+
<TargetFramework>net8.0</TargetFramework>
54
</PropertyGroup>
65

76
<ItemGroup>
8-
<PackageReference Include="Microsoft.AspNetCore.App" />
9-
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
107
<PackageReference Include="StructureMap" Version="4.5.0" />
118
</ItemGroup>
129

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System;
2+
using Microsoft.Extensions.DependencyInjection;
3+
using StructureMap;
4+
5+
namespace StructureMap.AspNetCore.Sample;
6+
7+
public class StructureMapContainerBuilderFactory : IServiceProviderFactory<Container>
8+
{
9+
private IServiceCollection _services;
10+
11+
public Container CreateBuilder(IServiceCollection services)
12+
{
13+
_services = services;
14+
return new Container();
15+
}
16+
17+
public IServiceProvider CreateServiceProvider(Container builder)
18+
{
19+
builder.Configure(config =>
20+
{
21+
config.Populate(_services);
22+
});
23+
24+
return builder.GetInstance<IServiceProvider>();
25+
}
26+
}

test/StructureMap.Microsoft.DependencyInjection.Tests/StructureMap.Microsoft.DependencyInjection.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
3-
<TargetFrameworks>netcoreapp3.1</TargetFrameworks>
3+
<TargetFrameworks>net8.0</TargetFrameworks>
44
</PropertyGroup>
55

66
<ItemGroup>

0 commit comments

Comments
 (0)