Local Application

Wisej.NET Hybrid applications can be configured to run offline without any need to connect to a remote web server.

Samples

Configuration

When creating a new Hybrid Client application, an offline application can be specified with the UseWisejOffline() extension method.

Startup.cs
public static MauiApp Main()
{
	var builder = MauiApp.CreateBuilder();
	builder
		.UseMauiApp<App>()

		 // Uncomment and replace with Offline startup Type to use embedded web server.
		 .UseWisejOffline<OfflineStartup>()

		// Uncomment when registering AppBuilderExtenders as part of a separate class library.
		// .UseWisejExtenders()

		.UseWisejHybrid((config) =>
		{
			// Uncomment to provide an offline fallback timeout.
			// config.OfflineTimeout = 5000;

			// Provide the startup URL for the Hybrid WebView.
			config.StartupUrl = "http://localhost:5000";
		});

	return builder.Build();
}

In the example above the client application registers an offline app, OfflineStartup, which runs an embedded web server that serves the Wisej.NET application:

OfflineStartup.cs
public class OfflineStartup
{
	public static string Main(CancellationToken token, object[] args)
	{
		var url = "http://localhost:5000";
		var server = new WebServer(url);
		server.WithWisej();
		server.WithStaticFolder("/", Path.GetDirectoryName(typeof(OfflineStartup).Assembly.Location), true);

		new Thread(() => server.RunAsync(token).Wait()).Start();

		return url;
	}
}

The default OfflineStartup file can be found within the Wisej.NET Hybrid Offline Application project template in Visual Studio.

Switching Offline & Online Applications

It is possible to switch between the offline (disconnected) application and online (connected) application with one few line of code:

At any point in time, simply use Application.Navigate("url", "target") to switch between URLs.

For example, if an offline application was registered on port 5000, but you're currently connected to an "online" application, you can use the following line of code anywhere in your Wisej.NET application to switch to the offline version:

Application.Navigate("http://localhost:5000");

Offline Fallback

If you would like to switch to the offline version of an application when the network gets disconnected, configure the OfflineTimeout member inside of the client app's Startup.cs file:

public static MauiApp Main()
{
	var builder = MauiApp.CreateBuilder();
	builder
		.UseMauiApp<App>()

		// Uncomment and replace with Offline startup Type to use embedded web server.
		.UseWisejOffline<OfflineStartup>()

		.UseWisejHybrid((config) =>
		{
			// Uncomment to provide an offline fallback timeout.
			config.OfflineTimeout = 5000;

			// Provide the startup URL for the Hybrid WebView.
			config.StartupUrl = "http://localhost:5000";
		});

	return builder.Build();
}

The line config.OfflineTimeout = 5000; in the example above specifies that when the user is no longer connected to a network, the application should automatically switch from the "online" version to the "offline" version of the application.

Last updated