I've been browsing and googling about how did things like OWIN hooks on the ASP.NET activities.
I know if we create a self hosted OWIN, we will call it manually and it is clear that we are calling OWIN to make it start running.
However, I see that OWIN is automatically invoked when ASP.NET started without the need of calling webapp start or anything else. OWIN just hook into ASP.NET and act as an interceptor on each request.
My example would be signalr, we call the mapping of signalr in OWIN configuration method. However, I dont see anything that could possibly call OWIN configuration method. But signalr is already mapped and works.
How did OWIN hooks on ASP.NET activities? Is it OWIN who register the hooks or ASP.NET who now recognize OWIN and call automatically?
You will have a similar line like this on your project:
[assembly: OwinStartup(typeof(MyApp.Security.Authentication.Startup))]
The line above informs .NET the class and method that will be invoked at the beginning.
Alternative you can configure the startup in WebConfig
<appSettings>
...
<add key="owin:appStartup" value="MyApp.Security.Authentication.Startup" />
...
</appSettings>
From that point, you can place OWIN components, as well as all the configuration items you would normally place in the Global.asax Application_Start event handler.
Delete Global.asax Class: If you use OWIN, there's no need to use Gobal.asax class and fire up the Application_Start event, so you can delete it.
Code example of Startup.cs
using System.Web.Http;
using Microsoft.Owin;
using Owin;
[assembly: OwinStartup(typeof(MyApp.Security.Authentication.Startup))]
namespace MyApp.Security.Authentication
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
HttpConfiguration config = new HttpConfiguration();
WebApiConfig.Register(config);
app.UseWebApi(config);
}
}
}
Edited:
OWIN uses a startup class where you can specify the components you wish to include in the application pipeline. If you look at the Katana source code, the Katana SystemWeb host uses the PreApplicationStartMethodAttribute to hook into the application startup. PreApplicationStartMethodAttribute, wich is introduced in .NET 4, allows you to have code run way early in the ASP.NET pipeline as an application starts up. I mean way early, even before Application_Start.
Check the "Owin Startup class detection" section in this link and this link about PreApplicationStartMethodAttribute.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With