Last active
June 8, 2018 07:39
-
-
Save Hereigo/7d0131fe1f151d0f296e43abfacbd8a3 to your computer and use it in GitHub Desktop.
dotNetCore Start template.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.AspNetCore; | |
using Microsoft.AspNetCore.Builder; | |
using Microsoft.AspNetCore.Hosting; | |
using Microsoft.AspNetCore.Http; | |
using Microsoft.Extensions.DependencyInjection; | |
using Microsoft.Extensions.Logging; | |
public static class Tools | |
{ | |
public static string GetMethodName([System.Runtime.CompilerServices.CallerMemberName] string memberName = "") | |
{ | |
return memberName; | |
} | |
} | |
public class Program | |
{ | |
public static void Main(string[] args) | |
{ | |
// var host = new WebHostBuilder() | |
// .UseKestrel() | |
// .UseContentRoot(System.IO.Directory.GetCurrentDirectory()) | |
// .UseIISIntegration() | |
// .UseStartup<Startup>() | |
// .Build(); | |
// host.Run(); | |
// OR | |
BuildWebHost(args).Run(); | |
// OR | |
// using (var host = WebHost.Start("http://localhost:8080", async context => | |
// { | |
// context.Response.ContentType = "text/html; charset=utf-8"; | |
// await context.Response.WriteAsync("Привет мир!"); | |
// })) | |
// { | |
// host.WaitForShutdown(); | |
// } | |
} | |
// .Net Core - Start object : | |
// adjust Kestrel, set root dir, set configs & logs, etc... | |
public static IWebHost BuildWebHost(string[] args) => | |
WebHost.CreateDefaultBuilder(args) | |
.UseStartup<Startup>() // set Start CLASS for requests. | |
.Build(); // create host. | |
} | |
public class Startup | |
{ | |
IHostingEnvironment _env; | |
ILoggerFactory _loggerFactory; | |
ILogger _logger; | |
public Startup(IHostingEnvironment env) | |
{ | |
_env = env; | |
} | |
// DI | |
public void ConfigureServices(IServiceCollection services) | |
{ | |
ILoggerFactory loggerFactory = new Microsoft.Extensions.Logging.LoggerFactory(); | |
services.AddSingleton<ILoggerFactory>(loggerFactory); | |
} | |
// Must have - IApplicationBuilder ! (it configs Middleware components) | |
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggingFactory logs) | |
{ | |
_loggerFactory = loggerFactory; | |
_loggerFactory.AddConsole(LogLevel.Trace); // (Configuration.GetSection("Logging")); //levels set in configuration | |
//_loggerFactory.AddDebug(); //does all log levels | |
_logger = _loggerFactory.CreateLogger("MyTestLogger"); | |
// app.Use... creates Middleware components (public delegate Task RequestDelegate(HttpContext context);) | |
if (env.IsDevelopment()) | |
{ | |
app.UseDeveloperExceptionPage(); | |
} | |
// Middleware is a Singleton! | |
// app.UseMvc(routes => | |
// { | |
// routes.MapRoute( | |
// name: "default", | |
// template: "{controller}/{action=Index}/{id?}"); | |
// }); | |
// GET Request's Context here & then write Response : | |
app.Run(async (context) => | |
{ | |
_logger.LogDebug(" = APP.RUN(ASYNC (CONTEXT) - IS INVOKED! = "); | |
// await context.Response.WriteAsync("Hello World!"); | |
await context.Response.WriteAsync(_env.ApplicationName); | |
}); | |
// Asp.Net Core provides default Middleware Components : | |
// Authentication | |
// CORS: Cross-Domain requests support | |
// Response Caching | |
// Response Compression | |
// Routing | |
// Session | |
// Static Files | |
// URL Rewriting Middleware | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment