Skip to content

Instantly share code, notes, and snippets.

@bitsprint
Last active December 15, 2015 06:08
Show Gist options
  • Select an option

  • Save bitsprint/5213589 to your computer and use it in GitHub Desktop.

Select an option

Save bitsprint/5213589 to your computer and use it in GitHub Desktop.
server.csx
#load "ApiControllerWithHub.csx"
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.SelfHost;
using System.Web.Http.Dispatcher;
using Newtonsoft.Json;
using Microsoft.AspNet.SignalR;
using Microsoft.AspNet.SignalR.Hubs;
/*
public abstract class ApiControllerWithHub<THub> : ApiController where THub : IHub
{
Lazy<IHubContext> hub = new Lazy<IHubContext>(
() => GlobalHost.ConnectionManager.GetHubContext<THub>()
);
protected IHubContext Hub
{
get { return hub.Value; }
}
}
public class ToDoHub : Hub { }
*/
public class ToDoHub : Hub { }
public class ToDoController : ApiControllerWithHub<ToDoHub> {
}
public class TestController : System.Web.Http.ApiController {
public HttpResponseMessage Get(HttpRequestMessage request) {
var message = JsonConvert.SerializeObject(new { message = "Hello world!" });
return request.CreateResponse(HttpStatusCode.OK, message);
}
}
public class ControllerResolver : DefaultHttpControllerTypeResolver {
public override ICollection<Type> GetControllerTypes(IAssembliesResolver assembliesResolver) {
var types = Assembly.GetExecutingAssembly().GetTypes();
return types.Where(x => typeof(System.Web.Http.Controllers.IHttpController).IsAssignableFrom(x)).ToList();
}
}
var address = "http://localhost:8080";
var conf = new HttpSelfHostConfiguration(new Uri(address));
conf.Services.Replace(typeof(IHttpControllerTypeResolver), new ControllerResolver());
var appXmlType = conf.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(t => t.MediaType == "application/xml");
conf.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType);
conf.Routes.MapHttpRoute(name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
var server = new HttpSelfHostServer(conf);
server.OpenAsync().Wait();
Console.WriteLine("Listening...");
Console.ReadKey();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment