Skip to content

Instantly share code, notes, and snippets.

@benaadams
Last active May 20, 2020 13:11
Show Gist options
  • Save benaadams/f8f5b3ea3354643b954f102fb778f191 to your computer and use it in GitHub Desktop.
Save benaadams/f8f5b3ea3354643b954f102fb778f191 to your computer and use it in GitHub Desktop.
Microservice Lib + App
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using System;
namespace MicroserviceLib
{
public class Microservice : Controller, IStartup
{
public static void Run<TStartup>(string[] args) where TStartup : class
=> WebHost.CreateDefaultBuilder(args)
.UseStartup<TStartup>()
.Build().Run();
IServiceProvider IStartup.ConfigureServices(IServiceCollection services)
{
services.AddMvc();
return services.BuildServiceProvider();
}
void IStartup.Configure(IApplicationBuilder app)
=> app.UseMvc();
}
}
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using MicroserviceLib;
public class Program : Microservice
{
private ILogger<Program> _log;
public Program(ILogger<Program> log)
{
_log = log;
}
[HttpGet("/")]
public string Hello()
{
_log.LogInformation("Two lines made me add brackets 😢");
return "Hello World";
}
public static void Main(string[] args)
=> Run<Program>(args);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment