咱們知道,藉助OWIN,WebApi,SignalR,WCF 均可以建立自託管(Self-Host)實例,徹底不須要IIS,靜態網站也能夠。javascript
最近作一個服務器監控小工具,用 SignalR 通訊,監控端是一個靜態網站(掛在IIS),服務端是一個 WinForm 程序(SignalR 服務寄宿),可是我想網站也寄宿到這個 WinForm 中,這樣經過 WebApp.Start 開啓主機時,網站也開啓了,部署也方便css
爲了減小配置代碼,咱們用一個靜態文件處理的中間件 Beginor.Owin.StaticFilehtml
Install-Package Beginor.Owin.StaticFile
在 OWIN 啓動類中配置java
using Beginor.Owin.StaticFile; [assembly: OwinStartup(typeof(AisinGioro.Server.OwinStartup))] namespace AisinGioro.Server { public class OwinStartup { public void Configuration(IAppBuilder app) { //靜態文件託管 app.Map("/wwwroot", map => { // 容許跨域 map.UseCors(CorsOptions.AllowAll); map.UseStaticFile(new StaticFileMiddlewareOptions { RootDirectory = @"D:\P22\Felix\AisinGioro\AisinGioro.WebPortal", DefaultFile = "index.html", EnableETag = true, EnableHtml5LocationMode = true, MimeTypeProvider = new MimeTypeProvider(new Dictionary<string, string> { { ".html", "text/html" }, { ".htm", "text/html" }, { ".dtd", "text/xml" }, { ".xml", "text/xml" }, { ".ico", "image/x-icon" }, { ".css", "text/css" }, { ".js", "application/javascript" }, { ".json", "application/json" }, { ".jpg", "image/jpeg" }, { ".png", "image/png" }, { ".gif", "image/gif" }, { ".config", "text/xml" }, { ".woff2", "application/font-woff2"}, { ".eot", "application/vnd.ms-fontobject" }, { ".svg", "image/svg+xml" }, { ".woff", "font/x-woff" }, { ".txt", "text/plain" }, { ".map", "text/plain" } }) }); }); //SignalR託管 app.Map("/signalr", map => { // 容許跨域 map.UseCors(CorsOptions.AllowAll); var hubConfiguration = new HubConfiguration { //Resolver = **, // You can enable JSONP by uncommenting line below. // JSONP requests are insecure but some older browsers (and some // versions of IE) require JSONP to work cross domain EnableJSONP = true, EnableDetailedErrors = true }; // Run the SignalR pipeline. We're not using MapSignalR // since this branch is already runs under the "/signalr" // path. map.RunSignalR(hubConfiguration); }); //該值表示鏈接在超時以前保持打開狀態的時間長度。 //默認爲110秒 //GlobalHost.Configuration.ConnectionTimeout = TimeSpan.FromSeconds(110); //該值表示在鏈接中止以後引起斷開鏈接事件以前要等待的時間長度。 //默認爲30秒 GlobalHost.Configuration.DisconnectTimeout = TimeSpan.FromSeconds(60); } } }
其中 RootDirectory 也能夠配置相對目錄,好比 ../app/wwwrootjson
MimeTypeProvider 屬性設置 MIME 映射(Content-Type),若是不設置,Beginor.Owin.StaticFile 只能能判斷普通的類型(好比 html,css,js),因此仍是設置下比較好跨域
建立Web服務監聽,開啓主機服務器
private void DoWork() { try { //建立WEB服務器監聽 using (WebApp.Start<OwinStartup>(_hostUrl)) { if (this.OnStartSucceed != null) this.OnStartSucceed(this, EventArgs.Empty); LogHelper.Trace("Server started! {0}", _hostUrl); while (!cancelTokenSource.IsCancellationRequested) Thread.Sleep(10); } LogHelper.Trace("Server stopped!"); if (this.OnStopSucceed != null) this.OnStopSucceed(this, EventArgs.Empty); //Server stopped! } catch (Exception ex) { ex = ex.GetBaseException(); LogHelper.Error(ex.Message + " " + _hostUrl); if (this.OnFailed != null) this.OnFailed(this, new ErrorOccuredEventArgs(ex)); } }
若是外部程序執行 cancelTokenSource.Cancel,主機服務中止運行app
=========================dom
客戶端鏈接SignalR用這個地址:http://localhost:9000/signalride
而訪問網站,用這個地址:http://localhost:9000/wwwroot/