Last active
December 15, 2015 06:08
-
-
Save bitsprint/5213589 to your computer and use it in GitHub Desktop.
server.csx
This file contains hidden or 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
| #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