Exemple #1
0
        /// <summary>
        /// Sets the default TWCoreValues
        /// </summary>
        public static void SetDefaultTWCoreValues(this IServiceCollection services, CoreWebSettings settings = null)
        {
            settings = settings ?? new CoreWebSettings();
            services.AddMvc(options =>
            {
                try
                {
                    options.OutputFormatters.Add(new XmlSerializerOutputFormatter());

                    if (settings.EnableFormatMapping)
                    {
                        options.FormatterMappings.SetMediaTypeMappingForFormat
                            ("xml", MediaTypeHeaderValue.Parse("application/xml"));
                        options.FormatterMappings.SetMediaTypeMappingForFormat
                            ("js", MediaTypeHeaderValue.Parse("application/json"));
                    }

                    if (settings.EnableTWCoreSerializers)
                    {
                        var serializers = SerializerManager.GetBinarySerializers();
                        foreach (var serializer in serializers)
                        {
                            options.AddISerializerInputFormatter(serializer);
                            options.AddISerializerOutputFormatter(serializer);
                            if (settings.EnableFormatMapping)
                            {
                                options.FormatterMappings.SetMediaTypeMappingForFormat(serializer.Extensions[0].Substring(1),
                                                                                       MediaTypeHeaderValue.Parse(serializer.MimeTypes[0]));
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Core.Log.Write(ex);
                }
            })
            .AddJsonOptions(options =>
            {
                if (settings.EnableJsonStringEnum)
                {
                    options.SerializerSettings.Converters.Add(new Newtonsoft.Json.Converters.StringEnumConverter());
                }
            });
            services.AddLogging(cfg =>
            {
                if (settings.EnableTWCoreLogger)
                {
                    cfg.AddTWCoreLogger();
                }
            });
            if (settings.EnableGZipCompressor)
            {
                services.Configure <GzipCompressionProviderOptions>(options => options.Level = System.IO.Compression.CompressionLevel.Fastest);
                services.AddResponseCompression();
            }
        }