Being less stupid.

This is going to be basically just a ramble — TLDR version at the bottom.

I’ve been learning IdentityServer3 for the better part of 2, maybe 3 weeks now. I’m a self-taught developer with nearing 3 years of development. When I jumped on, I knew how web servers worked to a degree thanks to taking HTML classes and my Net Sec degree but I never understood how underlying frameworks like ASP.NET MVC and OWIN worked. I pressed a pretty button and BOOM a WEB API / MVC Application was born and I could start coding away. For the most part, how it worked was magic to me but I knew which code did what after the project was spun up and was good to go. While attempting to setup my server w/ IDS3 to accept its own tokens for the API it is also hosting… I’ve begun to start understanding what goes on underneath those templates when you hit the button for a new project…

I was tasked to spin up a new API in a language I’m unfamiliar with, where there are no pretty templates to get things up and running… you just have to understand how the underlying framework works. I failed. I had no idea HOW MVC created an API or, as I’m realizing now, what OWIN even was. Each of these frameworks can be used separately or in conjunction to spin up a flavor of Web API whether you’re using MVC’s global.asax which is tied to a code-behind file for configuration or using OWIN to specify a start-up file which does about the same thing, with a different flow.

I never really took the time to actually learn how the hell these things worked, and I see now I’ve suffered because of it. Here’s an example of what I’ve been talking about:

In an ASP.NET project, there’s a file called global.asax…

The Global.asax file, also known as the ASP.NET application file, is an optional
file that contains code for responding to application-level events raised by 
ASP.NET or by HttpModules. The Global.asax file resides in the root directory of 
an ASP.NET-based application.

When you’re spinning up a WEB API with ASP.NET, this file routes to a code-behind which begins all the configuration for the server. So the markup for it would look like

<%@ Application Codebehind="Global.asax.cs" Inherits=" WolfSoftware.IdentityServer.WebApiApplication" Language="C#" %>

The codebehind for it would then by default have some configuration done

public class WebApiApplication : HttpApplication
 {
    protected void Application_Start()
    {
       AreaRegistration.RegisterAllAreas();
       GlobalConfiguration.Configure(WebApiConfig.Register);
       FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
       RouteConfig.RegisterRoutes(RouteTable.Routes);
       BundleConfig.RegisterBundles(BundleTable.Bundles);
    }
 }

Up until now, this was all really magic code for me. I didn’t understand how this really configured the server, just that it did.

So now, throwing OWIN into play…

Open Web Interface for .NET (OWIN) defines an abstraction between .NET web 
servers and web applications. OWIN decouples the web application from the server,
which makes OWIN ideal for self-hosting a web application in your own process, 
outside of IIS.

So OWIN can do exactly what ASP.NET just did, but without having the global.asax and depending on markup. With OWIN, you declare a class as the startup class and begin configuration from there.

[assembly: OwinStartup(typeof(LobbyGuard.IdentityServer.Startup))]

namespace LobbyGuard.IdentityServer
{
   public partial class Startup
   {
      public void Configuration(IAppBuilder app)
      {
         ConfigureWebApi(app);
      }
      private void ConfigureWebApi(IAppBuilder app)
      {
         HttpConfiguration config = new HttpConfiguration();
         config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
         );
         app.UseWebApi(config);
      }
 }

This has a bit less configuration than what was shown with the ASP.NET but the idea is there. Now that I know (I feel like I did, it just didn’t click when I needed it to…) how it all works… I completely understand now how to spin up the Web API in the new language I was tasked with because I know how more about the framework itself.

TL;DR

I didn’t know how to spin up a ASP.NET or OWIN Web Api in a new language because I’ve been used to C# templates doing the configuration for me; Now I understand the differences between the two, how they both work, and how I could use said new language.

I don’t know what was mystifying me before about any of this, did I just not look? did I not read? Time to be a little less… stupid.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s