This topic only applies to Blazor WebAssembly projects.


Blazor is a .NET web framework that allows developers to run C# in the browser. Blazor compiles C# code into WebAssembly format.


WebAssembly (WASM) is a small, fast binary format that promises near-native performance for web applications. WebAssembly is designed to be a compilation target for any language, JavaScript being just one of them. Every major browser supports WebAssembly.


Why a HttpClient needs special attention in a Blazor WebAssembly project

The .Net HttpClient class is used by VenturaSQL for sending HTTP requests to the server.


From a Blazor WebAssembly app you cannot simply create a new instance of the HttpClient class and use it to access the server. The HttpClient in Blazor WebAssembly is implemented using the browser Fetch API and is subject to its limitations, including enforcement of the same origin policy.


For this reason a HttpClient instance can only be created by the Blazor runtime. Blazor will instantiate and configure the HttpClient to run in the sandbox environment of the browser.


Whenever Ventura needs a HttpClient instance it calls a user defined method that is expected to return a proper HttpClient object. The method is assigned with VenturaConfig.SetHttpClientFactory(). Inside that method, Blazor WebAssembly is asked for a HttpClient instance.


Below the standard Program.cs for a Blazor WebAssembly project. The highlighted lines need to be added to the Program class.


  • The line VenturaConfig.SetHttpClientFactory(GetHttpClientForVenturaSQL) sets the HttpClientFactory method that VenturaSQL will call whenever it needs a HttpClient instance.
  • The call to _service_provider.GetService(typeof(HttpClient)) asks the Blazor WebAssembly runtime for a HttpClient instance.
  • The VenturaConfig.DefaultConnector is dynamically set to the correct URL.


Program.cs


using System;

using System.Net.Http;

using System.Threading.Tasks;

using Microsoft.AspNetCore.Components.WebAssembly.Hosting;

using Microsoft.Extensions.DependencyInjection;

using Ventura;


namespace BlazorDemo.Client

{


   public class Program

   {

       private static ServiceProvider _service_provider;


       public static async Task Main(string[] args)

       {

           var builder = WebAssemblyHostBuilder.CreateDefault(args);

           builder.RootComponents.Add<App>("app");


           builder.Services.AddTransient(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });


           SetupVentura(builder);


           await builder.Build().RunAsync();

       }


       private static void SetupVentura(WebAssemblyHostBuilder builder)

       {

           // This is needed by VenturaSQL

           _service_provider = builder.Services.BuildServiceProvider();


           // This is needed by VenturaSQL

           VenturaConfig.SetHttpClientFactory(GetHttpClientForVenturaSQL);


           // This is needed by VenturaSQL.

           VenturaConfig.DefaultConnector = new HttpConnector("DefaultConnector", $"{builder.HostEnvironment.BaseAddress}Ventura.FSPRO");


       }


       private static HttpClient GetHttpClientForVenturaSQL(HttpConnector connector)

       {

           var client = _service_provider.GetService(typeof(HttpClient)) as HttpClient;


           return client;

       }


   }

}