123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- using System;
- using System.Web.Http;
- using System.Web.Http.Cors;
- using System.Web.Http.SelfHost;
- namespace WCS.API
- {
- public class WcsServerHost
- {
- private static WcsServerHost instance = null;
- private static HttpSelfHostServer httpSelfHostServer = null;
- private static readonly object obj_Locker = new object();
- private WcsServerHost()
- {
- }
- public static WcsServerHost Instance
- {
- get
- {
- if (instance == null)
- {
- lock (obj_Locker)
- {
- if (instance == null)
- {
- instance = new WcsServerHost();
- }
- }
- }
- return instance;
- }
- }
- public string GetStr()
- {
- return "=================================";
- }
- public bool CloseServer()
- {
- try
- {
- if (httpSelfHostServer != null)
- {
- httpSelfHostServer.CloseAsync();
- }
- return true;
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.ToString());
- return false;
- }
- }
- public bool StartServer(string uri)
- {
- try
- {
- // string address = "http://localhost:3350";192.168.2.54:3350/api/AgvMes/ReleaseAGV
- HttpSelfHostConfiguration httpSelfHostConfiguration = new HttpSelfHostConfiguration(uri);
- httpSelfHostConfiguration.Formatters.Remove(httpSelfHostConfiguration.Formatters.XmlFormatter);
- httpSelfHostConfiguration.Formatters.Add(httpSelfHostConfiguration.Formatters.JsonFormatter);
-
- httpSelfHostConfiguration.Routes.MapHttpRoute //Ìí¼Ó·ÓÉ
- (name: "wcsapi",
- routeTemplate: "api/{controller}/{action}/{id}",
- defaults: new { id = RouteParameter.Optional });
- EnableCorsAttribute enableCorsAttribute = new EnableCorsAttribute("*", "*", "*");
- httpSelfHostConfiguration.EnableCors(enableCorsAttribute);
- httpSelfHostServer = new HttpSelfHostServer(httpSelfHostConfiguration);
- httpSelfHostServer.OpenAsync().Wait();
- return true;
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.ToString());
- return false;
- }
- }
- }
- }
|