Skip to content

Instantly share code, notes, and snippets.

@joeenzminger
Created February 8, 2012 16:00
Show Gist options
  • Save joeenzminger/1770645 to your computer and use it in GitHub Desktop.
Save joeenzminger/1770645 to your computer and use it in GitHub Desktop.
HttpListener Basic Example
private void _ListenerProc()
{
if (_Listener != null)
return; //Start shouldn't have been called
try
{
_Listener = new HttpListener();
_Listener.Prefixes.Add("http://*:" + _Port.ToString() + VirtualDirectory);
_Listener.Prefixes.Add("https://*:" + _SecurePort.ToString() + VirtualDirectory);
_Listener.AuthenticationSchemeSelectorDelegate = new AuthenticationSchemeSelector(_GetAuthenticationScheme);
//_Listener.AuthenticationSchemes = AuthenticationSchemes.Anonymous;
_Listener.IgnoreWriteExceptions = true;
_Listener.UnsafeConnectionNtlmAuthentication = true;
_Listener.Start();
ProcessContext process = new ProcessContext(_ProcessContext);
while ((Thread.CurrentThread.ThreadState & System.Threading.ThreadState.AbortRequested) == 0)
{
HttpListenerContext Context = _Listener.GetContext();
Interlocked.Increment(ref _PendingCallCount);
Log.LogInfo("Processing new HttpListenerContext");
AsyncHelper.FireAndForget(process, new object[] { Context });
}
}
catch (ThreadAbortException)
{
}
//catch (Exception e)
//{
// Console.WriteLine("Caught Exception opening Listener");
// Console.WriteLine(e.Message);
//}
finally
{
if (_Listener != null)
_Listener.Stop();
_Listener.Close();
_Listener = null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment