示例#1
0
        /// <inheritdoc />
        public override Task ExecuteResultAsync(ActionContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }
            var services = context.HttpContext.RequestServices;
            IXmlResultExecutor executor    = null;
            string             serviceName = string.Empty;

            switch (XmlSerializerType)
            {
            case XmlSerializerType.XmlSeriralizer:
                executor    = services.GetService <XmlResultExecutor>();
                serviceName = "XmlSerializerFormatterServices";
                break;

            case XmlSerializerType.DataContractSerializer:
                executor    = services.GetService <XmlDcResultExecutor>();
                serviceName = "XmlDataContractSerializerFormatterServices";
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(XmlSerializerType));
            }
            if (executor == null)
            {
                var loggerFactory = services.GetRequiredService <ILoggerFactory>();
                var logger        = loggerFactory.CreateLogger <XmlResult>();
                // No formatter supports this.
                logger.NoExecutor(XmlSerializerType.ToString());
                context.HttpContext.Response.StatusCode = StatusCodes.Status406NotAcceptable;
#if DEBUG
                var msg = Resources.XmlFromater_WasNotSetup_To_Mvc(serviceName);
                context.HttpContext.Response.ContentType = "text/html";
                MvcXmlLoggerExtensions.StringToHttpContext(context.HttpContext, msg);
#endif
                return(Task.FromResult(0));
            }

            return(executor.ExecuteAsync(context, this));
        }
示例#2
0
        public async Task ExecuteAsync_XmlExecutorDataContractContent()
        {
            // Arrange
            var value   = new PurchaseOrder();
            var context = GetActionContext();

            CreateServices(context.HttpContext);

            //
            var result = new XmlResult(value)
            {
                XmlSerializerType = XmlSerializerType.DataContractSerializer
            };
            var services = context.HttpContext.RequestServices;
            IXmlResultExecutor executor = null;

            executor = services.GetService <XmlDcResultExecutor>();

            // Act
            await executor.ExecuteAsync(context, result);

            // Assert
            Assert.Equal("application/xml; charset=utf-8", context.HttpContext.Response.ContentType);

            // Verify to as the new restored object
            //There may be differ DataContract style has been used
            var written  = GetWrittenBytes(context.HttpContext);
            var sWritten = Encoding.UTF8.GetString(written);

            StringReader           sreader  = new StringReader(sWritten);
            DataContractSerializer ser      = new DataContractSerializer(typeof(PurchaseOrder));
            PurchaseOrder          newValue = (PurchaseOrder)ser.ReadObject(XmlReader.Create(sreader));

            Assert.Equal(value.billTo.street, newValue.billTo.street);
            Assert.Equal(value.shipTo.street, newValue.shipTo.street);
        }
示例#3
0
        public async Task ExecuteAsync_XmlExecutorContent()
        {
            // Arrange
            var value             = new PurchaseOrder();
            var xmlWriterSettings = FormattingUtilities.GetDefaultXmlWriterSettings();

            xmlWriterSettings.CloseOutput = false;
            var textw         = new StringWriter();
            var writer        = XmlWriter.Create(textw, xmlWriterSettings);
            var xmlSerializer = new XmlSerializer(value.GetType());

            xmlSerializer.Serialize(writer, value);
            var expected = Encoding.UTF8.GetBytes(textw.ToString());
            var context  = GetActionContext();

            CreateServices(context.HttpContext);

            var services = context.HttpContext.RequestServices;
            IXmlResultExecutor executor = null;

            executor = services.GetService <XmlResultExecutor>();
            var result = new XmlResult(value);

            // Act
            await executor.ExecuteAsync(context, result);

            // Assert
            var written = GetWrittenBytes(context.HttpContext);

            var s1 = Encoding.UTF8.GetString(expected);
            var s2 = Encoding.UTF8.GetString(written);

            Assert.Equal(expected, written);
            Assert.Equal(s1, s2);
            Assert.Equal("application/xml; charset=utf-8", context.HttpContext.Response.ContentType);
        }