Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Microsoft.Extensions.Hosting;
- using Microsoft.Extensions.Logging;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading;
- using System.Threading.Tasks;
- namespace SubConInterfaceServer
- {
- public class Worker : BackgroundService
- {
- private readonly JokeService _jokeService;
- private readonly ILogger<Worker> _logger;
- public Worker(JokeService jokeService,
- ILogger<Worker> logger) =>
- (_jokeService, _logger) = (jokeService, logger);
- protected override async Task ExecuteAsync(CancellationToken stoppingToken)
- {
- while (!stoppingToken.IsCancellationRequested)
- {
- try
- {
- _logger.LogWarning("Create a new product");
- //Create a new product
- Product product = new Product
- {
- Name = "Gizmo",
- IsComplete = false
- };
- System.Uri url = null;
- try {
- url = await _jokeService.CreateProductAsync(product);
- } catch (Exception ex) {
- _logger.LogWarning(ex.Message);
- }
- // Get the product
- product = await _jokeService.getProductAsync(url.PathAndQuery);
- _logger.LogWarning("product's informaton as follows:\n");
- _logger.LogWarning("product.Id= " + product.Id);
- _logger.LogWarning("product.Name= " + product.Name);
- _logger.LogWarning("product.IsComplete= " + product.IsComplete);
- _logger.LogWarning("Updating Name.....");
- // Update the product
- product.Name = "aa";
- await _jokeService.UpdateProductAsync(product);
- product = await _jokeService.getProductAsync(url.PathAndQuery);
- _logger.LogWarning("product's informaton after updated as follows:\n");
- _logger.LogWarning("product.Id= " + product.Id);
- _logger.LogWarning("product.Name= " + product.Name);
- _logger.LogWarning("product.IsComplete= " + product.IsComplete);
- // Delete the product
- var statusCode = await _jokeService.DeleteProductAsync(product.Id);
- _logger.LogWarning($"Deleted (HTTP Status = {(int)statusCode})");
- await Task.Delay(TimeSpan.FromMinutes(1), stoppingToken);
- }
- catch (OperationCanceledException) { break; }
- await Task.Delay(1000, stoppingToken);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement