Skip to content

Instantly share code, notes, and snippets.

@igloo15
Created November 7, 2016 15:59
Show Gist options
  • Save igloo15/9cd99b48b17d3c8c962b92d41c77eb39 to your computer and use it in GitHub Desktop.
Save igloo15/9cd99b48b17d3c8c962b92d41c77eb39 to your computer and use it in GitHub Desktop.
Server class for kestrel web server
using Microsoft.AspNetCore.Hosting;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
namespace WebServer.Main
{
public class Server
{
private WebHostBuilder _serverBuilder = null;
private IWebHost _server = null;
public Server(string path, string port = "1515", string webroot = "")
{
if (webroot == null)
throw new ArgumentNullException("webroot");
if (String.IsNullOrEmpty(path))
path = Directory.GetCurrentDirectory();
if (String.IsNullOrEmpty(port))
port = "5000";
_serverBuilder = new WebHostBuilder();
_server = _serverBuilder
.UseKestrel()
.UseContentRoot(path)
.UseWebRoot(webroot)
.UseStartup<Startup>()
.UseUrls($"http://localhost:{port}")
.Build();
}
public void Start()
{
_server.Run();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment