DnsServerApp: main method updated to work correctly with console and as a background daemon.

This commit is contained in:
Shreyas Zare
2018-12-30 16:46:42 +05:30
parent 38c15ee87c
commit ea23f78a09
2 changed files with 82 additions and 32 deletions

View File

@@ -19,6 +19,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
using DnsServerCore;
using System;
using System.Threading;
namespace DnsServerApp
{
@@ -31,14 +32,26 @@ namespace DnsServerApp
if (args.Length == 1)
configFolder = args[0];
DnsWebService service = new DnsWebService(configFolder, new Uri("https://technitium.com/download/dns/updateca2.bin"));
DnsWebService service = null;
try
{
service = new DnsWebService(configFolder, new Uri("https://technitium.com/download/dns/updateca2.bin"));
service.Start();
Console.WriteLine("Technitium DNS Server was started successfully.");
Console.WriteLine("Using config folder: " + service.ConfigFolder);
Console.WriteLine("");
Console.WriteLine("Note: Open http://localhost:" + service.WebServicePort + "/ in web browser to access web console.");
Console.WriteLine("");
if (Console.IsInputRedirected)
{
//app running as background service
Thread.Sleep(Timeout.Infinite);
}
else
{
Console.WriteLine("Press [CTRL + X] to stop...");
while (true)
@@ -48,9 +61,21 @@ namespace DnsServerApp
if (key.Modifiers == ConsoleModifiers.Control && key.Key == ConsoleKey.X)
break;
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
finally
{
if (service != null)
{
Console.WriteLine("Technitium DNS Server is stopping...");
service.Dispose();
Console.WriteLine("Technitium DNS Server was stopped successfully.");
}
}
}
}
}

View File

@@ -19,6 +19,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
using DnsServerCore;
using System;
using System.Threading;
namespace DnsServerApp
{
@@ -31,14 +32,26 @@ namespace DnsServerApp
if (args.Length == 1)
configFolder = args[0];
DnsWebService service = new DnsWebService(configFolder, new Uri("https://technitium.com/download/dns/updatewa2.bin"));
DnsWebService service = null;
try
{
service = new DnsWebService(configFolder, new Uri("https://technitium.com/download/dns/updatewa2.bin"));
service.Start();
Console.WriteLine("Technitium DNS Server was started successfully.");
Console.WriteLine("Using config folder: " + service.ConfigFolder);
Console.WriteLine("");
Console.WriteLine("Note: Open http://localhost:" + service.WebServicePort + "/ in web browser to access web console.");
Console.WriteLine("");
if (Console.IsInputRedirected)
{
//app running as background service
Thread.Sleep(Timeout.Infinite);
}
else
{
Console.WriteLine("Press [CTRL + X] to stop...");
while (true)
@@ -48,9 +61,21 @@ namespace DnsServerApp
if (key.Modifiers == ConsoleModifiers.Control && key.Key == ConsoleKey.X)
break;
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
finally
{
if (service != null)
{
Console.WriteLine("Technitium DNS Server is stopping...");
service.Dispose();
Console.WriteLine("Technitium DNS Server was stopped successfully.");
}
}
}
}
}