Disabling delayed DLL load functionality in a .NET (C#) app - c#

I've got a .net windows service application that calls out to a bunch of other dlls of our own creation. However, all of these dlls were created for x86, and we've since moved on to Any CPU (on x64 environments). Sadly, thanks to .NET's delayed loading functionality, many of these dlls are not loaded unless we exercise some rare and somewhat complicated code paths. The net result? We get incorrect format exceptions days or weeks after deploying code.
I want to know if there's a way to force .NET to fully load all assemblies that it directly references so that I can see such incompatibilities without manually poring through the dozens of projects from which these dependent dlls were created, or worse, doing full regression tests to force all of the assemblies to be loaded.
Addendum: Even if there's an easier way to resolve my specific x86-dlls-in-a-x64-environment issue, I'm still interested in seeing if there's a way to force the environment to load all of its dependencies. You never know when it'll come in handy! :)
EDIT: These are all managed DLLS, and I actually have used reflection to triage the issue in production. However, in a development environment, it suffers from the same problem the other options have: manually traversing all of the DLLs in one way or another.

One way is to statically link the libraries using ILMerge to merge them into the executable itself. otherwise the whole point of DLLs is that they are not evaluated until they are used in code.
see The .NET equivalent of static libraries?
of course you could just run a diagnostic sequence on app load that touches some aspect of all your dlls, so at least you know on load what is there and what won't work.

Related

Could not load DLL or dependency for ASP.NET app

