Esempio n. 1
0
        public void OutputModeltoOutputDTO_ShouldMapOutputModeltoOutputDTO()
        {
            //Arrange
            OutputModel model = new OutputModel {
                source_id = "789", address_line_1 = "10 Dalton Hill", gender = "M"
            };
            OutputDTO dto = new OutputDTO();

            var services = new ServiceCollection();

            services.AddAutoMapper(typeof(AutoMapping));
            services.AddTransient <IMappers, Mappers>();

            IServiceProvider serviceProvider = services.BuildServiceProvider();
            IMappers         mappers         = serviceProvider.GetRequiredService <IMappers>();

            //Act
            dto = mappers.OutputModeltoOutputDTO(model);

            //Assert
            Assert.NotNull(dto);
            Assert.Equal(model.source_id, dto.source_id);
            Assert.Equal(model.address_line_1, dto.address_line_1);
            Assert.Equal(model.gender, dto.gender);
        }
Esempio n. 2
0
        public void invokeLocalApp(InputDTO inputDTO, OutputDTO outputDTO, string serviceInstallPath)
        {
            string localAppPath = Path.Combine(serviceInstallPath, inputDTO.path);

            //判断本地程序是否存在
            if (!File.Exists(localAppPath))
            {
                outputDTO.code = ResultCode.Erroe;
                outputDTO.msg  = "【第三方程序集不存在】";

                return;
            }

            string args = " ";

            for (int i = 0; i < inputDTO.param.Count; i++)
            {
                args += inputDTO.param[i].value + " ";
            }


            Interop.CreateProcess(localAppPath, args);

            outputDTO.code = ResultCode.Success;
        }
Esempio n. 3
0
        /// <summary>
        /// Http向客户端输出返回值
        /// </summary>
        /// <param name="httpListenerContext"></param>
        /// <param name="outputDTO"></param>
        private void outputStreamToClient(HttpListenerContext httpListenerContext, OutputDTO outputDTO)
        {
            try
            {
                string rtnMsg = JsonHelper.Serialize <OutputDTO>(outputDTO);
                if (pramaterLoggerPrint)
                {
                    LoggerManager.writeLog(serviceLogFilePath, "服务器【返回】" + rtnMsg);
                }

                httpListenerContext.Response.StatusCode = 200;
                httpListenerContext.Response.Headers.Add("Access-Control-Allow-Origin", "*");
                httpListenerContext.Response.ContentType     = "application/json";
                httpListenerContext.Response.ContentEncoding = Encoding.UTF8;
                byte[] buffer = System.Text.Encoding.UTF8.GetBytes(rtnMsg);
                httpListenerContext.Response.ContentLength64 = buffer.Length;
                var output = httpListenerContext.Response.OutputStream;
                output.Write(buffer, 0, buffer.Length);
                output.Close();
            }
            catch (Exception e)
            {
                LoggerManager.writeLog(serviceLogFilePath, "【生成出参失败】,异常:" + e.ToString());
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Gets Json from Resources and Transforms it into the Output format
        /// </summary>
        /// <returns>string</returns>
        public string GetLearner()
        {
            LearnerDTO   learnerDTO   = JsonFactory.LoadJson();
            LearnerModel learnerModel = mappers.LearnerDTOtoLearnerModel(learnerDTO);
            OutputModel  outputModel  = mappers.LearnerModeltoOutputModel(learnerModel);
            OutputDTO    outputDTO    = mappers.OutputModeltoOutputDTO(outputModel);

            return(JsonFactory.JsonSerialize(outputDTO));
        }
Esempio n. 5
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);
            }
        }
Esempio n. 6
0
        public void JsonSerialize_ShouldSerializeDTOtoJson()
        {
            //Arrange
            OutputDTO output = new OutputDTO {
                source_id = "999", first_name = "Oscar", last_name = "Moby"
            };

            //Act
            string Json = JsonFactory.JsonSerialize(output);

            //Assert
            Assert.NotNull(Json);
        }
