C# reference that requires additional files - c#

I'm having a problem with a third party C# class library:
The given .net dll requires additional files to be in the same directory than the .dll (config files etc.).
When I load the library as reference into asp.net project everything works smoothly until first call to the library. I get exceptions from the library saying that the required files are missing.
Now the question is where should I put those required files into? I've tried obj/debug/, bin/ and including the files in the project and selecting "Always copy to output directory", but none helped.
I guess it's something to do with IIS and its way of handling the reference/additional files?
Edit: It's working fine in Windows Forms application, but not in ASP.NET web application.

You can view source code of external class library with iLSpy or Reflector to find how exactly additional files are referenced.
It's can be an application execute path (so it must be IIS shadow copy directory) or GAC.

For that you need to read the documentation. The method throwing the error should be expecting the file in some other folder other than bin, debug or obj.

Related

How to include a C# .dll wrapper in a C# project?

So I have a C# project and I'm trying to add USB functionality to it via the Silicon Labs CP2112 USB to i2c bridge. They provide drivers as 2 .dll files: SLABHIDtoSMBus.dll and SLABHIDDevice.dll (which apparently is needed at runtime). I've also found a SLABCP2112.cs file that is supposed to allow the dll to be used in C#.
I've added the SLABCP2112.cs to my project successfully and Intellisense recognizes the functions. The problem is that when I run the program it always throws an exception that the SLABHIDtoSMBUS.dll is not found(System.DllNotFoundException). I've tried moving the dll to various locations such as the main project folder, the 'USB' folder in the project where I've included the SLABCP2112.cs, the Windows SysWOW64 folder and finally in the c#'s projects 'bin' folder.
Placing it in the bin folder still throws an exception but changes the error to a bad image error or something similar. For good measure I've tried to add the .dll's as References in the project but this causes an error "invalid or unsupported type".
I'm assuming the problem is still that the compiler doesn't know where to look for the dll files, so where should I put them? Or if not, how do I get this working?

c#, how to add a binary dependency file safe as resource to the EXE in designer

my visual studio project has some includes (i use the .net framework 4.6.1).
So I wanted to do a loader in .net 2.0 for unpacking the needed files without admin rights into a user folder with the EXE together.
My idea was to add them as type "FILE" resource into the "loader" project and then extract them as a file again in the user directory together with the .net 4.6.1 EXE. As soon I add one of the files to my NET 2.0 loader project as resource in the designer, the whole project is broken and spits out errors, which I do not understand.
How I can add a complex file as a resource in my project (DLL / EXE)? Simple TEXT-Files work so far. (The extracting routine works also). I get the error right after adding a binary dependency file (DLL) like "System.Data.dll". It says after adding the resource that I have a wrong reference or something alike. The project becomes immediately unusable after it with that error persisting. If I delete the file out of the resource again, the error persists. I have to start all over again with the project.
Any ideas?

