Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            try
            {
                string   port       = "8888";
                string[] parameters = Environment.GetCommandLineArgs();

                if (parameters.Length > 1)
                {
                    port = parameters[1];
                }

                if (parameters.Length > 2)
                {
                    FLogFile = parameters[2];
                }

                if (parameters.Length > 3)
                {
                    FMaxRunTime = TimeSpan.FromMinutes(Convert.ToInt32(parameters[3]));
                }

                string physicalDir = Directory.GetCurrentDirectory();

                if (!(physicalDir.EndsWith(Path.DirectorySeparatorChar.ToString())))
                {
                    physicalDir = physicalDir + Path.DirectorySeparatorChar;
                }

                // Copy this hosting DLL into the /bin directory of the application
                string FileName = Assembly.GetExecutingAssembly().Location;

                try
                {
                    if (!Directory.Exists(physicalDir + "bin" + Path.DirectorySeparatorChar))
                    {
                        Directory.CreateDirectory(physicalDir + "bin" + Path.DirectorySeparatorChar);
                    }

                    File.Copy(FileName, physicalDir + "bin" + Path.DirectorySeparatorChar + Path.GetFileName(FileName), true);
                }
                catch
                {
                    ;
                }

                Fthlw = (ThreadedHttpListenerWrapper)ApplicationHost.CreateApplicationHost(
                    typeof(ThreadedHttpListenerWrapper), "/", physicalDir);

                Log("trying to listen on port " + port);
                string[] prefixes = new string[] {
                    "http://+:" + port + "/"
                };

                Fthlw.Configure(prefixes, "/", Directory.GetCurrentDirectory());

                try
                {
                    Fthlw.Start();
                }
                catch (HttpListenerException)
                {
                    Console.WriteLine();
                    Console.WriteLine();
                    Log("we cannot listen on this port. perhaps you need to run as administrator once: ");
                    Log(
                        "  netsh http add urlacl url=http://+:" + port + "/ user="******"\\" + Environment.UserName);
                    Console.WriteLine();
                    Console.WriteLine();
                    Console.WriteLine();

                    throw;
                }

                string Message = "Listening for requests on http://127.0.0.1:" + port + "/";

                if (FMaxRunTime.HasValue)
                {
                    Message += " for a maximum run time: " + FMaxRunTime.Value.ToString();
                }

                Log(Message);

                if (FMaxRunTime.HasValue)
                {
                    (new Thread(() => ThreadCheckMaxRunTime())).Start();
                }

                while (!FStopServer)
                {
                    try
                    {
                        Fthlw.ProcessRequest();
                    }
                    catch (AppDomainUnloadedException e)
                    {
                        Log(e.Message);
                        break;
                    }
                }
            }
            catch (Exception e)
            {
                Log(e.ToString());
            }
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            try
            {
                string port = "8888";
                string[] parameters = Environment.GetCommandLineArgs();

                if (parameters.Length > 1)
                {
                    port = parameters[1];
                }

                if (parameters.Length > 2)
                {
                    FLogFile = parameters[2];
                }

                if (parameters.Length > 3)
                {
                    FMaxRunTime = TimeSpan.FromMinutes(Convert.ToInt32(parameters[3]));
                }

                string physicalDir = Directory.GetCurrentDirectory();

                if (!(physicalDir.EndsWith(Path.DirectorySeparatorChar.ToString())))
                {
                    physicalDir = physicalDir + Path.DirectorySeparatorChar;
                }

                // Copy this hosting DLL into the /bin directory of the application
                string FileName = Assembly.GetExecutingAssembly().Location;

                try
                {
                    if (!Directory.Exists(physicalDir + "bin" + Path.DirectorySeparatorChar))
                    {
                        Directory.CreateDirectory(physicalDir + "bin" + Path.DirectorySeparatorChar);
                    }

                    File.Copy(FileName, physicalDir + "bin" + Path.DirectorySeparatorChar + Path.GetFileName(FileName), true);
                }
                catch
                {
                    ;
                }

                Fthlw = (ThreadedHttpListenerWrapper)ApplicationHost.CreateApplicationHost(
                    typeof(ThreadedHttpListenerWrapper), "/", physicalDir);

                Log("trying to listen on port " + port);
                string[] prefixes = new string[] {
                    "http://+:" + port + "/"
                };

                Fthlw.Configure(prefixes, "/", Directory.GetCurrentDirectory());

                try
                {
                    Fthlw.Start();
                }
                catch (HttpListenerException)
                {
                    Console.WriteLine();
                    Console.WriteLine();
                    Log("we cannot listen on this port. perhaps you need to run as administrator once: ");
                    Log(
                        "  netsh http add urlacl url=http://+:" + port + "/ user="******"\\" + Environment.UserName);
                    Console.WriteLine();
                    Console.WriteLine();
                    Console.WriteLine();

                    throw;
                }

                string Message = "Listening for requests on http://127.0.0.1:" + port + "/";

                if (FMaxRunTime.HasValue)
                {
                    Message += " for a maximum run time: " + FMaxRunTime.Value.ToString();
                }

                Log(Message);

                if (FMaxRunTime.HasValue)
                {
                    (new Thread(() => ThreadCheckMaxRunTime())).Start();
                }

                while (!FStopServer)
                {
                    try
                    {
                        Fthlw.ProcessRequest();
                    }
                    catch (AppDomainUnloadedException e)
                    {
                        Log(e.Message);
                        break;
                    }
                }
            }
            catch (Exception e)
            {
                Log(e.ToString());
            }
        }