RestApiServer.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Web.Http.SelfHost;
  7. namespace XdCxRhDW.App.WebAPI
  8. {
  9. public class RestApiServer
  10. {
  11. public const int PORT = 8091;
  12. private static HttpSelfHostServer _server;
  13. public static void StartUp()
  14. {
  15. string host = string.Empty;
  16. host ="127.0.0.1";
  17. try
  18. {
  19. if (_server != null)
  20. {
  21. _server.CloseAsync().Wait();
  22. _server.Dispose();
  23. _server = null;
  24. }
  25. string baseAddress = $"http://{host}:{PORT}";
  26. var config = new HttpSelfHostConfiguration(baseAddress);
  27. Console.WriteLine();
  28. Routes.ConfigureRoutes(config);
  29. Console.WriteLine("Instantiating The Server...");
  30. _server = new HttpSelfHostServer(config);
  31. _server.OpenAsync().Wait();
  32. Console.WriteLine("Server is Running Now and listen : " + baseAddress);
  33. }
  34. catch (Exception e)
  35. {
  36. throw e;
  37. }
  38. }
  39. public static void ShutDown()
  40. {
  41. if (_server != null)
  42. {
  43. _server.CloseAsync();
  44. _server.Dispose();
  45. }
  46. }
  47. }
  48. }