Outils pour utilisateurs

Outils du site


developpement:dotnet:msioc

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Les deux révisions précédentesRévision précédente
Prochaine révision
Révision précédente
developpement:dotnet:msioc [2023/05/12 15:43] sgariepydeveloppement:dotnet:msioc [2023/06/19 18:43] (Version actuelle) – [Exemple] sgariepy
Ligne 53: Ligne 53:
  
 ====== Hosted Service ====== ====== Hosted Service ======
 +
 +
 +  * Permet d'exécuter des tâches en arrière-plan
 +  * 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 //hosted//
 +
 +
  
  
Ligne 58: Ligne 66:
 using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
  
-public class CustomHostedBackgroundService: IHostedService, IDisposable+public class CustomHostedService: IHostedService, IDisposable
 { {
-    public CustomHostedBackgroundService()+    public CustomHostedService()
     {     {
         Console.WriteLine("ctor");         Console.WriteLine("ctor");
Ligne 84: Ligne 92:
 <code csharp> <code csharp>
 services services
-    .AddTransient<IHostedService, CustomHostedBackgroundService>();+    .AddTransient<IHostedService, CustomHostedService>();
 </code> </code>
 +
 +_Injecter_ le service manuellement:
 +
 +<code csharp>
 +app.Services.GetService(typeof(CustomHostedService));
 +</code>
 +
 +
 +===== Background Service =====
 +
 +
 +<code csharp>
 +using Microsoft.Extensions.Hosting;
 +
 +public class CustomBackgroundService: BackgroundService
 +{
 +    public CustomBackgroundService()
 +    {
 +        Console.WriteLine("ctor");
 +    }
 +    
 +    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
 +    {
 +        await Task.Delay(1000, stoppingToken);
 +    }
 +}
 +</code>
 +
 +
 +Dans la configuration du _container_:
 +
 +<code csharp>
 +services
 +    .AddHostedService<CustomBackgroundService>();
 +</code>
 +
 +_Injecter_ le service manuellement:
 +
 +<code csharp>
 +app.Services.GetService(typeof(CustomBackgroundService));
 +</code>
 +
 +==== Exemple ====
 +
 +
 +<code csharp>
 +protected override async Task ExecuteTask(CancellationToken cancellationToken)
 +{
 +
 +    while (!cancellationToken.IsCancellationRequested)
 +    {
 +        using (var scope = _serviceProvider.CreateScope())
 +        {
 +            var productService = scope.ServiceProvider.GetService<IProductService>();
 +        
 +        }
 +    }
 +}
 +</code>
 +
 +====== 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) =>
 + services
 + .AddSingleton<ISimpleService, SimpleService>()
 + .AddSingleton<BasicService>()
 + )
 + .Build();
 +
 + using IServiceScope serviceScope = host.Services.CreateScope();
 + IServiceProvider provider = serviceScope.ServiceProvider;
 +
 + BasicService basicService = provider.GetService<BasicService>();
 +
 + // host.Run();
 +
 + Console.WriteLine("End of program");
 +}
 +
 +#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: ISimpleService
 +{
 + private readonly ISimpleService _simpleService;
 +
 + public SimpleService() {}
 +
 + public void SayHello()
 + {
 + Console.WriteLine("Hello");
 + }
 +}
 +
 +#endregion
 +</code>
 + 
 +
 +
 +
 +
  
  
developpement/dotnet/msioc.1683899014.txt.gz · Dernière modification : 2023/05/12 15:43 de sgariepy