static void Main() { try { AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException); string cwd = System.IO.Path.GetDirectoryName(Environment.GetCommandLineArgs()[0]); System.IO.Directory.SetCurrentDirectory(cwd ?? "."); DotLiquid.Hash h = new DotLiquid.Hash(); Nancy.ViewEngines.DotLiquid.DotLiquidRegistrations r = new Nancy.ViewEngines.DotLiquid.DotLiquidRegistrations(); try { var log4net_config = new System.IO.FileInfo("log4net.xml"); log4net.Config.XmlConfigurator.ConfigureAndWatch(log4net_config); BauerLib.log4netAdoProviderHelper.ConfigureConnectionStringToAllLoggers(); // BauerLib.MemoryAppenderWithEvents.AddAppenderToAllLoggersEvent(HandleLogEvent); log4net.LogManager.GetRepository().ConfigurationChanged += (s, e) => { try { BauerLib.log4netAdoProviderHelper.ConfigureConnectionStringToAllLoggers(); // BauerLib.MemoryAppenderWithEvents.AddAppenderToAllLoggersEvent(HandleLogEvent); } catch { } }; } catch { } log.InfoFormat("{0} started.", Environment.GetCommandLineArgs()[0]); } catch (Exception) { } HostFactory.Run(x => { x.UseLog4Net(); x.RunAsLocalSystem(); x.SetDescription("Bauer Logistics REST bridge"); x.SetDisplayName("WIP REST Service"); x.SetServiceName("WIP REST Service"); x.Service <BauerOrderRestService>(s => { s.ConstructUsing(name => new BauerOrderRestService()); s.WhenStarted(tc => { tc.Start(); }); s.WhenStopped(tc => { tc.Stop(); }); }); }); }
public void RunWebService() { int port = 50006; int.TryParse(BauerLib.Registry.ServicePort, out port); if (PortInUse(port)) { log.ErrorFormat("Error {0} in use !", port); return; } string baseAddress = string.Format("http://localhost:{0}", port); Nancy.ViewEngines.DotLiquid.DotLiquidRegistrations r = new Nancy.ViewEngines.DotLiquid.DotLiquidRegistrations(); DotLiquid.Template.NamingConvention = new DotLiquid.NamingConventions.CSharpNamingConvention(); Nancy.Hosting.Self.HostConfiguration hostConfigs = new Nancy.Hosting.Self.HostConfiguration(); // netsh http add urlacl url=http://+:50006/ user=EVERYONE // netsh http add urlacl url=http://+:50006/ user=JEDER hostConfigs.UrlReservations.CreateAutomatically = false; hostConfigs.RewriteLocalhost = true; int retries = 6; while (retries > 0) { try { host = new Nancy.Hosting.Self.NancyHost(hostConfigs, new Uri(baseAddress)); host.Start(); retries = -1; } catch (Exception ex) { log.Error("Error starting rest-service", ex); retries--; if (retries <= 3) { hostConfigs.UrlReservations.CreateAutomatically = true; } System.Threading.Thread.Sleep(500); } } log.InfoFormat("Running on {0}", baseAddress); if (Environment.UserInteractive) { System.Diagnostics.Process.Start(baseAddress); } }