Skip to content

Instantly share code, notes, and snippets.

View dcomartin's full-sized avatar

Derek Comartin dcomartin

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