WebApiServerHost.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. using MaxCommunication.DataBase.Document;
  2. using System;
  3. using System.Web.Http;
  4. using System.Web.Http.Cors;
  5. using System.Web.Http.SelfHost;
  6. namespace ProjectManagementSystem.WebApi.Service
  7. {
  8. public class WebApiServerHost
  9. {
  10. public TextDocument textDocument { get; set; } = new TextDocument(@"PMS\Log", "WebApiErrorLog_");
  11. public WebApiServerHost(bool IsListenterWebApi)
  12. {
  13. if (IsListenterWebApi) { StartServer(); }
  14. }
  15. private void StartServer()
  16. {
  17. try
  18. {
  19. HttpSelfHostConfiguration httpSelfHostConfiguration = new HttpSelfHostConfiguration("http://localhost:63300");
  20. httpSelfHostConfiguration.Formatters.Remove(httpSelfHostConfiguration.Formatters.XmlFormatter);
  21. httpSelfHostConfiguration.Formatters.Add(httpSelfHostConfiguration.Formatters.JsonFormatter);
  22. httpSelfHostConfiguration.Routes.MapHttpRoute(
  23. name: "DefaultApi",
  24. routeTemplate: "api/{controller}/{action}/{id}",
  25. defaults: new { id = RouteParameter.Optional }
  26. );
  27. EnableCorsAttribute enableCorsAttribute = new EnableCorsAttribute("*", "*", "*");
  28. httpSelfHostConfiguration.EnableCors(enableCorsAttribute);
  29. HttpSelfHostServer httpSelfHostServer = new HttpSelfHostServer(httpSelfHostConfiguration);
  30. httpSelfHostServer.OpenAsync().Wait();
  31. }
  32. catch (Exception ex)
  33. {
  34. textDocument.WriteFile(ex.Message + "\n" + ex.StackTrace);
  35. }
  36. }
  37. }
  38. }