|
1 | | -# :warning: As [StructureMap has been sunsetted](https://jeremydmiller.com/2018/01/29/sunsetting-structuremap/), it's recommended to move to [Lamar](https://jasperfx.github.io/lamar/), StructureMap's successor, which is more compatible with ASP.NET Core's DI system. :warning: |
| 1 | +# .NET Core 3.1 Structuremap Integration |
2 | 2 |
|
3 | | -# StructureMap integration for ASP.NET Core [](https://ci.appveyor.com/project/khellang/structuremap-microsoft-dependencyinjection/branch/master) |
| 3 | +Available on Nuget here: https://www.nuget.org/packages/StructureMap.Microsoft.DependencyInjection.Forked/ |
4 | 4 |
|
| 5 | +This was forked from [this underlying library](https://github.com/structuremap/StructureMap.Microsoft.DependencyInjection) due to a bug around Disposing objects. |
5 | 6 |
|
6 | | -This repository contains the source of two NuGet packages: |
| 7 | +## Integration guide |
7 | 8 |
|
8 | | - - StructureMap.AspNetCore |
9 | | - - StructureMap.Microsoft.DependencyInjection (formerly known as StructureMap.Dnx) |
10 | | - |
11 | | -These packages provide integration with ASP.NET Core and the built-in container on different levels. |
12 | | - |
13 | | -## StructureMap.AspNetCore |
14 | | - |
15 | | -Adds integration with the ASP.NET Core hosting mechanism. |
16 | | - |
17 | | -### Installation |
18 | | - |
19 | | -Add `StructureMap.AspNetCore` to your project: |
20 | | - |
21 | | -```json |
22 | | -<ItemGroup> |
23 | | - <PackageReference Include="StructureMap.AspNetCore" Version"<version>" /> |
24 | | -</ItemGroup> |
| 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 | + }); |
25 | 18 | ``` |
26 | 19 |
|
27 | | -### Usage |
28 | | - |
29 | | -The package adds the `UseStructureMap` extension method to `IWebHostBuilder`. Calling this method will instruct the ASP.NET Core host to |
30 | | -create a StructureMap `Registry` and optionally let the user configure it using a `Startup.ConfigureContainer(Registry)` method. |
31 | | - |
32 | | -### Example |
33 | | - |
34 | | -```csharp |
35 | | -using System.IO; |
36 | | -using Microsoft.AspNetCore.Hosting; |
37 | | -using StructureMap.AspNetCore; |
| 20 | +Startup.cs: |
| 21 | +```cs |
| 22 | +public void ConfigureServices(IServiceCollection services) |
| 23 | +{ |
| 24 | + services.AddControllers(); |
| 25 | +} |
38 | 26 |
|
39 | | -public static class Program |
| 27 | +public void ConfigureContainer(Container builder) |
40 | 28 | { |
41 | | - public static void Main(string[] args) |
| 29 | + builder.Configure(config => |
42 | 30 | { |
43 | | - CreateWebHostBuilder(args).Build().Run(); |
44 | | - } |
45 | | - |
46 | | - public static IWebHostBuilder CreateWebHostBuilder(string[] args) => |
47 | | - WebHost.CreateDefaultBuilder(args) |
48 | | - .UseStructureMap() // Add support for StructureMap |
49 | | - .UseStartup<Startup>(); |
| 31 | + // Your services here |
| 32 | + config.AddRegistry(new MyRegistry()); |
| 33 | + }); |
50 | 34 | } |
51 | 35 | ``` |
52 | 36 |
|
53 | | -The runtime will then look for a `ConfigureContainer` method on the specified `Startup` class: |
54 | | - |
55 | | -```csharp |
56 | | -public class Startup |
| 37 | +The registry: |
| 38 | +```cs |
| 39 | +public class MyRegistry : Registry |
57 | 40 | { |
58 | | - public void ConfigureServices(IServiceCollection services) |
59 | | - { |
60 | | - // Configure the ASP.NET specific stuff. |
61 | | - } |
62 | | - |
63 | | - public void ConfigureContainer(Registry registry) |
64 | | - { |
65 | | - // Use StructureMap-specific APIs to register services in the registry. |
66 | | - } |
67 | | - |
68 | | - public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) |
| 41 | + public MyRegistry() |
69 | 42 | { |
| 43 | + For<Something>().Singleton().Use<Something>(); |
70 | 44 | } |
71 | 45 | } |
72 | 46 | ``` |
73 | 47 |
|
74 | | - |
75 | | -## StructureMap.Microsoft.DependencyInjection |
76 | | - |
77 | | -Adds StructureMap support for [Microsoft.Extensions.DependencyInjection](https://github.com/aspnet/DependencyInjection) |
78 | | - |
79 | | -### Installation |
80 | | - |
81 | | -Add `StructureMap.Microsoft.DependencyInjection` to your project: |
82 | | - |
83 | | -```json |
84 | | -<ItemGroup> |
85 | | - <PackageReference Include="StructureMap.Microsoft.DependencyInjection" Version"<version>" /> |
86 | | -</ItemGroup> |
87 | | -``` |
88 | | - |
89 | | -### Usage |
90 | | - |
91 | | -The package contains a single, public extension method, `Populate`. |
92 | | -It's used to populate a StructureMap container using a set of `ServiceDescriptors` or an `IServiceCollection`. |
93 | | - |
94 | | -#### Example |
95 | | - |
96 | | -```csharp |
97 | | -using System; |
98 | | -using Microsoft.Extensions.DependencyInjection; |
99 | | -using StructureMap; |
100 | | - |
101 | | -public class Startup |
| 48 | +StructureMapContainerBuilderFactory.cs |
| 49 | +```cs |
| 50 | +public class StructureMapContainerBuilderFactory : IServiceProviderFactory<Container> |
102 | 51 | { |
103 | | - public IServiceProvider ConfigureServices(IServiceCollection services) |
| 52 | + private IServiceCollection _services; |
| 53 | + |
| 54 | + public Container CreateBuilder(IServiceCollection services) |
104 | 55 | { |
105 | | - services.AddMvc(); |
106 | | - services.AddWhatever(); |
| 56 | + _services = services; |
| 57 | + return new Container(); |
| 58 | + } |
107 | 59 |
|
108 | | - var container = new Container(); |
109 | | - |
110 | | - // You can populate the container instance in one of two ways: |
111 | | - |
112 | | - // 1. Use StructureMap's `Configure` method and call |
113 | | - // `Populate` on the `ConfigurationExpression`. |
114 | | - |
115 | | - container.Configure(config => |
| 60 | + public IServiceProvider CreateServiceProvider(Container builder) |
| 61 | + { |
| 62 | + builder.Configure(config => |
116 | 63 | { |
117 | | - // Register stuff in container, using the StructureMap APIs... |
118 | | -
|
119 | | - config.Populate(services); |
| 64 | + config.Populate(_services); |
120 | 65 | }); |
121 | | - |
122 | | - // 2. Call `Populate` directly on the container instance. |
123 | | - // This will internally do a call to `Configure`. |
124 | | - |
125 | | - // Register stuff in container, using the StructureMap APIs... |
126 | | -
|
127 | | - // Here we populate the container using the service collection. |
128 | | - // This will register all services from the collection |
129 | | - // into the container with the appropriate lifetime. |
130 | | - container.Populate(services); |
131 | 66 |
|
132 | | - // Finally, make sure we return an IServiceProvider. This makes |
133 | | - // ASP.NET use the StructureMap container to resolve its services. |
134 | | - return container.GetInstance<IServiceProvider>(); |
| 67 | + return builder.GetInstance<IServiceProvider>(); |
135 | 68 | } |
136 | 69 | } |
137 | 70 | ``` |
0 commit comments