WebAPIDBService.cs 2.7 KB
Newer Older
cdy816's avatar
cdy816 已提交
1 2
using Cdy.Tag;
using Microsoft.AspNetCore.Hosting;
3 4 5 6 7
using Microsoft.Extensions.Hosting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
cdy816's avatar
cdy816 已提交
8 9 10 11 12
using Microsoft.AspNetCore.Builder;
using System.Net.WebSockets;
using Microsoft.AspNetCore.Http;
using System.Threading;
using Microsoft.AspNetCore.Routing;
13 14 15

namespace DBDevelopService
{
cdy816's avatar
cdy816 已提交
16
    public class WebAPIDBService
17 18 19 20 21 22
    {

        #region ... Variables  ...
        /// <summary>
        /// 
        /// </summary>
cdy816's avatar
cdy816 已提交
23
        public static WebAPIDBService Service = new WebAPIDBService();
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
        private IHost mhost;
        #endregion ...Variables...

        #region ... Events     ...

        #endregion ...Events...

        #region ... Constructor...

        #endregion ...Constructor...

        #region ... Properties ...

        #endregion ...Properties...

        #region ... Methods    ...

        /// <summary>
        /// 
        /// </summary>
        public void Start(int port)
        {
cdy816's avatar
cdy816 已提交
46 47 48 49 50 51 52 53 54
            try
            {
                LoggerService.Service.Info("WebAPIDBService", "Ready to start to WebAPI DBService.....");
                StartAsync("0.0.0.0", port);
            }
            catch(Exception ex)
            {
                LoggerService.Service.Erro("WebAPIDBService", ex.Message);
            }
55 56 57 58 59 60 61 62 63 64
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        public async void StartAsync(string ip, int port)
        {
            string sip = ip;
cdy816's avatar
cdy816 已提交
65
            if (!sip.StartsWith("http://"))
66
            {
cdy816's avatar
cdy816 已提交
67
                sip = "http://" + ip;
68 69 70
            }
            sip += ":" + port;
            mhost = CreateHostBuilder(sip).Build();
cdy816's avatar
cdy816 已提交
71
            LoggerService.Service.Info("WebAPIDBService", "启动服务:"+ sip);
72 73 74 75 76 77 78 79
            await mhost.StartAsync();
        }

        /// <summary>
        /// 
        /// </summary>
        public async void StopAsync()
        {
cdy816's avatar
cdy816 已提交
80
            LoggerService.Service.Info("WebAPIDBService", "关闭服务:");
cdy816's avatar
cdy816 已提交
81 82
            await mhost.StopAsync();
            mhost.Dispose();
83 84
        }

cdy816's avatar
cdy816 已提交
85 86 87 88 89
        /// <summary>
        /// 
        /// </summary>
        /// <param name="serverUrl"></param>
        /// <returns></returns>
90 91 92 93
        public static IHostBuilder CreateHostBuilder(string serverUrl) =>
           Host.CreateDefaultBuilder()
               .ConfigureWebHostDefaults(webBuilder =>
               {
cdy816's avatar
cdy816 已提交
94
                   webBuilder.UseStartup<WebAPIStartup>();
95 96 97 98 99 100 101 102 103 104
                   webBuilder.UseUrls(serverUrl);
               });

        #endregion ...Methods...

        #region ... Interfaces ...

        #endregion ...Interfaces...
    }
}