private void LogRequest(string service, string operation, TestOperationRequest request)
        {
            var requestAsXml = _xmlSerializer.ToXml(request);

            using (Serilog.Context.LogContext.PushProperty("Request", requestAsXml, true))
            {
                Log.Here().Information("Request: {@service} {@Operation}", service, operation);
            }
        }
    private static void XmlGenericNullTest(IXmlSerializer serializer)
    {
        TestModel?model            = null;
        var       xml              = serializer.ToXml(model);
        var       deserializeModel = serializer.FromXml <TestModel>(xml);

        Assert.Null(deserializeModel);
    }
    private static void XmlNonGenericNullTest(IXmlSerializer serializer)
    {
        var type             = typeof(TestModel);
        var xml              = serializer.ToXml(type, null);
        var deserializeModel = serializer.FromXml(type, xml);

        Assert.Null(deserializeModel);
    }
예제 #4
0
    private static void XmlGenericTest(IXmlSerializer serializer)
    {
        var model            = TestModelFactory.Create();
        var xml              = serializer.ToXml(model);
        var deserializeModel = serializer.FromXml <TestModel>(xml) !;

        Assert.Equal(
            Tuple.Create(model.Id, model.Age, model.CreateTime, model.Name, model.Gender),
            Tuple.Create(deserializeModel.Id, deserializeModel.Age, deserializeModel.CreateTime,
                         deserializeModel.Name, deserializeModel.Gender));
    }
예제 #5
0
        private MailMessage ConstructMailMessage(string templatePath, object variables, string to, string subject)
        {
            if (templatePath == null)
            {
                throw new ArgumentNullException("templatePath");
            }
            if (to == null)
            {
                throw new ArgumentNullException("to");
            }

            if (!File.Exists(templatePath))
            {
                throw new FileNotFoundException("Template file not found.", templatePath);
            }

            var    compiler = _templateCompiler;
            string compiled;

            using (var xmlStream = new MemoryStream())
            {
                _xmlSerializer.ToXml(variables, xmlStream);
                using (var output = new MemoryStream())
                {
                    compiler.CompileXsltFromFile(xmlStream, templatePath, output);

                    output.Seek(0, SeekOrigin.Begin);
                    using (var reader = new StreamReader(output))
                    {
                        compiled = reader.ReadToEnd();
                    }
                }
            }

            string layoutHtml = LayoutHtml;

            if (layoutHtml != null)
            {
                compiled = _replaceRegex.Replace(layoutHtml, compiled);
            }

            var mailMessage = new MailMessage
            {
                Subject      = subject,
                IsBodyHtml   = true,
                BodyEncoding = System.Text.Encoding.UTF8,
                Body         = compiled
            };

            mailMessage.To.Add(to);

            return(mailMessage);
        }
예제 #6
0
        public MailMessage ConstructMailMessage(string templatePath, object variables)
        {
            if (templatePath == null)
            {
                throw new ArgumentNullException("templatePath");
            }

            if (!File.Exists(templatePath))
            {
                throw new FileNotFoundException("Template file not found.", templatePath);
            }

            var    compiler = _templateCompiler;
            string compiled;

            using (var xmlStream = new MemoryStream())
            {
                _xmlSerializer.ToXml(variables, xmlStream);

                xmlStream.Position = 0;
                var    sr    = new StreamReader(xmlStream);
                string myStr = sr.ReadToEnd();

                using (var output = new MemoryStream())
                {
                    compiler.CompileXsltFromFile(xmlStream, templatePath, output);

                    output.Seek(0, SeekOrigin.Begin);
                    using (var reader = new StreamReader(output))
                    {
                        compiled = reader.ReadToEnd();
                    }
                }
            }

            string layoutHtml = LayoutHtml;

            if (layoutHtml != null)
            {
                compiled = _replaceRegex.Replace(layoutHtml, compiled);
            }

            compiled = compiled.Replace("&lt;", "<").Replace("&gt;", ">");
            var mailMessage = new MailMessage
            {
                IsBodyHtml   = true,
                BodyEncoding = System.Text.Encoding.UTF8,
                Body         = compiled
            };

            return(mailMessage);
        }