EEMSBaseServer.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. 
  2. namespace EEMSService;
  3. public class EEMSBaseServer : IDisposable
  4. {
  5. public WebApplication? WebApplication { get; private set; }
  6. public bool Initialize(long messageSize = 26144)//26144:256kb
  7. {
  8. if (WebApplication is not null)
  9. return false;
  10. WebApplicationBuilder builder = WebApplication.CreateBuilder();
  11. builder.Services.AddSignalR(
  12. options =>
  13. {
  14. options.EnableDetailedErrors = true;
  15. options.MaximumReceiveMessageSize = messageSize;
  16. });
  17. builder.Services.AddHostedService<HostLifeTime>();
  18. builder.Services.AddSingleton<DeviceManager>();
  19. builder.Services.AddSingleton<ClientManager>();
  20. builder.Services.AddSingleton<UISender>();
  21. WebApplication = builder.Build();
  22. WebApplication.MapHub<UIHub>("/UIHub");
  23. WebApplication.MapHub<ClientsHub>("/ClientHub");
  24. return true;
  25. }
  26. public async Task<bool> StartService(string ip, ushort port)
  27. {
  28. if (WebApplication is null)
  29. return false;
  30. if (string.IsNullOrEmpty(ip) || port < 1000)
  31. return false;
  32. try
  33. {
  34. await WebApplication.RunAsync($"http://{ip}:{port}");
  35. }
  36. catch
  37. {
  38. return false;
  39. }
  40. return true;
  41. }
  42. #region Dispose
  43. private bool disposedValue;
  44. protected virtual void Dispose(bool disposing)
  45. {
  46. if (!disposedValue)
  47. {
  48. if (disposing)
  49. {
  50. // TODO: dispose managed state (managed objects)
  51. this.WebApplication?.DisposeAsync().AsTask().Wait();
  52. this.WebApplication = null;
  53. }
  54. // TODO: free unmanaged resources (unmanaged objects) and override finalizer
  55. // TODO: set large fields to null
  56. disposedValue = true;
  57. }
  58. }
  59. // // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
  60. // ~EEMS_Server()
  61. // {
  62. // // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
  63. // Dispose(disposing: false);
  64. // }
  65. public void Dispose()
  66. {
  67. // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
  68. Dispose(disposing: true);
  69. GC.SuppressFinalize(this);
  70. }
  71. #endregion
  72. }