-
-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
95 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="DryIoc.Microsoft.DependencyInjection" Version="6.1.0" /> | ||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.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,80 @@ | ||
using DryIoc; | ||
using DryIoc.Microsoft.DependencyInjection; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
|
||
namespace DryIocDemo | ||
{ | ||
internal class Program | ||
{ | ||
static void Main(string[] args) | ||
{ | ||
// var rules = Rules.MicrosoftDependencyInjectionRules; | ||
var rules = Rules.MicrosoftDependencyInjectionRules.WithConcreteTypeDynamicRegistrations(reuse: Reuse.Transient); | ||
// var rules = Rules.Default.WithConcreteTypeDynamicRegistrations(reuse: Reuse.Transient); | ||
// var rules = Rules.Default.WithConcreteTypeDynamicRegistrations(reuse: Reuse.Transient).WithMicrosoftDependencyInjectionRules(); | ||
|
||
var container = new Container(rules); | ||
|
||
var hostBuilder = Host.CreateDefaultBuilder() | ||
.UseServiceProviderFactory(new DryIocServiceProviderFactory(container)) | ||
.ConfigureServices(services => | ||
{ | ||
services.AddSingleton<IService, Service>(); | ||
}); | ||
|
||
|
||
var host = hostBuilder.Build(); | ||
|
||
// WORKAROUND for now! | ||
container = host.Services.GetService<IContainer>() as Container; | ||
|
||
try | ||
{ | ||
var resolveInterfaceFromContainer = container.Resolve<IService>(); | ||
} | ||
catch (Exception ex) | ||
{ | ||
Console.WriteLine("Catch a exception when resolve interface from Container: " + ex.Message); | ||
} | ||
|
||
try | ||
{ | ||
var resolveImplementFromContainer = container.Resolve<Service>(); | ||
} | ||
catch (Exception ex) | ||
{ | ||
Console.WriteLine("Catch a exception when resolve implement from Container" + ex.Message); | ||
} | ||
|
||
try | ||
{ | ||
var resolveInterfaceFromServiceProvider = host.Services.GetRequiredService<IService>(); | ||
} | ||
catch (Exception ex) | ||
{ | ||
Console.WriteLine("Catch a exception when resolve interface from ServiceProvider" + ex.Message); | ||
} | ||
|
||
try | ||
{ | ||
var resolveImplementFromServiceProvider = host.Services.GetRequiredService<Service>(); | ||
} | ||
catch (Exception ex) | ||
{ | ||
Console.WriteLine("Catch a exception when resolve implement from ServiceProvider" + ex.Message); | ||
} | ||
|
||
host.RunAsync(); | ||
} | ||
} | ||
|
||
|
||
public interface IService | ||
{ | ||
} | ||
|
||
public class Service : IService | ||
{ | ||
} | ||
} |