ServiceHost.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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;
  7. using System.Web.Http.Cors;
  8. using System.Web.Http.SelfHost;
  9. namespace Pms.WebApiHost
  10. {
  11. public class ServiceHost
  12. {
  13. public HttpSelfHostServer server;
  14. public HttpSelfHostConfiguration config;
  15. public EnableCorsAttribute cors;
  16. public bool OnOff;
  17. public ServiceHost()
  18. {
  19. }
  20. /// <summary>
  21. ///
  22. /// </summary>
  23. /// <param name="ApiPort"></param>
  24. public void StartService(ushort ApiPort)
  25. {
  26. string Url = "http://localhost:" + ApiPort.ToString();
  27. // http://localhost:62200/api/Agv/orderAdd
  28. config = new HttpSelfHostConfiguration(Url);
  29. //config.MapHttpAttributeRoutes();
  30. //config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
  31. config.Formatters.Remove(config.Formatters.XmlFormatter);
  32. config.Formatters.Add(config.Formatters.JsonFormatter);
  33. config.Routes.MapHttpRoute(
  34. name: "DefaultApi",
  35. routeTemplate: "api/{controller}/{action}/{id}",
  36. defaults: new { id = RouteParameter.Optional }
  37. );
  38. cors = new EnableCorsAttribute("*", "*", "*");
  39. config.EnableCors(cors);
  40. server = new HttpSelfHostServer(config);
  41. server.OpenAsync().Wait();
  42. }
  43. }
  44. }