MadsKirkFoged / SharpFluids

Lightweight CoolProp C# Wrapper - for easy fluid properties lookups
MIT License
30 stars 9 forks source link

Doesn't work in Blazor WebAssembly #10

Closed jeisonjei closed 3 years ago

jeisonjei commented 4 years ago

Hello, unfortunately library doesn't work with .netcore console app on my computer. Error on the line Fluid air=new Fluid(FluidList.Air); Details below:

System.TypeInitializationException
  HResult=0x80131534
  Message=The type initializer for 'CoolPropPINVOKE' threw an exception.
  Source=SharpFluids
  StackTrace:
   at CoolPropPINVOKE.AbstractState_factory__SWIG_0(String jarg1, String jarg2)
   at AbstractState.factory(String backend, String fluid_names)
   at SharpFluids.Fluid..ctor(FluidList Type)
   at c03.Program.Main(String[] args) in C:\Users\Danil\Documents\CodingTrainer\c03\Program.cs:line 11

Inner Exception 1:
TypeInitializationException: The type initializer for 'SWIGExceptionHelper' threw an exception.

Inner Exception 2:
BadImageFormatException: An attempt was made to load a program with an incorrect format. (0x8007000B)
MadsFoged commented 4 years ago

I should work fine in .NET core however the error you are seeing is from using it in 64bit which it does not (yet) support.

1) Right click on your project 2) Select "Configuration manager" 3) Below 'Platform' click on 'Any CPU' 4) Select '' 5) Select 'x86'

jeisonjei commented 4 years ago

Yes, it works, thank you! Actually my problem is that I want to use this library for Blazor application which is on .netstandard2.1. Is it possible to make it working on .netstandard?

MadsFoged commented 4 years ago

I have just updated the Nuget so It should/might work on .netstandard2.1 Can you test if it does?

jeisonjei commented 4 years ago

Just tried, on my computer doesn't work Here is the code:

@page "/"
<div class="row">
    <div class="col-4">
        <div class="row">
            <div class="col">
                <button class="btn btn-info btn-lg" @onclick="ShowDensity">Show Density</button>
            </div>
            <div class="col">
                <h2>@Result</h2>
            </div>
        </div>
    </div>
</div>

@code   {
    public double Result { get; set; }
    public void ShowDensity()
    {
        Fluid air = new Fluid(FluidList.Air);
        air.UpdatePT(Pressure.FromBars(1.013), Temperature.FromDegreesCelsius(13));
        Result = air.Density.Value;
    }
}

Here is the error from page: 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 assembly:<unknown assembly> type:<unknown type> member:(null) at (wrapper managed-to-native) ... Active project platform is x86

MadsFoged commented 4 years ago

It looks like it cant find the 'CoolProp.DLL'

Im not sure if a Blazor application does something special but you can try to manually place the DLL into it output folder and see if that in fact in the real error

jeisonjei commented 4 years ago

I actually don't know, but maybe this could tell something:

#r "C:\Users\Danil\.nuget\packages\sharpfluids\2.0.9"
(1,1): error CS0006: Metadata file 'C:\Users\Danil\.nuget\packages\sharpfluids\2.0.9' could not be found
> #r "C:\Users\Danil\Documents\CodingTrainer\a01\Client\bin\Debug\netstandard2.1\CoolProp.dll"
(1,1): error CS0009: Metadata file 'C:\Users\Danil\Documents\CodingTrainer\a01\Client\bin\Debug\netstandard2.1\CoolProp.dll' could not be opened -- PE image doesn't contain managed metadata.
jeisonjei commented 3 years ago

Well, I eventually have found the answer - since all code in Blazor WebAssembly executes in the browser, framework kind of convert all code to WebAssembly. And since SharpFluids uses CoolProp.dll library, Blazor can't use it. Explanation is here https://stackoverflow.com/questions/66966926/c-dll-in-blazor-project But it's completely possible to use SharpFluids in Blazor Server app