CS-Script "Could not load file or assembly" - c#

I'm using CS-Script (link: http://www.csscript.net/) to compile a script file. According to their documentation, I'm doing it right.
new AsmHelper(CSScript.Compile("test.cs"), null, true);
However at runtime, I get:
Could not load file or assembly
'file:///C:\Users...\CSScriptLibrary.dll'
or one of its dependencies. Operation
is not supported. (Exception from
HRESULT: 0x80131515)
I'm somehow suspecting .NET 4.0 or some obscure manifest, but I did add the CSScriptLibrary.dll in the .NET 4.0 directory of cs-script/Lib/Bin.
Any suggestion?

Make sure you add it as a reference in your C# project.
See here for info on how to do it: http://msdn.microsoft.com/en-us/library/7314433t(VS.80).aspx
-Edit-
If that doesn't work, try this: Link

Related

Wixsharp - Could not load file or assembly

I am trying to include some reference files in Wix# managed project using DefaultRefAssemblies.Add method:
ManagedProject project = new ManagedProject();
project.DefaultRefAssemblies.Add("FontAwesome.Sharp.dll");
project.DefaultRefAssemblies.Add("protobuf-net.dll");
project.DefaultRefAssemblies.Add("Newtonsoft.Json.dll");
project.DefaultRefAssemblies.Add("ManagedOpenSsl.dll");
project.DefaultRefAssemblies.Add("ssleay32.dll");
When I try to build a MSI I get an error. The problem happens to be in loading of ssleay32 assembly (part of OpenSSL). When I exclude this file, the build succeeds. Can you please help me understand the exception? The target framework of the Wix# project is .NET Framework 4.8.
Unhandled Exception: System.BadImageFormatException: Could not load file or assembly '361984 bytes loaded from WixSharp, Version=1.15.0.0, Culture=neutral, PublicKeyToken=3775edd25acc43c2' or one of its dependencies. An attempt was made to load a program with an incorrect format. ---> System.BadImageFormatException: Bad IL format.
Wixsharp supports .net assemblies compiled for:
Platform x86
Managed assemblies (Not native)
Target framework 3.5
So, make sure "ssleay32.dll" has 3 requirements above. Every ssleay32 assembly i have found was native (unmanaged).
Workaround in case of no luck with:
You can try to save ssleay32.dll to embedding resource file and before "ManagedOpenSsl.dll" types usage you should load dll in the memory. Load unmanaged assembly
Good luck!

Referencing library using Windows.winmd

What I'm trying to do
I'm trying to create a library (Arduino.dll) to interact with my Bluetooth device from a laptop (Windows 10). This library is intended to be used by a desktop application.
To achieve that, I had to reference both Windows.winmd and System.Runtime.WindowsRuntime.
What's working
I did write the library and tested it on a console application which was on another project in the same solution. To make it work, I had to reference the project and Windows.winmd in the test project. Note that this test project is used to test all of my libraries, so it is heavily manipulated and I tinkered with the settings a lot for years.
What's not working
I finished to write my dll, and wanted to use it in my desktop application. Therefore, I referenced both Arduino.dll and Windows.winmd as in my test project, expecting to work the same way. Sadly, on running the application, I got a "dll not found" when instantiating a class for Arduino.dll :
An unhandled exception of type 'System.IO.FileNotFoundException' occurred in LuxAFX4.exe
Additional information: Could not load file or assembly 'System.Runtime.WindowsRuntime, Version=4.0.10.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies. The system cannot find the file specified.
Strange, but well, I tried to reference the (same) System.Runtime.WindowsRuntime as in Arduino.dll but no luck, I now got an even weirder error :
An unhandled exception of type 'System.BadImageFormatException' occurred in LuxAFX4.exe
Additional information: Could not load file or assembly 'System.Runtime.WindowsRuntime, Version=4.0.10.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies. Reference assemblies should not be loaded for execution. They can only be loaded in the Reflection-only loader context. (Exception from HRESULT: 0x80131058)
During my search for a solution, I understood that I shouldn't try to load this "reference library" and if it is not found, it must be because it is not installed on the system. But then, why would it work in my test project?
Another thing I may have understand, is that if my library doesn't expose members of referenced 'sub'-libraries, they won't be needed when referencing my library. Should I try to 'hide' all references? If so, is there a way to catch all exposed members? Source : How to to avoid referencing a dll's referenced dll
Side note : I use C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETCore\v4.5.1\System.Runtime.WindowsRuntime.dll and C:\Program Files (x86)\Windows Kits\10\UnionMetadata\Windows.winmd.
May be related : Could not load file or assembly Windows.winmd and How to reference Windows.winmd from a .NET Core library?
After some more research, and with the hint given by Hans Passant, I finally understood that I had chosen the wrong System.Runtime.WindowsRuntime.dll to start with. Changing it to use C:\Windows\Microsoft.NET\Framework64\v4.0.30319\System.Runtime.WindowsRuntime.dll instead in both the project with the DLL and the project using it, resolved my problem.
I got the new file location here : C# “await” error when using WinRT from Desktop app

