CoreDevice.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. using Microsoft.AspNetCore.Hosting.Server.Features;
  2. using Microsoft.Azure.Cosmos;
  3. using Microsoft.Extensions.DependencyInjection;
  4. using Microsoft.Extensions.Hosting;
  5. using Microsoft.Extensions.Logging;
  6. using Microsoft.Extensions.Options;
  7. using System;
  8. using System.Collections.Concurrent;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Net.Http;
  12. using System.Text;
  13. using System.Text.Json;
  14. using System.Threading.Tasks;
  15. using Microsoft.AspNetCore.Hosting.Server;
  16. namespace TEAMModelOS.SDK.DI.Device
  17. {
  18. public class CoreDevice
  19. {
  20. private readonly IServiceProvider _services;
  21. private readonly ILogger _logger;
  22. private readonly IHttpClientFactory _httpClientFactory;
  23. private readonly IHostApplicationLifetime _lifetime;
  24. private ConcurrentDictionary<string, Task <ClientDevice>> clientDevice { get; } = new ConcurrentDictionary<string, Task<ClientDevice>>();
  25. public CoreDevice(IServiceProvider services, ILogger<CoreDevice> logger, IHostApplicationLifetime lifetime, IHttpClientFactory httpClientFactory)
  26. {
  27. _services = services;
  28. _logger = logger;
  29. _services=services;
  30. _httpClientFactory=httpClientFactory;
  31. _lifetime = lifetime;
  32. }
  33. public Task<ClientDevice> GetCoreDevice()
  34. {
  35. try
  36. {
  37. var server = _services.GetService<IServer>();
  38. var d = server?.Features.Get<IServerAddressesFeature>();
  39. IEnumerable<string> _url = server?.Features.Get<IServerAddressesFeature>()?.Addresses;
  40. var device = clientDevice.GetOrAdd("Device", x => DeviceHelper.GetClientInfo(_httpClientFactory, _logger, _url));
  41. return device;
  42. }
  43. catch (Exception e)
  44. {
  45. _logger?.LogWarning(e, e.Message);
  46. throw;
  47. }
  48. }
  49. }
  50. }