Packaging files into one executable (C#)

I developed a small project using C# and got everything working perfectly.
The only problem is that this app will be regularly sent via emails, therefore I'm trying to assemble all the files as one executable. The user will only have to open and see one executable, which will load the needed files and start the application.
The app does not use any external libraries. The only .dll file is for the IWshRuntimeLibrary.
The files that need to be assembled:
Note that I know that some files can be removed (svhost etc.). However this won't help because I'll still have more than one file. Also I've tried some free assemblers online but none of them worked.
In Solution Explorer, select the reference of Interop.IWshRunctime... and in the property window, change the value of Embed Interop Types to True.
This will not generate a separate dll for Interop Types.
PS: This is not a generic solution, but works with Interop Type only.
I've used https://github.com/Fody/Costura in the past. Nuget install Install-Package Costura.Fody (https://www.nuget.org/packages/Costura.Fody/)
Description about what it does from the source:
Take all assemblies (and pdbs) that have been marked as "Copy Local" and embed them as resources in the target assembly.
Actually you are looking for a method to merge some assemblies and resources into one executable file.
Custora.Fody, ILMerge, Obfuscators with assembly merging feature are some examples that can be used for this purpose.
You can find some answers here:
Embedding DLLs in a compiled executable
How to merge multiple assemblies into one?
List of obfuscators for .NET
If this is not about security reason, I would create the directory structure as follow:
- root
└ Startup.bat
└ lib
└ DiagnosticsSwitch.exe
└ DiagnosticsSwitch.exe.config
└ DiagnosticsSwitch.exe.manifest
└ Interop.IWshRuntimeLibrary.dll
And inside the Startup.bat
"lib/DiagnosticsSwitch.exe"

C# Web Api/ MVC.net not loading assemblies properly

I'm new to C# and .net in general, and I need to use it to work with the SDK for a major piece of software we use.
I can add the assemblies they tell me to add in a console application and everything works fine. When I try to add them to any web application (either MVC or WebAPI), I keep getting errors saying other assemblies are missing. Eventually I just added every assembly/reference (not sure the correct term for this, pretty much just .dll files) in the sdk folder and now it is working. This definitely cannot be the correct way to do this.
Is there something I am missing that allows assemblies/references to load other assemblies/references, or is something else the cause of my issue?
UPDATE:
The exceptions are usually something like this:
Could not load file or assembly 'Server, Version=1.5.1.0, Culture=neutral, PublicKeyToken=d11ef57bba4acf91' or one of its dependencies. The system cannot find the file specified.
And it does not occur during build, only when the function is actually being called from the web application( in my case, since it's an api, when I visit the URL that returns my JSON data)
You can use a tool called "IL Disassembler" that is installed along with Visual Studio to see what other dependencies the SDK's Assembly depends on.
The install location may vary but mine is here...
"C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6 Tools\ildasm.exe"
Drag and drop the SDK's assembly that you are using onto the tool and click the manifest field. A dialog will open with the other assemblies that you'll need to include.
If your console application works but the web application does not then the exception should specify which assembly is missing.
Compare you console output directory with the output directory of you web build. Make sure the dll that is required is being deployed to the web directory you are running.
In visual studio with your web project find the assembly under references and select it. Make sure that the property "Copy Local" is set to TRUE.

Why does VS2010 allow for the concept of "include in project"?

I'm still learning the basics of how VS2010 sees the world. Apparently, you can optionally "include" a file in a project. I'm a bit confused by this: If a file is version-controlled, AND the file is within the project directory, shouldn't it implicitly be "included" in the project? If not, what's the use case where a version-controlled file in the project directory should NOT be included in the project?
=== Addition ===
Based on the answers I've gotten so far, maybe I should rephrased my question: What does it mean for a file to be "included" in a project?
A project needs to know about files in order for compilation and distribution to occur. Just because you have a file that's under source-control, doesn't mean that it will be compiled if the project is unaware of it.
Also, you may want to include files as part of a distribution package. We do this quite often for our web projects that we distribute using web app gallery.
Conversely, you could have documentation or sql scripts that you version control, but do not want them to be part of the project.
EDIT: In answer to your update, what it means for a file to be included in a project is that the file is actually added to the .csproj or .vbproj file and will be used during compilation and/or distribution. VS does differentiate if the file is Content or if it needs to Compile it. This can be seen by clicking on the file in Solution Explorer and looking at the Build Action property.
No, you don't want random files that happen to be in the project directory included in source control.
We do sometimes put documentation (pdfs) or drawings/schematics in the project folder and under version control but you don't need them inside the visual studio project (especially when they are not being distributed because they are for internal use only).
Excluding the file from your project can be useful if the file is related to the project but not necessarily needed in the solution.
Example
If I need some test XML for an application that i'm writing; that is designed to normally be pulling this from a WCF service, it can be useful to keep that file in the directory for a development environment where I use IO to get the XML for testing, but I don't necessarily want it in my solution which is source controlled.
When you exclude a file from a project is no longer compiled or embedded, then when you want to include it again you can do so without having lost your settings.
If you e.g. copy a file (containing a helpful class which want to have in your project) into a folder of your project, then you will see ... nothing. You have to check the option "Show all files" of the solution explorer and the copied file can be seen, but it is still "greyed out". No you can choose the menuitem Include in project and that file will be integrated in your project and a pending change (add) for your source control is added too. Visual Studio doesn't include all files it can find in the project folder automatically to the project - and that is a good feature.
One of my colleagues explained to me a scenario in which a version-controlled file should NOT be part of the project. Here's the idea:
A developer writes some new code.
The code is experimental, and not intended to be part of the normal build.
The easiest way to exclude the file from the build is to NOT include it in the project, but still version-control it.
This way, the file can be shared with other developers, but not break the build.

Categories

Resources