Unable to load DLL 'LibFlac': The specified module could not be found. (Exception from HRESULT: 0x8007007E)

HERE is the link to my project.
The error is on line 107: "context = FLAC__stream_encoder_new();" in file: FlacWriter.cs --> I didn't write that file. I got it from HERE
So I understand that libFLAC.dll is missing. So I tried all of the below approaches to solve the issue:
I downloaded libFLAC dll form rarewares.org/lossless.php#flac-dll-x64 I tried to add it as a reference, i get this ERROR:
"A reference to C:\CodeProjects\WaveConverter\WaveConverter\Libraries\libFLC_dynamic.dll' could not be added. Please make sure that the file is accessible, and that it is valid assembly or COM component."
I tried something else: I added the dll file to bin/Debug, reopened my project. ran the project again. same error on line: " context = FLAC__stream_encoder_new();" ERROR:"Unable to load DLL 'LibFlac': The specified module could not be found. (Exception from HRESULT: 0x8007007E)"
I also tried 'importing' DLL in my code like this: I tried to add this line: "[DllImport("libFLAC.dll")]" under line 14 in my project file: Worker.cs
I get this ERROR: "Duplicate 'DLLimport' attribute'
I also downloaded dependency-walker from dependencywalker.com Not sure how to operate it, but when it finished downloading, I clicked on OPEN icon and opened the libFLAC_dynamic.dll file I was trying to add to the project. And there were no errors. I'm not sure how to use this dependencyWalker though...
What else can I try? How do I fix this error?
You are p/invoking an unmanaged library names LibFlac.dll. You need that library and its dependencies to be in the DLL search path when you first call a function from the library. Typically that means doing the following:
Placing LibFlac.dll in the same directory as your executable file, and
Installing all dependencies that LibFlac.dll has. Typically this will be an MSVC C++ runtime. Read the documentation carefully to work out what dependencies the DLL has.
This is an unmanaged DLL. Don't attempt to add it as a reference. It also seems that the DLL you have is named libFLC_dynamic.dll which differs from the name that your code is expecting. Have you got the right DLL?

Referenced assembly 'GenCode128.dll' is not a valid assembly

I am now dealing with a existing solution in VS2012.
Now I can execute it in debug mode but cannot trigger some of the function with the error
"Exceptions: System.Exception: Could not load file or assembly
'Interop.Score, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=5ce8a8a190705d24' or one of its dependencies. The
located assembly's manifest definition does not match the assembly
reference. (Exception from HRESULT: 0x80131040)"
After finding some of the suggestion, some of that suggest me to compile the solution in 32-bit (x86).
However, when I tried to rebuild with the target platform x86, another type of error occur and I even cannot run in debug mode, the error:
Referenced assembly 'GenCode128.dll' is not a valid assembly C#
P.S. GenCode128.dll is one of the error .dll, there is at least 2 of this kind of error.
I cannot search any successful way to solve this problem, appreciate for any help.
You can use a decompiler such as DotPeek (https://www.jetbrains.com/decompiler) to look inside your DLL and see if it is a valid .NET DLL -- and which version of .NET it is. It could be an x64-compiled assembly; in that case you cannot use it on a 32-bit system or from a 32-bit-compiled .NET assembly.
You can try installing it from NuGet.
I've created a NuGet package for GenCode128.dll here: https://www.nuget.org/packages/GenCode128/
Also the source can be found in GitHub: https://github.com/SourceCodeBackup/GenCode128

"Unverifiable code failed policy check" for a closed source assembly

I'm attempting to dynamically load some (purchased) assemblies from resource streams in a C# program during an MSI installation routine, but I'm getting "Unverifiable code failed policy check".
I read some tips online about compiling the embedded assembly with /clr:safe, but I don't have that option. Is there a way to work around this policy check?
Thanks.
The only way to load unverifiable code is from a full trust process (or maybe app domain) with verification disabled.
EDIT: I'm not making this up, one of the C# language designers said "Unverifiable code requires full trust and is generally to be avoided"
Its already too late, but it may help someone.
I've used Sqlite DLL in my project and when I deployed the code on sandbox environment, I was getting exception (see below)
[FileLoadException: Unverifiable code failed policy check. (Exception from HRESULT: 0x80131402)]
[FileLoadException: Could not load file or assembly 'System.Data.SQLite, Version=1.0.79.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139' or one of its dependencies. Unverifiable code failed policy check. (Exception from HRESULT: 0x80131402)]
I added trust level within system.web in the web.config file and its working fine.
<trust level="Full|High|Medium|Low|Minimal" originUrl="url"/>
This may not be relevant any longer, but I encountered the same issue recently. If you take the stream and stick it in a temp file, you can then use Assembly.LoadFrom to load the assembly.

Categories