using Microsoft.AspNetCore.Server.Kestrel.Core; using NLog; using System.Net; namespace ChatRoom; public class Server { public static void Main(string[] args) { var self = new Server(); self.Run(args); } /// /// Logger for this class. /// Logger log = LogManager.GetCurrentClassLogger(); /// /// Configures logging subsystem. /// private void ConfigureLogging() { var config = new NLog.Config.LoggingConfiguration(); var console = new NLog.Targets.ConsoleTarget("console") { Layout = @"${date:format=HH\:mm\:ss}|${level}| ${message} ${exception}" }; config.AddTarget(console); config.AddRuleForAllLevels(console); LogManager.Configuration = config; } /// /// Program body. /// /// Command line arguments. private void Run(string[] args) { //configure logging ConfigureLogging(); //indicate server is about to start log.Info("Server is about to start"); //start the server StartServer(args); } /// /// Starts integrated server. /// /// Command line arguments. private void StartServer(string[] args) { //create web app builder var builder = WebApplication.CreateBuilder(args); //configure integrated server builder.WebHost.ConfigureKestrel(opts => { opts.Listen(IPAddress.Loopback, 5000, opts => { opts.Protocols = HttpProtocols.Http2; }); }); //add support for GRPC services builder.Services.AddGrpc(); //add the actual services builder.Services.AddSingleton(new ChatRoomService()); //build the server var app = builder.Build(); //turn on request routing app.UseRouting(); //configure routes app.MapGrpcService(); //run the server app.Run(); // app.RunAsync(); //use this if you need to implement background processing in the main thread } }