WebService.cs 2.8 KB
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Runtime.Serialization;
using System.ServiceModel.Activation;
using System;

namespace BLL
{
    [ServiceContract(Name = "Services")]
    internal interface IWebService
    {
        [OperationContract]
        [WebGet(UriTemplate = "CreateEmptyRecycleTask?emptyStation={line}", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
        Result CreateEmptyRecycleTask(string line);
        
    }

    [DataContract]
    internal class Result
    {
        [DataMember]
        public string Succeed { get; set; }

        [DataMember]
        public string ResultData { get; set; }

        [DataMember]
        public string ErrorMessage { get; set; }

    }

    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Single, IncludeExceptionDetailInFaults = true)]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    internal class ClsWebService : IWebService
    {
        internal ClsWebService()
        {
        }

        public Result CreateEmptyRecycleTask(string line)
        {
            AGVControl.Common.log.Info("WebService Request emptyStation=" + line);

            Result res;
            if (AGVControl.Common.agvProductionLine.TryGetValue(line, out string value))
            {
                res = new Result() { Succeed = "true", ResultData = "", ErrorMessage = "" };
                AGVControl.Common.log.Info("WebService Response OK");

                //加到任务
                AGVControl.Common.linePlace.Add(value);
            }
            else
            {
                res = new Result() { Succeed = "false", ResultData = "", ErrorMessage = "Not find " + line };
                AGVControl.Common.log.Info("WebService Response false");
            }

            return res;
        }
    }

    public class WebService
    {
        private WebServiceHost _serviceHost;

        public void Open(string url)
        {
            try
            {
                ClsWebService service = new ClsWebService();
                _serviceHost = new WebServiceHost(service, new Uri(url));
                _serviceHost.Open();
                AGVControl.Common.log.Info("Web服务已开启");
            }
            catch (Exception ex)
            {
                AGVControl.Common.log.Error("Open", ex);
            }
        }

        public void Close()
        {
            try
            {
                _serviceHost.Close();
                AGVControl.Common.log.Info("Web服务已关闭");
            }
            catch (Exception ex)
            {
                AGVControl.Common.log.Error("Close", ex);
            }

        }
    }



}