developpement:dotnet:msioc
Différences
Ci-dessous, les différences entre deux révisions de la page.
Prochaine révision | Révision précédente | ||
developpement:dotnet:msioc [2023/05/11 19:50] – créée sgariepy | developpement:dotnet:msioc [2023/06/19 18:43] (Version actuelle) – [Exemple] sgariepy | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | ====== | + | ====== |
- | Recherche " | + | En utilisant le package '' |
+ | Documenté dans le contexte de .NET Core 7. | ||
Lifetimes | Lifetimes | ||
Ligne 14: | Ligne 15: | ||
* Kept alive until scope is disposed | * Kept alive until scope is disposed | ||
* Scope is defined for a HTTP request, or expliceitly defined | * Scope is defined for a HTTP request, or expliceitly defined | ||
- | * Transcient | + | * Transient |
- | * | + | * Initialized every time they are requested |
+ | * Instances cannot be shared accoss different components in an application | ||
+ | * Ideal for retrieving information from a database or API | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | ===== Example de " | ||
+ | |||
+ | Ceci n'est pas exactement le container, mais la configuration d'un container. | ||
+ | |||
+ | |||
+ | <code csharp> | ||
+ | using Microsoft.Extensions.DependencyInjection; | ||
+ | |||
+ | namespace Infrastructure; | ||
+ | |||
+ | public static class DependencyInjection | ||
+ | { | ||
+ | public static IServiceCollection AddInfrastructure(this IServiceCollection services) | ||
+ | { | ||
+ | services.AddSingleton< | ||
+ | |||
+ | return services; | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Ensuite avec le '' | ||
+ | |||
+ | <code csharp> | ||
+ | builder.Services | ||
+ | .AddApplication() | ||
+ | .AddInfrastructure(); | ||
+ | </ | ||
+ | |||
+ | ====== Hosted Service ====== | ||
+ | |||
+ | |||
+ | * Permet d' | ||
+ | * Configuré dans ASP.NET Core ou via un modèle Worker Service | ||
+ | * Worker Service sont faits pour des tâches en arrière-plan | ||
+ | * Utiliser ASP.NET Core pour ajouter un service // | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | <code csharp> | ||
+ | using Microsoft.Extensions.Hosting; | ||
+ | |||
+ | public class CustomHostedService: | ||
+ | { | ||
+ | public CustomHostedService() | ||
+ | { | ||
+ | Console.WriteLine(" | ||
+ | } | ||
+ | |||
+ | public async Task StartAsync(CancellationToken cancellationToken) | ||
+ | { | ||
+ | Console.WriteLine(" | ||
+ | |||
+ | await Task.Delay(1000, | ||
+ | } | ||
+ | |||
+ | public Task StopAsync(CancellationToken cancellationToken) | ||
+ | { | ||
+ | return Task.CompletedTask; | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | |||
+ | Dans la configuration du _container_: | ||
+ | |||
+ | <code csharp> | ||
+ | services | ||
+ | .AddTransient< | ||
+ | </ | ||
+ | |||
+ | _Injecter_ le service manuellement: | ||
+ | |||
+ | <code csharp> | ||
+ | app.Services.GetService(typeof(CustomHostedService)); | ||
+ | </ | ||
+ | |||
+ | |||
+ | ===== Background Service ===== | ||
+ | |||
+ | |||
+ | <code csharp> | ||
+ | using Microsoft.Extensions.Hosting; | ||
+ | |||
+ | public class CustomBackgroundService: | ||
+ | { | ||
+ | public CustomBackgroundService() | ||
+ | { | ||
+ | Console.WriteLine(" | ||
+ | } | ||
+ | |||
+ | protected override async Task ExecuteAsync(CancellationToken stoppingToken) | ||
+ | { | ||
+ | await Task.Delay(1000, | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | |||
+ | Dans la configuration du _container_: | ||
+ | |||
+ | <code csharp> | ||
+ | services | ||
+ | .AddHostedService< | ||
+ | </ | ||
+ | |||
+ | _Injecter_ le service manuellement: | ||
+ | |||
+ | <code csharp> | ||
+ | app.Services.GetService(typeof(CustomBackgroundService)); | ||
+ | </ | ||
+ | |||
+ | ==== Exemple ==== | ||
+ | |||
+ | |||
+ | <code csharp> | ||
+ | protected override async Task ExecuteTask(CancellationToken cancellationToken) | ||
+ | { | ||
+ | |||
+ | while (!cancellationToken.IsCancellationRequested) | ||
+ | { | ||
+ | using (var scope = _serviceProvider.CreateScope()) | ||
+ | { | ||
+ | var productService = scope.ServiceProvider.GetService< | ||
+ | |||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ====== Exemple exécutable dans LinqPad 7 ====== | ||
+ | |||
+ | Language: C# Program, .NET 7.0 | ||
+ | |||
+ | <code csharp> | ||
+ | void Main() | ||
+ | { | ||
+ | string[] args = new string[0]; | ||
+ | |||
+ | using IHost host = Host.CreateDefaultBuilder(args) | ||
+ | .ConfigureServices((_, | ||
+ | services | ||
+ | .AddSingleton< | ||
+ | .AddSingleton< | ||
+ | ) | ||
+ | .Build(); | ||
+ | |||
+ | using IServiceScope serviceScope = host.Services.CreateScope(); | ||
+ | IServiceProvider provider = serviceScope.ServiceProvider; | ||
+ | |||
+ | BasicService basicService = provider.GetService< | ||
+ | |||
+ | // host.Run(); | ||
+ | |||
+ | Console.WriteLine(" | ||
+ | } | ||
+ | |||
+ | #region Services | ||
+ | |||
+ | public class BasicService | ||
+ | { | ||
+ | private readonly ISimpleService _simpleService; | ||
+ | |||
+ | public BasicService( | ||
+ | ISimpleService simpleService) | ||
+ | { | ||
+ | _simpleService = simpleService; | ||
+ | |||
+ | SetupService(); | ||
+ | } | ||
+ | |||
+ | |||
+ | private void SetupService() | ||
+ | { | ||
+ | _simpleService.SayHello(); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | public interface ISimpleService { | ||
+ | public void SayHello(); | ||
+ | } | ||
+ | |||
+ | public class SimpleService: | ||
+ | { | ||
+ | private readonly ISimpleService _simpleService; | ||
+ | |||
+ | public SimpleService() {} | ||
+ | |||
+ | public void SayHello() | ||
+ | { | ||
+ | Console.WriteLine(" | ||
+ | } | ||
+ | } | ||
+ | |||
+ | # | ||
+ | </ | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
developpement/dotnet/msioc.1683827423.txt.gz · Dernière modification : 2023/05/11 19:50 de sgariepy