1

I am working through Scott Allen's MVC 5 Fundamentals course on Pluralsight

The following code should work , but when I browse to localhost:8080 I get a blank page

using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Microsoft.Owin.Hosting;
using Owin;
namespace ConsoleApplication1
{
    using AppFunc = Func<IDictionary<string, object>, Task>;
    class Program
    {
        static void Main(string[] args)
        {
            string uri = "http://localhost:8080";
            using (WebApp.Start<Startup>(uri))   // Katana Please start, using the configuration from the Startup class and listening on the port given by the uri
            {
                Console.WriteLine("Started!");
                Console.ReadKey();
                Console.WriteLine("Stopping!");
            }
        }
    }

    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            app.Use<HelloWorldComponent>();
        }
    }

    public class HelloWorldComponent
    {
        AppFunc _next;
        public HelloWorldComponent(AppFunc next)
        {
            _next = next;
        }

        // Katana uses reflection to find this Invoke function that matches the AppFunc signature
        public Task Invoke(IDictionary<string, object> environment)
        { 
            var response = environment["owin.ResonseBody"] as Stream;

            using (var writer = new StreamWriter(response))
            {
                return writer.WriteAsync("Hello");
            }
        }
    }
}

How do I get it working?

icedwater
  • 4,701
  • 3
  • 35
  • 50
Kirsten
  • 15,730
  • 41
  • 179
  • 318
  • I see that the Invoke Method does not actually match the AppFunc signature because it does not include Task – Kirsten Apr 19 '15 at 00:48
  • IAppBuilder is deprecated in Owin... but Katana is built on it. http://jamesmckay.net/2014/08/sorting-out-the-confusion-that-is-owin-katana-and-iappbuilder/ – Kirsten Apr 19 '15 at 00:55

1 Answers1

2

Everything is setup correctly, there is just a small typo in fetching the response object:

 var response = environment["owin.ResponseBody"] as Stream;

 using (var writer = new StreamWriter(response))
 {
    return writer.WriteAsync("Hello");
 }

Notice the "p" in "ResponseBody"!

OdeToCode
  • 4,906
  • 23
  • 18
  • I have a new problem at http://stackoverflow.com/questions/29803698/an-unhandled-exception-of-type-system-argumentexception-occurred-in-microsoft – Kirsten Apr 22 '15 at 16:33