HttpListenerHost tutorial?

152 views
Skip to first unread message

innes

unread,
Nov 18, 2009, 6:33:28 AM11/18/09
to OpenRasta
Hi, Does anyone have, or could they rustle up, a total beginner-level
piece of example code for using HttpListenerHost? Maybe even just a
console app that responds to requests to a single url - nothing
dynamic. I have had a go, but got hit by a 'pipeline not initialized'
exception whenever making any http request to the listener. I expect
that most people use OR in IIS, hence the lack of an example. Any help
is appreciated, thanks!

innes

unread,
Nov 18, 2009, 9:45:12 AM11/18/09
to OpenRasta
For more background, here's my test program, followed by the exception
that gets thrown when I try and GET http://localhost:9222/test.txt:

-----------------------------------------------
using System;
using OpenRasta.DI;
using OpenRasta.Hosting.HttpListener;
using OpenRasta.Configuration;

namespace ORConsole
{
public class TestHandler
{
public string Get() { return "hello world"; }
}

public class Configurator : IConfigurationSource
{
public void Configure()
{
ResourceSpace.Has
.ResourcesOfType<string>()
.AtUri("test.txt")
.HandledBy<TestHandler>()
.TranscodedBy<OpenRasta.Codecs.TextPlainCodec>();
}
}

public class HttpListenerHostWithConfiguration : HttpListenerHost
{
public IConfigurationSource Configuration { get; set; }

public override bool ConfigureRootDependencies(IDependencyResolver
resolver)
{
var result = base.ConfigureRootDependencies(resolver);
if (result && Configuration != null)
resolver.AddDependencyInstance<IConfigurationSource>
(Configuration);
return result;
}
}

public class Program
{
private static void Main()
{
var h = new HttpListenerHostWithConfiguration
{Configuration = new Configurator()};

h.Initialize(new[]{"http://+:9222/"}, "/", null);
h.StartListening();

Console.WriteLine("Press enter to exit.");
Console.ReadLine();
}
}
}
-----------------------------------------------

-----------------------------------------------
System.InvalidOperationException was unhandled
Message="The pipeline has not been initialized and cannot run."
Source="OpenRasta"
StackTrace:
at OpenRasta.Pipeline.PipelineRunner.CheckPipelineIsInitialized
() in C:\temp\ortrunk\src\core\OpenRasta\Pipeline
\PipelineRunner.cs:line 54
at OpenRasta.Pipeline.PipelineRunner.Run(ICommunicationContext
context) in C:\temp\ortrunk\src\core\OpenRasta\Pipeline
\PipelineRunner.cs:line 90
at
OpenRasta.Hosting.HostManager.<>c__DisplayClass4.<HandleHostIncomingRequestReceived>b__3
() in C:\temp\ortrunk\src\core\OpenRasta\Hosting\HostManager.cs:line
194
at OpenRasta.Hosting.HostManager.ThreadScopedAction(Action
action) in C:\temp\ortrunk\src\core\OpenRasta\Hosting
\HostManager.cs:line 160
at
OpenRasta.Hosting.HostManager.HandleHostIncomingRequestReceived(Object
sender, IncomingRequestEventArgs e) in C:\temp\ortrunk\src\core
\OpenRasta\Hosting\HostManager.cs:line 187
at System.EventHandler`1.Invoke(Object sender, TEventArgs e)
at
OpenRasta.Hosting.HttpListener.HttpListenerHost.ProcessRequest
(IAsyncResult result) in C:\temp\ortrunk\src\core\OpenRasta\Hosting
\HttpListener\HttpListenerHost.cs:line 84
at System.Net.LazyAsyncResult.Complete(IntPtr userToken)
at System.Net.LazyAsyncResult.ProtectedInvokeCallback(Object
result, IntPtr userToken)
at System.Net.ListenerAsyncResult.WaitCallback(UInt32
errorCode, UInt32 numBytes, NativeOverlapped* nativeOverlapped)
at
System.Threading._IOCompletionCallback.IOCompletionCallback_Context
(Object state)
at System.Threading.ExecutionContext.runTryCode(Object
userData)
at
System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup
(TryCode code, CleanupCode backoutCode, Object userData)
at System.Threading.ExecutionContext.RunInternal
(ExecutionContext executionContext, ContextCallback callback, Object
state)
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state)
at
System.Threading._IOCompletionCallback.PerformIOCompletionCallback
(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP)
InnerException:
-----------------------------------------------

Sebastien Lambla

unread,
Nov 18, 2009, 11:13:15 AM11/18/09
to open...@googlegroups.com
You haven't added the using (OpenRastaConfiguration.Manual) {} in your
configuration code

innes

unread,
Nov 19, 2009, 6:12:45 AM11/19/09
to OpenRasta
Thanks.

On Nov 18, 4:13 pm, "Sebastien Lambla" <s...@serialseb.com> wrote:
> You haven't added the using (OpenRastaConfiguration.Manual) {} in your
> configuration code
>
> -----Original Message-----
> From: innes [mailto:inn...@gmail.com]
> Sent: 18 November 2009 14:45
> To: OpenRasta
> Subject: [openrasta] Re: HttpListenerHost tutorial?
>
> For more background, here's my test program, followed by the exception
> that gets thrown when I try and GEThttp://localhost:9222/test.txt:
Reply all
Reply to author
Forward
0 new messages