コード例 #1
0
        /// <summary>
        /// 监听Http请求
        /// </summary>
        private void httpRequestHandle()
        {
            while (true)
            {
                HttpListenerContext context = httpListener.GetContext();
                Thread threadsub            = new Thread(new ParameterizedThreadStart((requestContext) =>
                {
                    HttpListenerContext httpListenerContext = (HttpListenerContext)requestContext;
                    string message = getHttpJsonData(httpListenerContext.Request);

                    if (pramaterLoggerPrint)
                    {
                        LoggerManager.writeLog(serviceLogFilePath, "服务器:【收到】" + message);
                    }

                    //JSON字符串反序列化
                    InputDTO inputDTO   = null;
                    OutputDTO outputDTO = new OutputDTO();
                    try
                    {
                        inputDTO = JsonHelper.Deserialize <InputDTO>(message);
                    }
                    catch (Exception e)
                    {
                        LoggerManager.writeLog(serviceLogFilePath, "【json格式不正确】,异常:" + e.ToString());

                        outputDTO.code = ResultCode.Erroe;
                        outputDTO.msg  = "【json格式不正确】";
                        outputStreamToClient(httpListenerContext, outputDTO);
                        return;
                    }

                    //调用本地程序
                    QuoteContext quoteContext = new QuoteContext();
                    try
                    {
                        quoteContext.invokeLocalApp(inputDTO, outputDTO, serviceInstallPath);
                    }
                    catch (Exception e)
                    {
                        outputDTO.code = ResultCode.Erroe;
                        outputDTO.msg  = "【调取插件异常】";
                        LoggerManager.writeLog(serviceLogFilePath, outputDTO.msg + e.ToString());
                    }
                    finally
                    {
                        Directory.SetCurrentDirectory(serviceInstallPath);
                    }

                    outputStreamToClient(httpListenerContext, outputDTO);
                }));
                threadsub.Start(context);
            }
        }