Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Newtonsoft.Json.Converters;
using Notifier.Common.Extensions;
using Notifier.Common.Settings;
using Notifier.WebApi.Common.Data;
using Notifier.WebApi.Common.Data.Entities;
using Notifier.WebApi.Services;
namespace Notifier.WebApi
{
public class Startup
{
public Startup(IConfiguration configuration, IWebHostEnvironment env)
{
Configuration = configuration;
Environment = env;
}
public IConfiguration Configuration { get; }
public IWebHostEnvironment Environment { get; }
public void ConfigureServices(IServiceCollection services)
{
services
.AddControllers(
options =>
{
// Needed because of using newtonsoft json
// (else there are performance issues in .net core 3)
options.SuppressOutputFormatterBuffering = true;
})
.AddNewtonsoftJson(
options =>
{
options.UseCamelCasing(true);
options.SerializerSettings.Converters.Add(new StringEnumConverter());
});
// Configure the settings to be part of "di" via IOptions<T>
services
.Configure<AzureTableSettings>(Configuration.GetSection(nameof(AzureTableSettings)))
.Configure<EventHubSettings>(Configuration.GetSection(nameof(EventHubSettings)));
// Add application insights.
services.AddApplicationInsights(Configuration);
// Add the the needed service registrations
services.AddSingleton<IRepository<NotificationEntity>, StorageTableRepository<NotificationEntity>>();
services.AddSingleton<INotificationService, NotificationService>();
services.AddMvcCore();
services.AddCors(options =>
{
options.AddDefaultPolicy(cors =>
{
cors
.WithOrigins(Configuration.GetSection(nameof(CorsSettings)).Get<CorsSettings>().AllowedOrigins)
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials();
});
});
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseCors();
if (env.IsDevelopment())
app.UseDeveloperExceptionPage();
else
app.UseHsts();
app.UseHttpsRedirection();
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
}
@shenssy

This comment has been minimized.

Copy link

@shenssy shenssy commented Nov 12, 2020

Good

@shenssy

This comment has been minimized.

Copy link

@shenssy shenssy commented Nov 12, 2020

Good!

@screamingworld

This comment has been minimized.

Copy link
Owner Author

@screamingworld screamingworld commented Nov 12, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment