WebService.cs 1.3 KB
using BLL;
using System;
using System.ServiceModel.Web;

namespace SmartScan
{
    public static class WebService
    {
        private static WebServiceHost _serviceHost;

        public static bool IsOpen { private set; get; }

        public static void Open()
        {
            try
            {
                string url = BLLCommon.config.WebService;
                if (string.IsNullOrWhiteSpace(url))
                {
                    Model.LogNet.log.Info("WebService没有配置,不开启");
                    return;
                }
                WebCallWork service = new();
                _serviceHost = new(service, new Uri(url));
                _serviceHost.Open();
                Model.LogNet.log.Info("Web服务已开启,URL=" + url);
                IsOpen = true;
            }
            catch (Exception ex)
            {
                Model.LogNet.log.Error("Open", ex);
                IsOpen = false;
            }
        }

        public static void Close()
        {
            if (_serviceHost != null)
            {
                if (_serviceHost.State != System.ServiceModel.CommunicationState.Faulted)
                    _serviceHost.Close();
            }
            Model.LogNet.log.Info("Web服务已关闭");
        }
    }

}