Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

read configuration workerservice

public class Program {
    public static void Main(string[] args) {
        CreateHostBuilder(args).Build().Run();
    }

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureServices((hostContext, services) => {
                IConfiguration configuration = hostContext.Configuration;

                WorkerOptions options = configuration
                  .GetSection("WCF")
                  .Get<WorkerOptions>();

                services.AddSingleton(options);

                services.AddHostedService<Worker>();
            });
}

public class Worker : BackgroundService {
    private readonly ILogger<Worker> logger;
    private readonly WorkerOptions options;

    public Worker(ILogger<Worker> logger, WorkerOptions options) {
        this.logger = logger;
        this.options = options;
    }

    protected override async Task ExecuteAsync(CancellationToken stoppingToken) {
        while (!stoppingToken.IsCancellationRequested) {
            //do something that uses options

            logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
            await Task.Delay(5000, stoppingToken);
        }
    }
}

public class WorkerOptions {
    public string AminServiceUri { get; set; }
    public string BillServiceUri { get; set; }

    //... other properties
}

//File appsettings.json
{
  "WCF": {
    "AminServiceUri":"http://localhost:45108/ServiceHost/v1/AminService.svc",
    "BillServiceUri":"http://localhost:45108/ServiceHost/v1/BillService.svc",

    //...other key-value pairs
  },
  "Logging": {
    "ExcessiveLogging": false
  }

}

Comment

read configuration workerservice


public class Worker : BackgroundService {
    private readonly ILogger<Worker> logger;
    private readonly WorkerOptions options;

    public Worker(ILogger<Worker> logger, WorkerOptions options) {
        this.logger = logger;
        this.options = options;
    }

    protected override async Task ExecuteAsync(CancellationToken stoppingToken) {
        while (!stoppingToken.IsCancellationRequested) {
            //do something that uses options

            logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
            await Task.Delay(5000, stoppingToken);
        }
    }
}

Comment

PREVIOUS NEXT
Code Example
Csharp :: c# 8 null coalescing assignment 
Csharp :: currentTimeMillis c# 
Csharp :: asp.net core redirecttoaction with parameters 
Csharp :: why vue cli do not refresh auto in local host 
Csharp :: hide game obj oncollisionenter 
Csharp :: C# infinite clock coroutine loop 
Csharp :: change a dropdown to a specific option via script 
Csharp :: xamarin picker item 
Csharp :: unity invisible cube 
Csharp :: find many object with tag unity 
Csharp :: snx disconnect linux 
Csharp :: how to write switch statement unity 
Csharp :: unity how to ommit letters from a string 
Csharp :: how to compare 2 date time in asp.net core 3.1 
Csharp :: how to unfreeze a rotation in a collider unity 2d 
Csharp :: c# how to open file explorer 
Csharp :: monogame fullscreen 
Csharp :: mvc input type file 
Csharp :: vb.net open file with default program 
Csharp :: c# initialize array 
Csharp :: object to mouse unity 
Csharp :: c# datetimepicker set weeks before today 
Csharp :: c# and 
Csharp :: c# console beep sounds 
Csharp :: c# check if array is empty 
Csharp :: c# encrypt decrypt string 
Csharp :: bootrap modal 
Csharp :: c# entity framework code first connection string 
Csharp :: c# find one object in list where 
Csharp :: unity random range 
ADD CONTENT
Topic
Content
Source link
Name
6+1 =