VPathToHost GetOrCreateApplication(string vhost, int port, string filepath, string virt) { VPathToHost vapp = null; string vdir = Path.GetDirectoryName(virt); string pdir = Path.GetDirectoryName(filepath); DirectoryInfo vinfo = new DirectoryInfo(vdir); DirectoryInfo pinfo = new DirectoryInfo(pdir); string final_pdir = null; string final_vdir = null; while (vinfo != null && pinfo != null) { if (final_pdir == null && CheckDirectory(pinfo)) { final_pdir = pinfo.ToString(); final_vdir = vinfo.ToString(); break; } if (pinfo.Name != vinfo.Name) { final_vdir = vinfo.ToString(); break; } pinfo = pinfo.Parent; vinfo = vinfo.Parent; } if (final_pdir == null) { final_pdir = pinfo.ToString(); } if (final_vdir == null) { final_vdir = vinfo.ToString(); } //Console.Error.WriteLine ("final_pdir: {0} final_vdir: {1}", final_pdir, final_vdir); vapp = server.GetApplicationForPath(vhost, port, virt, false); if (vapp == null) { // Don't know why this breaks mod-mono-server2.exe, but not mod-mono-server.exe //final_pdir = "file://" + final_pdir; if (final_vdir [0] != '/') { final_vdir = "/" + final_vdir; } server.AddApplication(vhost, port, final_vdir, final_pdir); vapp = server.GetApplicationForPath(vhost, port, virt, false); } return(vapp); }
public static void Start() { XSPWebSource websource=new XSPWebSource(IPAddress.Loopback,0); WebAppServer=new ApplicationServer(websource); string basePath = Directory.GetParent(Assembly.GetExecutingAssembly().Location).ToString(); string serverPath = basePath; if(serverPath[serverPath.Length - 1] != System.IO.Path.DirectorySeparatorChar) serverPath += System.IO.Path.DirectorySeparatorChar; serverPath += "WebUI"; string serverBinPath = serverPath + System.IO.Path.DirectorySeparatorChar + "bin" + System.IO.Path.DirectorySeparatorChar; WebAppServer.AddApplication("",-1,"/", serverPath); bool started = false; DateTime curr = DateTime.Now; while(!started && curr.AddMinutes(1) > DateTime.Now) { try { WebAppServer.Start(true); started = true; } catch (System.Net.Sockets.SocketException e) { if(e.ErrorCode == 10049 || e.ErrorCode == 10022) { //strange error on bind, probably network still not started //try to rerun server System.Threading.Thread.Sleep(10000); //WebAppServer.Start(true); } else throw; } } if(!started) WebAppServer.Start(true); //copy Mono.WebServer2.dll /*try { if (!Directory.Exists(serverBinPath)) Directory.CreateDirectory(serverBinPath); File.Copy(basePath + System.IO.Path.DirectorySeparatorChar+"Mono.WebServer2.dll", serverBinPath + "Mono.WebServer2.dll", true); } catch { ;} */ AppDomain ap = WebAppServer.GetApplicationForPath("", WebAppServer.Port, "/", false).AppHost.Domain; ap.AssemblyResolve += new ResolveEventHandler(MyResolveEventHandler); ap.UnhandledException += OnUnhandledExceptionEvent; ap.SetData("WebUIGate", webServerGate); port = WebAppServer.Port; //uri = new Uri("http://127.0.0.1:" + port.ToString() + "/"); uri = new Uri("http://localhost:" + port.ToString() + "/"); Console.WriteLine("Webserver started at " + uri.ToString()); }