Skip to content

OWIN ASP.NET Core

mattkol edited this page Jul 21, 2021 · 2 revisions

EdgeSharp implement's OWIN based Chromium.AspNetCore.Bridge which allows developers to integrate ASP.NET Core MVC, Razor and RazorPages without having to make network requests. Although this is primarily based on ASP.NET Core, with some tweaks developers can extend it for other OWIN based framework like NancyFx, Carter or a custom OWIN implementation.

EdgeSharp.Core.Owin contains all functionalities related to EdgeSharp OWIN. What developers need to be aware of that EdgeSharp OWIN applications requires an implementation of OwinAppBuilder for Win32 or OwinAppBuilder for WinForm/Wpf; setting the Scheme handler to use and setting apppropriate StartUrl.

Please see - Sample Owin AppBuilder for Win32

public class SampleOwinApp : OwinAppStartup
{
   public SampleOwinApp()
	: base (new CoreServices())
   {
   }
	
   public override void ConfigureServices(IServiceCollection services)
   {
	 base.ConfigureServices(services);
	 --- configure services
  }
}

The only requirement here is the CoreServices. Each of EdgeSharp hosts - Win32, WinForm and Wpf has its core services - Win32 Core Services, WinForm Core Services and Wpf Core Services. Although, the individual CoreServices class could be extended by inheritance, it is a lot easier to just override services in ConfigureServices method.

WinForm and Wpf uses a common AppBuilder that requires an implementation of IStartup template parameter. Samples IStartup for WinForm/WPF can be found at Sample WinForm IStartup, Sample Wpf IStartup.

Below snippet is a [Wpf Sample AppBuilder].

public partial class App : Application
{
   private OwinAppBuilder<Startup> _owinAppBuilder;
   protected override void OnStartup(StartupEventArgs e)
   {
	_owinAppBuilder = new OwinAppBuilder<Startup>();
	ServiceLocator.Bootstrap(_owinAppBuilder);

	SampleWindow mainWindow = new SampleWindow();
	mainWindow.Show();
  }

  protected override void OnExit(ExitEventArgs e)
  {
	_owinAppBuilder?.Stop();

	base.OnExit(e);
  }
}
	

Note: It is advisable to create a variable to hold the AppBuilder so that it can be properly stopped on application window exit. How and where the AppBuilder is created follows no particular pattern - what is important is the the bootstrapper must be called before the main window hosting WebView2 is created.

ServiceLocator.Bootstrap(_owinAppBuilder);

To register a Owin resource Scheme, it requires

  • Scheme - e.g http, https
  • Host - e.g edgesharp.com, mycustom.com - these are arbitrary/fake hostnames that are not expected to be real hostnames.
  • Folder - null. It is redundant - unused
  • Scheme type - Owin. Other scheme types can be found at: UrlSchemeType.

Following will use:

Scheme Host Folder Scheme Type
http edgesharp.com null Owin
var config = new Configuration();
var aspNetCoreScheme = new UrlScheme("http", "edgesharp.com", UrlSchemeType.Owin);
config.UrlSchemes.Add(localResource);
config.StartUrl = "http://edgesharp.com/";

Note: EdgeSharp registers Scheme: "https"; Host: "edgesharp.owin.com" by default, so developers do not need to register a new one if they are fine using the default.

If this default is used, the start url will be:

config.StartUrl = ""https://edgesharp.owin.com/";