Ejemplo n.º 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);
        }
Ejemplo n.º 2
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            // Enable AppInsights
            services.AddApplicationInsightsTelemetry();

            // Controller setup
            services
            .AddMvc(
                option =>
            {
                // Use default ProtobufFormatterOptions
                ProtobufFormatterOptions formatterOptions = new ProtobufFormatterOptions();
                option.InputFormatters.Insert(1, new ProtobufInputFormatter(formatterOptions));
                option.OutputFormatters.Insert(1, new ProtobufOutputFormatter(formatterOptions));
                option.FormatterMappings.SetMediaTypeMappingForFormat(
                    "protobuf",
                    MediaTypeHeaderValue.Parse("application/x-protobuf")
                    );
            }
                )
            .SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

            #region Database Configuration

            // Get configuration sections
            services.Configure <CosmosSchemaConfigurationSection>(this.Configuration.GetSection("CosmosSchema"));

            // Create Cosmos Connection, based on connection string location
            if (!String.IsNullOrEmpty(this.Configuration.GetConnectionString("CosmosConnection")))
            {
                services.AddTransient(cf => new CosmosConnectionFactory(this.Configuration.GetConnectionString("CosmosConnection")));
            }
            else
            {
                // Attempt to pull from generic 'CosmosConnection' setting
                // Throws exception if not defined
                services.AddTransient(cf => new CosmosConnectionFactory(this.Configuration["CosmosConnection"]));
            }

            // Configure data repository implementations
            services.AddTransient <CosmosContext>();
            services.AddSingleton <IMatchMessageRepository, CosmosMatchMessageRepository>();

            #endregion

            // Configure service layer
            services.AddSingleton <IMessageService, MessageService>();

            // Enable API versioning
            services.AddApiVersioning(o =>
            {
                // Share supported API versions in headers
                o.ApiVersionReader = new QueryStringApiVersionReader("api-version");
                o.AssumeDefaultVersionWhenUnspecified = true;
                o.DefaultApiVersion = new ApiVersion(
                    DateTime.Parse(this.Configuration["DefaultApiVersion"])
                    );
                o.ReportApiVersions = true;
            });
            services.AddVersionedApiExplorer(
                options =>
            {
                // Enable API version in URL
                options.SubstituteApiVersionInUrl = true;
                options.DefaultApiVersion         = new ApiVersion(
                    DateTime.Parse(this.Configuration["DefaultApiVersion"])
                    );
            }
                );

            // Add Swagger generator
            services.AddTransient <IConfigureOptions <SwaggerGenOptions>, SwaggerConfigureOptions>();
            services.AddSwaggerGen(c =>
            {
                // Set the comments path for the Swagger JSON
                var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
                var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
                c.IncludeXmlComments(xmlPath);
            });
        }