/// <summary> /// 增加程序集内的所有文件资源 /// </summary> /// <param name="assembly"></param> /// <param name="fileModule"></param> private void AddFileResources(Assembly assembly, FileModule fileModule) { var assemblyPath = Path.GetDirectoryName(assembly.Location); var filePath = Path.Combine(assemblyPath, "Public"); if (Directory.Exists(filePath)) fileModule.Resources.Add(new Resources.FileResources("/content/", filePath)); }
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(); }
/// <summary> /// 简单HTTP服务器 /// </summary> public SimpleServer(string serverName) : base(serverName) { // 增加消息体解析器 Add(new MultiPartDecoder()); Add(new UrlDecoder()); // 增加文件模块 var fileModule = new FileModule(); fileModule.AddDefaultMimeTypes(); AddEmbeddedResources(Assembly.GetCallingAssembly(), fileModule); AddFileResources(Assembly.GetCallingAssembly(), fileModule); }
/// <summary> /// 添加程序集内的所有嵌入的资源 /// </summary> /// <param name="assembly"></param> /// <param name="fileModule"></param> private void AddEmbeddedResources(Assembly assembly, FileModule fileModule) { string contentNamespace = null; foreach (var resourceName in assembly.GetManifestResourceNames()) { if (!resourceName.Contains("Content")) continue; contentNamespace = resourceName; break; } if (contentNamespace == null) return; int pos = contentNamespace.IndexOf("Content"); contentNamespace = contentNamespace.Substring(0, pos); fileModule.Resources.Add(new Resources.EmbeddedResourceLoader( "/content/", Assembly.GetCallingAssembly(), contentNamespace)); }
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); }