Пример #1
0
        public static IMvcCoreBuilder AddProtobufFormatters(this IMvcCoreBuilder builder, Action <ProtobufFormatterOptions> protobufFormatterOptionsConfiguration)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            var protobufFormatterOptions = new ProtobufFormatterOptions();

            protobufFormatterOptionsConfiguration?.Invoke(protobufFormatterOptions);

            foreach (var extension in protobufFormatterOptions.SupportedExtensions)
            {
                foreach (var contentType in protobufFormatterOptions.SupportedContentTypes)
                {
                    builder.AddFormatterMappings(m => m.SetMediaTypeMappingForFormat(extension, new MediaTypeHeaderValue(contentType)));
                }
            }

            builder.AddMvcOptions(options => options.InputFormatters.Add(new ProtobufInputFormatter(protobufFormatterOptions)));
            builder.AddMvcOptions(options => options.OutputFormatters.Add(new ProtobufOutputFormatter(protobufFormatterOptions)));

            return(builder);
        }