HttpService.cs
1.0 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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();
}
}
}