-
-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added example of console app integrating DryIoc.MS.DI with HostApplic…
…ationBuilder + updated MinWeb to .NET8, see #664
- Loading branch information
Showing
7 changed files
with
102 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<LangVersion>latest</LangVersion> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="DryIoc.Microsoft.DependencyInjection" Version="8.0.0-preview-03" /> | ||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.1" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// Sample from the official https://learn.microsoft.com/en-us/dotnet/core/extensions/dependency-injection-guidelines | ||
|
||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
using DryIoc; | ||
using DryIoc.Microsoft.DependencyInjection; | ||
using static System.Console; | ||
|
||
HostApplicationBuilder builder = Host.CreateApplicationBuilder(args); | ||
builder.Services.AddTransient<TransientDisposable>(); | ||
builder.Services.AddScoped<ScopedDisposable>(); | ||
builder.Services.AddSingleton<SingletonDisposable>(); | ||
|
||
// Integrate DryIoc | ||
builder.ConfigureContainer(new DryIocServiceProviderFactory(), | ||
// optional configuration action | ||
serviceProvider => | ||
{ | ||
// Factory returns the DryIocServiceProvider, but you may access the DryIoc container via the Container field. | ||
// Note: avoid storing the container instance in your app, because it depends on the current scope you're in. | ||
// Instead, if required, resolve it from the services as shown below. | ||
var c = serviceProvider.Container; | ||
c.Register<Bazz>(); | ||
}); | ||
|
||
|
||
using IHost host = builder.Build(); | ||
|
||
// Resolve the actual DryIoc.IContainer from the services. | ||
var container = host.Services.GetRequiredService<IContainer>(); | ||
var serviceProvider = container.Resolve<IServiceProvider>(); | ||
|
||
Console.WriteLine($"The actual container is {container.GetType().FullName}, and the service provider is {serviceProvider.GetType().FullName}"); | ||
|
||
ExemplifyDisposableScoping(host.Services, "Scope 1"); | ||
Console.WriteLine(); | ||
|
||
ExemplifyDisposableScoping(host.Services, "Scope 2"); | ||
Console.WriteLine(); | ||
|
||
await host.RunAsync(); | ||
|
||
static void ExemplifyDisposableScoping(IServiceProvider services, string scope) | ||
{ | ||
Console.WriteLine($"{scope}..."); | ||
|
||
using IServiceScope serviceScope = services.CreateScope(); | ||
Console.WriteLine($"The actual serviceScope implementation is {serviceScope.GetType().Name}"); | ||
|
||
IServiceProvider provider = serviceScope.ServiceProvider; | ||
|
||
_ = provider.GetRequiredService<TransientDisposable>(); | ||
_ = provider.GetRequiredService<ScopedDisposable>(); | ||
_ = provider.GetRequiredService<SingletonDisposable>(); | ||
_ = provider.GetRequiredService<Bazz>(); | ||
} | ||
|
||
public sealed class TransientDisposable : IDisposable | ||
{ | ||
public void Dispose() => Console.WriteLine($"{nameof(TransientDisposable)}.Dispose()"); | ||
} | ||
|
||
public sealed class ScopedDisposable : IDisposable | ||
{ | ||
public void Dispose() => Console.WriteLine($"{nameof(ScopedDisposable)}.Dispose()"); | ||
} | ||
|
||
public sealed class SingletonDisposable : IDisposable | ||
{ | ||
public void Dispose() => Console.WriteLine($"{nameof(SingletonDisposable)}.Dispose()"); | ||
} | ||
|
||
public record Bazz(TransientDisposable TransientDep, ScopedDisposable ScopedDep, SingletonDisposable SingletonDep); |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,14 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<LangVersion>10</LangVersion> | ||
|
||
<TargetFramework>net8.0</TargetFramework> | ||
<LangVersion>latest</LangVersion> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\DryIoc.Microsoft.DependencyInjection\DryIoc.Microsoft.DependencyInjection.csproj" /> | ||
<!-- <PackageReference Include="DryIoc.Microsoft.DependencyInjection" Version="6.0.0" /> --> | ||
<!-- <ProjectReference Include="..\..\src\DryIoc.Microsoft.DependencyInjection\DryIoc.Microsoft.DependencyInjection.csproj" /> --> | ||
<PackageReference Include="DryIoc.Microsoft.DependencyInjection" Version="8.0.0-preview-03" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters