Showing posts with label OWIN and Katana. Show all posts
Showing posts with label OWIN and Katana. Show all posts
Saturday, April 18, 2015
How to create your own Katana middleware component - at the metal
//using a C like typedef alias for the Func() inside the class file
using AppFunc = Func<IDictionary<string,object>,Task>();
In the Configuration method of Statup, use my OWIN Katana component
public void Configuration(IAppBuilder app)
{
app.Use<MyOwinComponent>();
}
// My lowest level OWIN component
public class MyOwinComponent
{
//Required to pass processing on to the next OWIN component
AppFunc _next;
public MyOwinComponent(AppFunc next)
{
_next = next;
}
// If not using await, remove the async keyword
//This method should match the AppFunc signature
public async Task Invoke(IDictionary<string,object> environment)
{
var response = environment["owin.ResponseBody"] as Stream;
using( var writer = new StreamWriter(response))
{
return await writer.WriteAsync("Hello from my Owin Component!");
}
//Example await next component and pass along the dictionary
// await _next(environment);
}
}
We can also add syntactic sugar to this so we can specify in configuration method as
app.UseMyOwinComponent();
by creating an extension method like so
public static class AppBuilderExtensions
{
public static void UseMyOwinComponent(this IAppBuilder app)
{
app.Use<MyOwinComponent>();
}
}
******************************************************************
NOTE: Katana provides higher abstractions using app.Run or app.Use with its overloaded functions to write our middleware components, and we should actually use that.
For instance, using just a lambda expression
app.Use( async (environment, next) =>
{
Console.Write("Requesting: " + environment.Request.Path);
await next();
Console.Write("Response: " + environment.Response.StatusCode);
}
app.UseMyOwinComponent();
How to create a Katana WebServer in a console application
Create a new Console app and add these 2 packages:
install-package Microsoft.Owin.Hosting
install-package Microsoft.Owin.Host.HttpListener
Add a class called Startup and a configuration method that takes an IAppBuilder interface. IAppBuilder is in the Owin namespace.
public class Startup
{
public void Configuration(IAppBuilder app)
{
//Add other features from other Katana components through
//the Use extension method. For instance, this below method
// asfter installing the Microsoft.Owin.Diagnostics package
// can be used instead of app.Run() to start a Web server
//with a Welcome Page.
//app.UseWelcomePage();
//Process all requests. Returns a task and is a wrapper to calling AppFunc.
// app.Run can be used to dynamically add your middleware as done here.
app.Run(ctx =>
{
return ctx.Response.WriteAsync("Hello Katana!");
});
}
}
The IAppBuilder interface has methods that allows us to configure and respond to HTTP requests. As we add additional Katana components (nuget packages), we may see additional extension methods that we can use to turn on to use additional Katana features.
Run() takes an OwinContext which has an Environment property, and returns a Task. This property is a Dictionary<string,object> and has all the information about the request - the Http Headers, cookies, the URL path, the Verb that was used etc. This is the core abstraction of OWIN and makes it very simple to add Owin components and middleware as needed in the request processing pipeline. Does away with all the bells and whistles with the conventional ASP.NET and System.Web assembly processing pipeline.
To start the web server on say port 8080, use a Katana component called WebApp to run this Startup class as below:
public static Main(string[] args)
{
string url = "http://localhost:8080";
using (WebApp.Start<Startup>(uri)
{
Console.WriteLine("Web Server started....");
Console.ReadKey();
Console.WriteLine("Web Server ended...");
}
}
Reference: This example has been taken from the Scott Allen Pluralsight course on OWIN.
install-package Microsoft.Owin.Hosting
install-package Microsoft.Owin.Host.HttpListener
Add a class called Startup and a configuration method that takes an IAppBuilder interface. IAppBuilder is in the Owin namespace.
public class Startup
{
public void Configuration(IAppBuilder app)
{
//Add other features from other Katana components through
//the Use extension method. For instance, this below method
// asfter installing the Microsoft.Owin.Diagnostics package
// can be used instead of app.Run() to start a Web server
//with a Welcome Page.
//app.UseWelcomePage();
//Process all requests. Returns a task and is a wrapper to calling AppFunc.
// app.Run can be used to dynamically add your middleware as done here.
app.Run(ctx =>
{
return ctx.Response.WriteAsync("Hello Katana!");
});
}
}
The IAppBuilder interface has methods that allows us to configure and respond to HTTP requests. As we add additional Katana components (nuget packages), we may see additional extension methods that we can use to turn on to use additional Katana features.
Run() takes an OwinContext which has an Environment property, and returns a Task. This property is a Dictionary<string,object> and has all the information about the request - the Http Headers, cookies, the URL path, the Verb that was used etc. This is the core abstraction of OWIN and makes it very simple to add Owin components and middleware as needed in the request processing pipeline. Does away with all the bells and whistles with the conventional ASP.NET and System.Web assembly processing pipeline.
To start the web server on say port 8080, use a Katana component called WebApp to run this Startup class as below:
public static Main(string[] args)
{
string url = "http://localhost:8080";
using (WebApp.Start<Startup>(uri)
{
Console.WriteLine("Web Server started....");
Console.ReadKey();
Console.WriteLine("Web Server ended...");
}
}
Reference: This example has been taken from the Scott Allen Pluralsight course on OWIN.
Katana and OWIN Middleware
Katana is the Microsoft implementation of OWIN that appeared with Visual Studio 2013 (MVC 5).
Prior to Katana, ASP.NET has been built on top of IIS (inetinfo.exe) that provides the hosting and infrastructure features for Web applications. It runs with the SYSTEM account on the Web Server. IIS itself has been around longer than ASP.NET. To make it work with ASP.NET it uses aspnet_isapi.DLL that communicates with an ASP.NET worker process using named pipes.
IIS 7+ uses aspnet_wp.exe which runs with the ASPNET account and which is responsible for hosting the different application domains for ASP.NET.
Over the years ASP.NET and IIS have evolved to provide a wide range of features from authentication to hashing, encryption, compression, caching, configuration, web sockets, logging, diagnostics etc. Even within a specific feature like diagnostics there are multiple methods like logging, request monitoring, tracing et al. NOT EVERYONE needs all these features - and that is the whole idea behind the OWIN middleware framework and Katana.
As HTML5 standards continues to mature, we are seeing a shift in Web Architecture. More and more processing is happening client side rather than on the server. Instead of the Server doing heavy processing and generating all the html, it is responding to only ajax requests that sends out just raw data! This is also true for mobile applications that consume these data with their native apps.The end result is developers are looking for a fast, efficient, lightweight and extensible host for the Web applications and Web Services, particularly with cloud based services where you pay for every byte and CPU cycles.
Like Linkedin does using Node.JS and HTML5 to build better and faster apps. It is easy to add a package to Node and make it run a lean and mean web server using a fraction of traditional resources on server side. It is also easier to scale because it consumes just the resources it needs.
Katana follows this architecture unlike ASP.NET MVC which relies on one giant core System.Web assembly on every project. Instead of one large assembly, Katana features are highly modular and componentized so you can add only the features you need for your project.
Katana allows you to create and plug in your middleware components by being agnostic of whether they are hosted in a console app or in IIS etc. More and more middleware components like Cookie Authentication have been developed for Katana moving away from dependence on ASP.NET MVC modules and handlers and filters etc .
To use and host your Katana custom components in IIS, create the component with the Startup class as a DLL with its build target pointing at the /bin folder in your project or solution. For more information and Katana components, look at the Katana open source project in codeplex.
To learn more about Katana, there's an excellent course by Scott Allen @pluralsight
http://www.pluralsight.com/courses/aspdotnet-mvc5-fundamentals
Prior to Katana, ASP.NET has been built on top of IIS (inetinfo.exe) that provides the hosting and infrastructure features for Web applications. It runs with the SYSTEM account on the Web Server. IIS itself has been around longer than ASP.NET. To make it work with ASP.NET it uses aspnet_isapi.DLL that communicates with an ASP.NET worker process using named pipes.
IIS 7+ uses aspnet_wp.exe which runs with the ASPNET account and which is responsible for hosting the different application domains for ASP.NET.
Over the years ASP.NET and IIS have evolved to provide a wide range of features from authentication to hashing, encryption, compression, caching, configuration, web sockets, logging, diagnostics etc. Even within a specific feature like diagnostics there are multiple methods like logging, request monitoring, tracing et al. NOT EVERYONE needs all these features - and that is the whole idea behind the OWIN middleware framework and Katana.
As HTML5 standards continues to mature, we are seeing a shift in Web Architecture. More and more processing is happening client side rather than on the server. Instead of the Server doing heavy processing and generating all the html, it is responding to only ajax requests that sends out just raw data! This is also true for mobile applications that consume these data with their native apps.The end result is developers are looking for a fast, efficient, lightweight and extensible host for the Web applications and Web Services, particularly with cloud based services where you pay for every byte and CPU cycles.
Like Linkedin does using Node.JS and HTML5 to build better and faster apps. It is easy to add a package to Node and make it run a lean and mean web server using a fraction of traditional resources on server side. It is also easier to scale because it consumes just the resources it needs.
Katana follows this architecture unlike ASP.NET MVC which relies on one giant core System.Web assembly on every project. Instead of one large assembly, Katana features are highly modular and componentized so you can add only the features you need for your project.
Katana allows you to create and plug in your middleware components by being agnostic of whether they are hosted in a console app or in IIS etc. More and more middleware components like Cookie Authentication have been developed for Katana moving away from dependence on ASP.NET MVC modules and handlers and filters etc .
To use and host your Katana custom components in IIS, create the component with the Startup class as a DLL with its build target pointing at the /bin folder in your project or solution. For more information and Katana components, look at the Katana open source project in codeplex.
To learn more about Katana, there's an excellent course by Scott Allen @pluralsight
http://www.pluralsight.com/courses/aspdotnet-mvc5-fundamentals
Subscribe to:
Posts (Atom)