I am very new to the .NET framework, and have been stuck on this issue for a week now.
I've been given a C# codebase, that is composed of several sub-projects which make use of DLLs compiled in C++. Each of these projects are inter-related, as they reference each other (non-circular).
If I create a basic Windows Forms Application, I can call functions from these DLLs no problem, by adding a reference to one of these sub-projects. However, if I create a very basic ASP.NET web app (which builds and runs fine on its own), it will break as soon as I add a reference to the sub-project I need.
By breaking, I mean I am given this error:
Could not load file or assembly 'XXXXX.DLL' or one of its
dependencies. The specified module could not be found.
I have done a lot of reading on this, since it seems to be a fairly common error, but none of the attempted solutions have worked.
To generalize, my main question is: Given two projects (one a windows form C# application, and the other an ASP.NET web application), both located in the same directory referencing the same project/DLLS, why does the web application struggle in locating the proper DLLs and/or dependencies?
Thanks in advance!
In the .NET application, have you tried compiling the DLL's and copying them directly into your /bin directory? That way you're eliminating the dependency and any reference issues VS may be choking on.
Then be sure to remove the references from your project so it's only using what it finds directly in the /bin directory. See if that works for you.
Once you're sure the issue is truly the references vs the DLL's themselves, and you project builds completely, try adding the references back in....be sure to do a clean of the project, then a rebuild.
If that still doesn't work, make sure both projects are not in the same solution. I had this issue not too long ago and it only seemed to work when I removed the other projects from the solution, opened another instance of VS2015 and then I was able to get everything working.
I know, I know....not really an answer if that ends up working, but when it comes to Microsoft's products sometimes we need to get a little creative. Let me know if this helps.

C# multiple managed dependencies failing when not run with optimizations

I've found this utterly hilarious bug when I've been trying to debug my WiX installer's custom actions.
I didn't write the code, but somehow one of my three managed .dlls decides it is invisible to the main .dll, that's referenced and called by WiX, when I turn optimisations off (to debug) for the main .dll.
As I understand, multiple dependencies in WiX are difficult or impossible without using a tool like ILMerge and I'll probably end up splitting everything down into their own separate .dlls and have loads of custom actions instead.
The main question here is: (out of pure curiosity) why are my dependencies loading properly when optimisations are enabled and not without them being enabled?

loading specific 32/64bit dll reference to project issue

I've been searching for a while, but appears my problem somewhat differs from the majority.
Here's the deal, I would like to make my program both 32 and 64bit compatible. Since being written in .NET it's not a problem, however I use an sqlite dll which is bit-specific.
In spite of the fact, that I have both versions of the dll, and I'm able to compile the main program for 64bit too, by re-adding the 64 bit version reference to the project, I'd like to make it work somehow differently.
Having to compile 2times is not efficient enough for me, instead, I'd like to find a way to make it dynamically adjustable: Given that the Dlls are the same, their contents:functions methods whatever, are the same, the only difference between them, is the bit version. Therefore, I would like to make my program's compatibility depend on the dll laying next to it.
Since normally it's added in the project as reference, Visual studio detects its contents by highlighting, I'd like to somehow keep it added as reference, BUT make the actual dll (which sits next to the exe) load, without losing the ability to use the dll functions the way I've done so far, in the editor, with highlightion.
I've read about Assembly.Load/LoadFrom/LoadFile, + Reflection, but it's not quite what I need.
Thank's in advance,
David
I know you have already done a lot of searching, but these two links really provide two great options for you, that you may have missed.
Import external dll based on 64bit or 32bit OS
Trying to not need two separate solutions for x86 and x64 program
In both links, look at Hans Passant answers. There's great.
Hope this helps.

.net library (dll) optimization

I am trying to using PDF Library ITextSharp, in my project. ITextSharp has so many features, i am not using even 5% of it. I am just wondering, why i should ship the complete dll file, when i use only 5% of it.
Is there any way to statically link the library to my project and remove all unused methods,features from the library ?
Disclaimer: I work for the company whose product I'm going to mention. I do know that other tools can do this as well but I only have direct experience with this one.
It is not necessary to modify any of the ITextSharp source or to perform your own custom build. It is possible to achieve all that you need with just the assemblies in your bin directory.
You can use Dotfuscator (the Removal option) to perform static analysis of the entirety of your application and output an assembly that only contains code that is actually used in your app. In addition you can use the Linking feature to link the DLL into your exe so that you are only shipping one file to the customer. This can result in a significantly smaller application footprint. You can take advantage of all of this functionality even if you choose not to use the obfuscation features that make you application harder to crack and reverse engineer.
Dotfuscator can be added into your build process in a number of ways, we integrate directly into Visual Studio (versions 2002 through 2010) so that you can just build your solution, there is also an MSBuild task for using on a Team Build server (if you choose not to have the build server build your solution), as well as a command line version for any other build system.
Dotfuscator will work on any .NET assembly type from including Silverlight assemblies.
These features are only available in the Pro version of Dotfuscator, if you contact PreEmptive Solutions we can set you up with a free, time limited evaluation so you can see how the product works for you.
If you just want to perform linking of assemblies there is also the ILMerge utility from Microsoft Research that will link multiple .NET assemblies into a single assembly.
There is no benefit of static linking with .NET dlls.
Its not easy to judge that you are using only 5% of the library, library may be using so much of internal code inside it to do lots of small small things that may not be seen by naked eye.
However iTextSharp is open source, you can create striped down version of iTextSharp to ship with your project.
By the way iTextSharp is smaller quite compared to earlier dlls required on non .net days.
Without modifying the source code and building your own .dll, no there is no way to not ship the entire thing. Additionally, if you wanted to head the route of creating your own modified .dll, please be aware of any possible licensing issues involved (I don't know if there are any, but it's certainly something you should be aware of). And finally, I would add, I don't know how big the iTextSharp .dll is, but really ask yourself if however much space it takes up actually matters.
If you want to reduce the size you have two options an obfuscator or an assembly compressor.
I haven't used any obfuscator for .Net just for Java so I can't recommend you any but they do what you pretend: remove unused methods and classes.
Or you can create a single compressed executable with all the needed assemblies that is auto decompressed when started like the popular Aspack and UPX do for windows executables http://en.wikipedia.org/wiki/Executable_compression . I have tried .NetZ and was happy with the results.
In this type of situation, your best bet is the site where you downloaded the code.
In many cases they use conditional compilation to include/exclude certain pieces. If the code isn't written with conditional compilation in mind, it will be "difficult" to do it yourself.
I personally would not recompile the source, unless there is a bug that needs to be fixed and you cannot wait for a new release. The time spent on the changes is time lost on your main project (the one you're getting paid for).

handling dependencies to a frequently-changing DLL

we have a number of c# projects that all depend on a common DLL (also c#). this DLL changes somewhat frequency as we're in an environment where we need to be able to build and deploy updated code frequently. the problem with this is, if one of these updates requires a change to the common DLL, we end up with several client apps that all have slightly different versions of the DLL.
we're trying to figure out how to improve this setup, so we can guarantee that anyone can check out a project (we use SVN) and be able to build it without issue. it seems like tagging each and every DLL change to a new version is excessive, but I'm not sure what the "right" solution is (or at least something elegant).
it would be ideal if any solution allowed a developer to be able to step into the DLL code from visual studio, which only seems to be possible if you have the project itself on your machine (might be wrong there).
Frankly, I think versioning your DLL in source control is the RIGHT solution.
It's very possible that a quickly changing core DLL could cause binary and source compatibility in a client project. By versioning, you can prevent each project from using the new DLL until you're ready to migrate.
This provides a level of safety - you know you won't break a client's project because somebody else changed something underneath you, but it's very easy to upgrade at any time to the new version.
Versioning in SVN is trivial - so I wouldn't let this be an issue. It's also safer from a business perspective, since you have a very clear "paper trail" of what version was used with a given deliverable of a client project, which has many benefits in terms of billing, tracking, testability, etc.
There's no easy solution - and the previous two answers are possibly the more accepted method of achieving what you want. If you had a CI environment, and were able to roll out all of your apps on-demand from a store that was built via CI, then you could avoid this problem. That can be lofty ambition, though, if there are old apps in there not governed by tests etc.
If your application is .Net 3.5 (might even need the SP1 too) then did you know that assemblies that are loaded from the network now no longer have any trust restrictions? This means that you could configure an assembly path on the machines in question to point to a shared, highly available, network location - and have all of your apps locate the assembly from there.
An alternative to this, but which would achieve the same goal, would be to build a component that hooks into the AppDomain.CurrentDomain.AssemblyResolve event - which is fired whenever the runtime can't auto-discover an assembly - and do a manual search in that network location for the dll in question (if you were to take the AssemblyName portion of the Full Name, append .dll to it, then you'd be reproducing the same search that the .Net Fusion binder performs anyway).
Just a thought ;)
I think you could benefit from setting up a continuous integration server with targets for each of the client projects and the common DLL project.
This way you'll immediately know when changes in the common DLL breaks any of the client projects. It could reduce the trouble of updating client projects when common DLL's interface changes. This solution might be inadequate if you development team is distributed and very large.
I wouldn't say there is a RIGHT solution though. There are many ways to manage dependency problems.
You could also have a look at Maven. It will help you set up project dependencies. Not sure how you can integrate Maven into Visual Studio though. Maven will allow you to specify which version of a project (in SVN) you want to depend on. Developers will then be able to checkout the correct project version and build their projects. Maven will checkout the correct version of the dependent projects from SVN for them. I haven't work with it myself, but a lot of open source projects in the Java community uses it.

Categories

Resources