Esempio n. 7
0
        //回调具体的策略方法
        public void invokeLocalApp(InputDTO inputDTO, OutputDTO outputDTO, string serviceInstallPath)
        {
            if (inputDTO.type == 1)
            {
                quoteStrategy = new CallDLLStrategy();
            }
            else if (inputDTO.type == 2)
            {
                quoteStrategy = new CallExeStrategy();
            }
            else
            {
                throw new Exception("【调用类型不正确,请检查,TYPE = " + inputDTO.type + "】");
            }

            this.quoteStrategy.invokeLocalApp(inputDTO, outputDTO, serviceInstallPath);
        }
Esempio n. 8
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 = getHttpParameters(httpListenerContext.Request);
                    if (pramaterLoggerPrint)
                    {
                        LoggerHelper.WriteLog("入参:" + message);
                    }

                    OutputDTO outputDTO = new OutputDTO();
                    //调用业务处理类处理业务请求
                    RequestHandler requesthandler = new RequestHandler();
                    try
                    {
                        outputDTO = requesthandler.BusinessHandler(message);
                    }
                    catch (Exception e)
                    {
                        outputDTO      = new OutputDTO();
                        outputDTO.code = PublicValue.ResultCode.Erroe;
                        outputDTO.msg  = "【调取插件异常】";
                        LoggerHelper.WriteLog("调取插件异常", e);
                    }

                    //返回调用结果
                    string rtnMsg = JSONUtil.Serialize <OutputDTO>(outputDTO);
                    if (pramaterLoggerPrint)
                    {
                        LoggerHelper.WriteLog("出参:" + rtnMsg);
                    }

                    outputStreamToClient(httpListenerContext, rtnMsg);
                }));
                threadsub.Start(context);
            }
        }
Esempio n. 9
0
        /// <summary>
        /// 根据入参请求处理业务
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public OutputDTO BusinessHandler(string message)
        {
            InputDTO  inputDTO;
            OutputDTO outputDTO = new OutputDTO();

            try
            {
                inputDTO = JSONUtil.Deserialize <InputDTO>(message);
            }
            catch (Exception e)
            {
                LoggerHelper.WriteLog("JSON反序列化失败", e);

                outputDTO.code = PublicValue.ResultCode.Erroe;
                outputDTO.msg  = "【json格式不正确】";

                return(outputDTO);
            }

            string serverRootDir = Directory.GetCurrentDirectory();
            string localAppPath  = Path.Combine(serverRootDir, inputDTO.path);

            //检查本地程序是否存在
            if (!File.Exists(localAppPath))
            {
                outputDTO.code = PublicValue.ResultCode.Erroe;
                outputDTO.msg  = "【所调用程序不存在,请检查Plugin目录下是否存在】";

                return(outputDTO);
            }

            //根据参数实例化调用程序接口
            IDynamicInvoke dynamicInvoke;

            if (inputDTO.type == (int)PublicValue.InvokeType.DLL)
            {
                dynamicInvoke = new DynamicInvokeDLL();
            }
            else if (inputDTO.type == (int)PublicValue.InvokeType.EXE)
            {
                dynamicInvoke = new DynamicInvokeExe();
            }
            else
            {
                outputDTO.code = PublicValue.ResultCode.Erroe;
                outputDTO.msg  = "【调用类型不正确,请检查,TYPE = " + inputDTO.type + "】";
                return(outputDTO);
            }

            try
            {
                ReturnDTO resultDTO = dynamicInvoke.invokeLocalApp(inputDTO, localAppPath);
                outputDTO.returns = resultDTO;

                return(outputDTO);
            }
            finally
            {
                Directory.SetCurrentDirectory(serverRootDir);
            }
        }
Esempio n. 10
0
        /// <summary>
        /// Takes a OutputModel object and maps the data to a OutputDTO object
        /// </summary>
        /// <param name="outputModel"></param>
        /// <returns>OutputDTO</returns>
        public OutputDTO OutputModeltoOutputDTO(OutputModel outputModel)
        {
            OutputDTO dto = _mapper.Map <OutputDTO>(outputModel);

            return(dto);
        }
Esempio n. 11
0
 /// <summary>
 /// Takes a OutputDTO Object and serializes it.
 /// </summary>
 /// <returns>string</returns>
 public static string JsonSerialize(OutputDTO output)
 {
     return(JsonConvert.SerializeObject(output));
 }