using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Cors;
using System.Web.Http.SelfHost;

namespace Pms.WebApiHost
{
    public class ServiceHost
    {
        public HttpSelfHostServer server;
        public HttpSelfHostConfiguration config;
        public EnableCorsAttribute cors;
        public bool OnOff;

        public ServiceHost()
        {
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ApiPort"></param>
        public void StartService(ushort ApiPort)
        {
            string Url = "http://localhost:" + ApiPort.ToString();
            // http://localhost:62200/api/Agv/orderAdd
            config = new HttpSelfHostConfiguration(Url);
            //config.MapHttpAttributeRoutes();
            //config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
            config.Formatters.Remove(config.Formatters.XmlFormatter);
            config.Formatters.Add(config.Formatters.JsonFormatter);
            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{action}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
            cors = new EnableCorsAttribute("*", "*", "*");
            config.EnableCors(cors);
            server = new HttpSelfHostServer(config);
            server.OpenAsync().Wait();
        }
    }

}
