Unhandled exception when using C# wrapper with Blazor-Web-Assembly-App
Description
I followed the recommended steps for including the C# wrapper with Nuget package. This works well for console applications but not for Blazor-Web-Assembly-Apps, where I get an unhandled exception.
Steps to Reproduce in Visual Studio 2019
- Create new console application
- Add Nuget package SharpFluids
- In Programm.cs replace all code with:
using SharpFluids;
using System;
using UnitsNet;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
var Water = new Fluid(FluidList.Water);
Water.UpdatePT(Pressure.FromBars(1.013), Temperature.FromDegreesCelsius(13));
Console.WriteLine("Density of water at 13°C: " + Water.Density);
Console.ReadLine();
}
}
}
- Run app, everythink works well.
- Create new Blazor-Web-Assembly-App. On the Additional information window check Progressive Web Application.
- Add Nuget package SharpFluids
- In Index.razor replace all code with:
@page "/"
@using SharpFluids
@using UnitsNet
<h1>Hello, world!</h1>
<button class="btn btn-primary" @onclick="TestPR">Click me</button>
<span>@waterDensity</span>
@code{ private UnitsNet.Density waterDensity;
private void TestPR()
{
var water = new Fluid(FluidList.Water);
water.UpdatePT(Pressure.FromBars(1.013), Temperature.FromDegreesCelsius(13));
waterDensity = water.Density;
} }
- Run app, click button, see the unhandled exception error.
The exception occurs in the line var water = new Fluid(FluidList.Water);
Please see message from Visual Studio Output window below.
Versions
CoolProp Version: Nuget package SharpFluids 2.0.93
Operating System and Version: Windows 10
Access Method: Visual Studio Professional 2019 Version 16.10.2 , C#
Additional Information
crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100] Unhandled exception rendering component: The type initializer for 'CoolPropPINVOKE' threw an exception. System.TypeInitializationException: The type initializer for 'CoolPropPINVOKE' threw an exception. ---> System.TypeInitializationException: The type initializer for 'SWIGExceptionHelper' threw an exception. ---> System.DllNotFoundException: CoolProp at CoolPropPINVOKE.SWIGExceptionHelper..cctor() --- End of inner exception stack trace --- at CoolPropPINVOKE..cctor() --- End of inner exception stack trace --- at AbstractState.factory(String backend, String fluid_names) at SharpFluids.Fluid.SetNewMedia(MediaType Type) at SharpFluids.Fluid..ctor(MediaType Media) at SharpFluids.Fluid..ctor(FluidList Type) at WebApplication4.Pages.Index.TestPR() in C:\Users<user>\source\repos\WebApplication4\Pages\Index.razor:line 17 at Microsoft.AspNetCore.Components.EventCallbackWorkItem.InvokeAsync[Object](MulticastDelegate delegate, Object arg) at Microsoft.AspNetCore.Components.EventCallbackWorkItem.InvokeAsync(Object arg) at Microsoft.AspNetCore.Components.ComponentBase.Microsoft.AspNetCore.Components.IHandleEvent.HandleEventAsync(EventCallbackWorkItem callback, Object arg) at Microsoft.AspNetCore.Components.EventCallback.InvokeAsync(Object arg) at Microsoft.AspNetCore.Components.RenderTree.Renderer.DispatchEventAsync(UInt64 eventHandlerId, EventFieldInfo fieldInfo, EventArgs eventArgs)
Is it even possible in general to use PINVOKE in C# in Blazor to call shared libraries that are binary? My gut says no.
I googled "blazor pinvoke binary" and found some comments but I do not understand the details. Could you please invest few minutes? If using the CoolProp.dll in Blazor is not possible, is there another way to implement the calculations?
I'm afraid you are on your own; I can't dig into this question given my constraints. I don't think it will be possible, unless you can recompile CoolProp into WASM. You can get javascript WASM, but I have no idea if Blazor can call this generated WASM. I would avoid SharpFluids if possible, to keep it simple(r).
OK Ian, I will try to find a solution and let you know if I do so.