HttpService.cs 1.0 KB
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.SelfHost;

namespace DeviceLib.WebApi
{
    public class HttpService : IDisposable
    {
        public int Port { get; set; }
        private readonly HttpSelfHostServer _server;
        public HttpService(int port)
        {
            this.Port = port;

            var config = new HttpSelfHostConfiguration($"http://0.0.0.0:{this.Port}");

            config.MapHttpAttributeRoutes();
            config.Routes.MapHttpRoute("DefaultApi", "nepconSmf/{controller}");
            config.Routes.MapHttpRoute("AgvApi", "api/{controller}");
            _server = new HttpSelfHostServer(config);
        }
        public Task StartHttpServer()
        {
            return _server.OpenAsync();
        }
        public Task CloseHttpServer()
        {
            return _server.CloseAsync();
        }
        public void Dispose()
        {
            _server?.Dispose();
        }
    }
}