Advertisement
vim_fans

Untitled

Sep 8th, 2021
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1. using Microsoft.Extensions.Hosting;
  2. using Microsoft.Extensions.Logging;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8.  
  9. namespace SubConInterfaceServer
  10. {
  11. public class Worker : BackgroundService
  12. {
  13. private readonly JokeService _jokeService;
  14. private readonly ILogger<Worker> _logger;
  15.  
  16. public Worker(JokeService jokeService,
  17. ILogger<Worker> logger) =>
  18. (_jokeService, _logger) = (jokeService, logger);
  19.  
  20.  
  21. protected override async Task ExecuteAsync(CancellationToken stoppingToken)
  22. {
  23.  
  24. while (!stoppingToken.IsCancellationRequested)
  25. {
  26. try
  27. {
  28.  
  29. _logger.LogWarning("Create a new product");
  30. //Create a new product
  31. Product product = new Product
  32. {
  33. Name = "Gizmo",
  34. IsComplete = false
  35. };
  36. System.Uri url = null;
  37. try {
  38. url = await _jokeService.CreateProductAsync(product);
  39. } catch (Exception ex) {
  40. _logger.LogWarning(ex.Message);
  41. }
  42. // Get the product
  43. product = await _jokeService.getProductAsync(url.PathAndQuery);
  44. _logger.LogWarning("product's informaton as follows:\n");
  45. _logger.LogWarning("product.Id= " + product.Id);
  46. _logger.LogWarning("product.Name= " + product.Name);
  47. _logger.LogWarning("product.IsComplete= " + product.IsComplete);
  48.  
  49. _logger.LogWarning("Updating Name.....");
  50. // Update the product
  51. product.Name = "aa";
  52. await _jokeService.UpdateProductAsync(product);
  53. product = await _jokeService.getProductAsync(url.PathAndQuery);
  54. _logger.LogWarning("product's informaton after updated as follows:\n");
  55. _logger.LogWarning("product.Id= " + product.Id);
  56. _logger.LogWarning("product.Name= " + product.Name);
  57. _logger.LogWarning("product.IsComplete= " + product.IsComplete);
  58.  
  59.  
  60. // Delete the product
  61. var statusCode = await _jokeService.DeleteProductAsync(product.Id);
  62. _logger.LogWarning($"Deleted (HTTP Status = {(int)statusCode})");
  63. await Task.Delay(TimeSpan.FromMinutes(1), stoppingToken);
  64. }
  65. catch (OperationCanceledException) { break; }
  66. await Task.Delay(1000, stoppingToken);
  67. }
  68. }
  69. }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement