MadsKirkFoged / SharpFluids

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

Using SharpFluids in a .Net 4.6.1 Class Library, getting 'Coolprop.dll not found' error after rebuilds #36

Open Sean-Hasse opened 2 years ago

Sean-Hasse commented 2 years ago

I've been getting an inconsistent issue with SharpFluids since changing a project to a library to add to another, larger project. The first time my project tries to create a fluid with 'Fluid f = new Fluid(FluidList.Water);' I get an exception. It appears that the code is not finding the CoolProp.dll when it tries to convert the FluidList object to a Fluid object. I've confirmed that the CoolProp.dll is present in the Debug folder, and that I'm properly calling 'using SharpFluids;' at the start and all.

Mystifyingly, the workaround I've found so far is to comment out that first 'Fluid f =' line, let my code run, and after it crashes because Fluid f is a null reference object, I un-comment out that code and it runs perfectly fine.

I suspect it's either an issue with the loading order for my project, or maybe it's an issue with the .Net framework I'm using. Any insights at all would be appreciated.

Additional Information: SharpFluids being used is version 3.0.270 but has been observed on multiple versions The full error is this: System.TypeInitializationException: The type initializer for 'CoolPropPINVOKE' threw an exception. ---> System.TypeInitializationException: The type initializer for 'SWIGExceptionHelper' threw an exception. ---> System.DllNotFoundException: Unable to load DLL 'CoolProp': The specified module could not be found. (Exception from HRESULT: 0x8007007E)

MadsKirkFoged commented 2 years ago

I somethings get the error myself when starting up a new project. I believe it is because the Nuget have to copy over the "Coolprop.dll" and sometimes it just doesn't do it. I can just press "Rebuild" and it then copies the dll correctly.

What you are talking about it using SharpFluids inside a library that you then use elsewhere. I have done exactly that and as I remember I got the same issue. The issue is that the nuget package can't copy the dlls when you get down another level. So the issue is to with copying files in nesting nugets packages and not really SharpFluids

Fix: I have put CoolProp.dll and CoolProp64.dll directly into the library and marked them Copy if newer

Sean-Hasse commented 2 years ago

Thank you very much for this help! One question: I did manage to find and set the packages properties to 'Copy if newer', but I'm not sure what level in the files you'd move CoolProp.dll and CoolProp64.dll to.

I copied and pasted both .dll files to my library's Project folder (so, on the same level as the other .cs files), and set both to Copy if newer, but upon Cleaning and Building, I'm running into the same error.

Sean-Hasse commented 2 years ago

Unfortunately, despite looking online for answers regarding this bug, I haven't found anything yet. What you described seems very likely the source of this glitch, rather than something to do with SharpFluids in particular, but I haven't been able to implement the solution that you suggested.

MadsKirkFoged commented 2 years ago

1) Can you try to update to maybe net47 or newer? Just to rule that out 2) After you have build your program can you find the dll files in the folder ..bin\Debug\

Sean-Hasse commented 2 years ago
  1. Switching to .Net 4.7+ might be possible, but it would require enough restructuring of the code that I am hesitant to try that unless it is necessary.

  2. I can confirm that both CoolProp.dll and CoolProp64.dll are in the ..bin\Debug\ folder after building the project.