/// <summary> /// 初始化HTTP服务器的实例 /// </summary> /// <param name="serverName">Name of the server.</param> /// <param name="factory">Factory used to create objects used in this library.</param> public Server(string serverName, HttpFactory factory) { _server = this; _factory = factory; ServerName = serverName; AuthenticationProvider = new AuthenticationProvider(); MaxContentSize = 1000000; }
static void Main(string[] args) { LogFactory.Assign(new ConsoleLogFactory()); ServiceProvider.Bootstrap(); ServiceHostInfo info = new ServiceHostInfo(); info.Name = ConfigurationMaster.Get(ServiceConfiguration.DefaultServiceConfigurationName); info.Address = Dns.GetHostName(); info.Port = int.Parse(ConfigurationMaster.Get(ServiceConfiguration.DefaultServicePortConfigurationName)); info.Binding = new NetTcpBinding(ServiceConfiguration.DefaultNetTcpBindingName); info.Contract = typeof(IDeviceProfileService); info.Service = typeof(DeviceProfileService); Console.WriteLine(string.Format(@"Service is starting on [{0}]", info.ToString())); ManagedServiceHostActivator<IDeviceProfileService> activator = new ManagedServiceHostActivator<IDeviceProfileService>(info); activator.Start(); Console.WriteLine(string.Format(@"Service address [{0}]", activator.ServiceHost.Description.Endpoints.First().Address)); string serverName = ConfigurationMaster.Get(@"HttpServerName"); string httpServerName = serverName + " HTTP Server"; int httpBindingPort = int.Parse(ConfigurationMaster.Get(@"HttpServerPort")); Server server = null; server = new Server(httpServerName); server.Add(HttpListenerFactory.Create(IPAddress.Any, httpBindingPort)); server.Add(new CameraListModule()); server.Add(new CameraModule()); server.Add(new CameraThumbnailModule()); FileModule fileModule = new FileModule(); EmbeddedResourceLoader embedded = new EmbeddedResourceLoader(); embedded.Add("/", Assembly.GetExecutingAssembly(), Assembly.GetExecutingAssembly().GetName().Name, Assembly.GetExecutingAssembly().GetName().Name + @".Resources.favicon.ico"); fileModule.Resources.Add(embedded); server.Add(fileModule); server.Add(new SimpleRouter("/favicon.ico", "/resources/favicon.ico")); server.Start(5); Console.WriteLine(string.Format("Start {0} on {1}.", httpServerName, httpBindingPort)); Console.WriteLine(); Console.WriteLine("Press any key to close service."); Console.ReadKey(); server.Stop(true); activator.Stop(); }
static void Main(string[] args) { string serverName = "Dennis Gao"; string httpServerName = serverName + " HTTP Server"; int httpBindingPort = 8000; Server server = null; server = new Server(httpServerName); server.Add(HttpListenerFactory.Create(IPAddress.Any, httpBindingPort)); FileModule fileModule = new FileModule(); server.Add(fileModule); EmbeddedResourceLoader embedded = new EmbeddedResourceLoader(); fileModule.Resources.Add(embedded); embedded.Add("/", Assembly.GetExecutingAssembly(), Assembly.GetExecutingAssembly().GetName().Name, Assembly.GetExecutingAssembly().GetName().Name + @".img.favicon.ico"); embedded.Add("/", Assembly.GetExecutingAssembly(), Assembly.GetExecutingAssembly().GetName().Name, Assembly.GetExecutingAssembly().GetName().Name + @".img.camera.jpg"); embedded.Add("/", Assembly.GetExecutingAssembly(), Assembly.GetExecutingAssembly().GetName().Name, Assembly.GetExecutingAssembly().GetName().Name + @".js.jquery-2-0-3.js"); embedded.Add("/", Assembly.GetExecutingAssembly(), Assembly.GetExecutingAssembly().GetName().Name, Assembly.GetExecutingAssembly().GetName().Name + @".js.jquery-mobile-1-3-2.js"); embedded.Add("/", Assembly.GetExecutingAssembly(), Assembly.GetExecutingAssembly().GetName().Name, Assembly.GetExecutingAssembly().GetName().Name + @".css.jquery-mobile-1-3-2.css"); embedded.Add("/", Assembly.GetExecutingAssembly(), Assembly.GetExecutingAssembly().GetName().Name, Assembly.GetExecutingAssembly().GetName().Name + @".html.login.html"); server.Add(new HomeModule()); server.Add(new SimpleRouter("/", "/html/login.html")); server.Add(new SimpleRouter("/favicon.ico", "/img/favicon.ico")); server.Start(5); Console.WriteLine(string.Format("Start {0} on {1}.", httpServerName, httpBindingPort)); Console.WriteLine(); Console.WriteLine("Press any key to close service."); Console.ReadKey(); server.Stop(true); }
private void OnListenerRequestReceived(object sender, RequestEventArgs e) { _server = this; Exception exception; try { ProcessingResult result = HandleRequest(e); if (result != ProcessingResult.Continue) return; exception = null; } catch (HttpException err) { Logger.Error("Got an HTTP exception."); ExceptionHandler.Handle(err); e.Response.Status = err.Code; e.Response.Reason = err.Message; exception = err; } catch (Exception err) { Logger.Error("Got an unhandled exception."); ExceptionHandler.Handle(err); exception = err; e.Response.Status = HttpStatusCode.InternalServerError; e.Response.Reason = "Failed to process request."; } if (exception == null) { e.Response.Status = HttpStatusCode.NotFound; e.Response.Reason = "Requested resource is not found."; exception = new HttpException(HttpStatusCode.NotFound, "Failed to find uri " + e.Request.Uri); } DisplayErrorPage(e.Context, exception); e.IsHandled = true; }
private void OnListenerErrorPageRequested(object sender, ErrorPageEventArgs e) { _server = this; DisplayErrorPage(e.Context, e.Exception); e.IsHandled = true; }