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 System.Web.Http; | |
using Owin; | |
namespace AspNetSelfHostDemo | |
{ | |
public class Startup | |
{ | |
public void Configuration(IAppBuilder app) | |
{ | |
// Configure Web API for self-host. |
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 System; | |
using Microsoft.Owin.Hosting; | |
namespace AspNetSelfHostDemo | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
using (WebApp.Start<Startup>("http://localhost:8080")) |
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 System.Collections.Generic; | |
using System.Web.Http; | |
namespace AspNetSelfHostDemo | |
{ | |
public class DemoController : ApiController | |
{ | |
// GET api/demo | |
public IEnumerable<string> Get() | |
{ |
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
static class Program | |
{ | |
/// <summary> | |
/// The main entry point for the application. | |
/// </summary> | |
static void Main() | |
{ | |
ServiceBase[] ServicesToRun; | |
ServicesToRun = new ServiceBase[] | |
{ |
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
public partial class SelfHostServiceBase : ServiceBase | |
{ | |
private IDisposable _webapp; | |
public SelfHostServiceBase() | |
{ | |
InitializeComponent(); | |
} | |
protected override void OnStart(string[] args) |
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
HostFactory.Run(x => | |
{ | |
x.Service<TopshelfService>(s => | |
{ | |
s.ConstructUsing(name => new TopshelfService()); | |
s.WhenStarted(tc => tc.Start()); | |
s.WhenStopped(tc => tc.Stop()); | |
}); | |
x.RunAsLocalSystem(); |
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
public class Startup | |
{ | |
public void Configuration(IAppBuilder app) | |
{ | |
// Configure Web API for self-host. | |
var config = new HttpConfiguration(); | |
config.Routes.MapHttpRoute( | |
name: "DefaultApi", | |
routeTemplate: "api/{controller}/{id}", | |
defaults: new { id = RouteParameter.Optional } |
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
public class CustomContentTypeProvider : FileExtensionContentTypeProvider | |
{ | |
public CustomContentTypeProvider() | |
{ | |
Mappings.Add(".json", "application/json"); | |
} | |
} |
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
var options = new FileServerOptions | |
{ | |
EnableDirectoryBrowsing = true, | |
EnableDefaultFiles = true, | |
DefaultFilesOptions = { DefaultFileNames = {"index.html"}}, | |
FileSystem = new PhysicalFileSystem("Assets"), | |
StaticFileOptions = { ContentTypeProvider = new CustomContentTypeProvider() } | |
}; | |
app.UseFileServer(options); |
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
app.Use(async (context, next) => | |
{ | |
// Add Header | |
context.Response.Headers["Product"] = "Web Api Self Host"; | |
// Call next middleware | |
await next.Invoke(); | |
}); |