Skip to content

Instantly share code, notes, and snippets.

@khellang
Last active April 13, 2020 01:58
Show Gist options
  • Save khellang/c8bc619b1f24b396dda4f20f5838dd40 to your computer and use it in GitHub Desktop.
Save khellang/c8bc619b1f24b396dda4f20f5838dd40 to your computer and use it in GitHub Desktop.
public class Startup : IStartup
{
private static readonly Assembly[] ControllerAssemblies = { typeof(Startup).GetTypeInfo().Assembly };
public Startup(IHostingEnvironment environment, ILoggerFactory logging)
{
Environment = environment;
ConfigureLogging(logging);
}
private IHostingEnvironment Environment { get; }
private static void ConfigureLogging(ILoggerFactory logging) =>
logging.AddDebug()
.AddConsole();
public IServiceProvider ConfigureServices(IServiceCollection services) =>
services.Scan(ConfigureServiceConventions)
.AddMvcCore(ConfigureMvcCore)
.BuildServiceProvider();
private static void ConfigureServiceConventions(IAssemblySelector selector) =>
selector.FromAssemblyOf<Startup>()
.AddClasses(x => x.AssignableToAny(
typeof(ICommandHandler<>),
typeof(IEventHandler<>),
typeof(IRepository<>),
typeof(IEventStore)))
.AsImplementedInterfaces()
.WithScopedLifetime()
.AddClasses(x => x.AssignableToAny(
typeof(ICommandBus),
typeof(IEventBus)))
.AsImplementedInterfaces()
.WithSingletonLifetime();
private void ConfigureMvcCore(IMvcCoreBuilder mvc) =>
mvc.AddControllersAsServices(ControllerAssemblies)
.AddJsonFormatters(ConfigureJson)
.AddAuthorization()
.AddCors();
private void ConfigureJson(JsonSerializerSettings settings)
{
settings.Formatting = Environment.IsDevelopment() ? Formatting.Indented : Formatting.None;
settings.ContractResolver = new CamelCasePropertyNamesContractResolver();
settings.NullValueHandling = NullValueHandling.Ignore;
}
public void Configure(IApplicationBuilder app) =>
app.UseIISPlatformHandler()
.UseCors(ConfigureCors)
.UseMvc();
private static void ConfigureCors(CorsPolicyBuilder policy) =>
policy.AllowAnyHeader()
.AllowAnyMethod()
.AllowAnyOrigin();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment