could not load file or assembly 'newtonsoft.json' - c#

am create a desktop application using wpf. am installd newtonsoft.json(using package manager) for json parsing. after am successfully build and run my application.
then iam copy the appliction.exe from project source > bin > debug to my Destop. then am attempt to run exe from Desktop, but i got an error message
like this
could not load file or assembly 'newtonsoft.json' ..... system cannot find
the path
what is the issue? any missing

The exe needs to be able to link to the dll when you run it. That's why it works in your debug folder (the newtonsoft dll is there if you look), while it's presumably not in your desktop.
You can either:
1) Make sure the dll is included with the exe (copy it to your desktop, for example). If you distribute the exe in a zip file, just include the dll. If you use an installer, make sure it also installs the dll to the same folder.
OR
2) ILMerge the DLL directly into your exe - this means the exe contains the entire DLL and will always be able to find it. There are NuGet packages that can do this for you autonatically. Try adding "MSBuild.ILMerge.Task" via NuGet, and then build your project again.
(There are other solutions but they're generally terrible, like PATH, so I'm not going to explain how they work).
Personally, I'd usually recommend the former - just include the DLL. Look inside folders where you have software on your PC (e.g. most folders in Program Files) - you'll see that's how it's usually done, with DLLs installed as separate files. ILMerge can get messy if you don't know what you're doing and you start doing weird things with your DLLs.

Related

Unpredictable System.DllNotFoundException

I downloaded a package from SourceForge, PlanEph, which has 64 and 32 bit DLLs for C#. I got the 32 bit included C# demo to work by putting the DLL in my bin/Debug directory (I'm using Visual Studio 2015 Community) and adding the DLL as a reference.
Then I tried to make my own version of the demo in a separate solution, and got the System.DllNotFoundException. Various experimentation lead me to believe I can't have two identical namespace names anywhere in my Visual Studio installation, so I erased everything and started over.
I made a directory C\GJAbin, put the DLL in it, and added it to the system Path variable. I also put a helloWorld type program in that dir and executed it from the command line to verify the directory really was in the path. Then I recreated the demo solution, added the DLL as a resource, and built the solution "successfully". Then I ran it and got the System.DllNotFoundException.
So I can't understand why the DLL is being found when compiling but not at run time.
Go to project settings, go to "publish" tab and on the top most button (labeled something like "application files"). Chose "Show all files" checkbox if you don't see your DLL. Set the DLL's publish status to "Include" (NOT "Include (Auto)"!!) and publish it again.
Now the DLL should be inside the publish folder.
So I can't understand why the DLL is being found when compiling but not at run time.
Locating the assembly at compile time is done differently (by MSBuild) than at runtime (by the CLR).
At compile time, MSBuild has specific search paths that it knows about, or in most cases like this, there will be something in your project file telling MSBuild where to look for the file. Usually the <Reference> has a <HintPath>.
At runtime, the CLR will attempt to find the assembly from its own set of well-known paths. It will look in your app's config file (if applicable), then in the Global Assembly Cache (GAC), then in your app's root directory. Much more detail on this is available here.
You can tell MSBuild to copy the reference to your build output directory (usually the same as your app root directory when running). In VS, you can do this by selecting the reference and looking at the Properties tool window (or press F4 by default). Set the CopyLocal state to True. In the project file, this will add a <Private>True</Private> on the <Reference>.
You can also add the assembly to the GAC using the gacutil tool, but this does make it harder if you want to share your app with others. Usually it's preferable to keep a copy in your app root directory.
If it's still not working, you can also see the log for how the runtime is trying to find this assembly. The fuslogvw.exe tool in the Windows SDK (you can run it from the VS command prompt and it will be on the %PATH%) allows you to enable logging for assembly loads. You do need to be able to run this as an administrator to configure the setting.
As you can see in the screenshot, you can either log the results in the exception (so that you can see it while debugging), or you can log it to a file on disk (so you can see it whenenver).
The problem turned out to be an unfortunate interaction among the way the author chose names and the way Visual Studio displays information and error messages. The author created a c# dll Astronomy.PlanEph32.dll containing a namespace PlanEph32, which which was really just a wrapper for the c dll PlanEph32.dll. So all the error messages about not being able to load PlanEph32.dll were referring to not finding the c dll; the c# dll was being found just fine.

Add Windows Media Player Dll to EXE

I built an application that used some extrnal libraries, Like Tag-lib, Naudio and Windows Media Player.
So they add me to my solution (in the same folder as my EXE file), some DLLs.
I wanted to embed those DLLs to my EXE file, so I looked at the internet and found some options.
The best answer I found was this:
Embedding DLLs in a compiled executable
And it worked, but not for all of the DLLs.
It successfully embedded Naudio and Tag-Lib, But Unabled to embed Windows Media Player's DLLs.
Can anyone help me embed the Windows Media Player's DLLs to my application's EXE?
Update:
After some help (Thanks Mathieu Wybrecht) it worked.
I did everything that he said, but still it isn't working well.
The EXE file work perfectlly, I can move him and it will work.
But when I'm in the project's solution, it error me about the missing Dlls (The Dlls of WMP that Costura.Fody just embed...), and then I copy the Dll again to the folder and the error gone. It seccessed to build the solution, rebuild the solution or starting the program, the DLLs Disapear again (embed to the EXE) and the error comes up again...
Your question is not clear. What do you mean by "when I started to use Windows Media Player, it added a DLL file, and now it doesn't ..."
Where did "it" add a DLL file?
What is "it" ? Your application? Windows Media Player?
What DLL was added?
If the problem is that you want to embed one more DLL in your exe, follow the how-to you found about Costura.Fody.
If the problem occurs at run time, ensure that all embedded DLL don't try to load more dependencies. You can check for their dependencies using "Dependency Walker", it exists for x86 and x64 platform.
Edit: you edited your question, and now I'm back to edit my answer too :)
So, you succeeded to embed few DLL but not every one of them. It can be related to some of following reasons:
Some of them are native DLL (you have to follow a particular process to embed) : you may refer to Fody/Costura unmanaged assemblies and Fody/Costura Native Libraries
For some of them, Reference in your project are maybe not marked as "Copy Local = true" and/or not considered as "Embeded Resources" for some reason
Maybe your library is correctly embedded but still present in your output directory for you may not have installed the clean-output target ?
take a look at Clean output directory
Or you may face another problem that is more specific, try to get a look at Fody/Costura issues, you may find a reason for which your desired libraries are not embedded.

Unable to load DLL System.Data.SQLite.dll after it is merged into one assembly

To easily distribute the software to clients, I have to use Ilmerge to combine everything into single exe file, including some dll files e.g. System.Data.SQLite.
When I tried to run the merged exe file, quickly I ran into one issue, it shows the error message "Unable to load DLL System.Data.SQLite.dll. The specified module could not be found." This could be solved by putting this dll file same directory with exe file.
However, this is not an ideal way for me. I had tried to browse my exe file and I am pretty sure that System.Data.SQLite already embed inside. The result I get from google is because System.Data.SQLite is not managed code and cannot be mixed
I have tested csharp-sqlite, but that's look like totally different library for me. Is there any quick way to solve my problem?
I used Inno Setup before and I added System.Data.SQLite.dll as well. Give it try using Inno Setup and see if you will run into the same problem.

Combine .NET external dlls to a single executable file

This is a simple question. I just can't run my program if the Newtonsoft.Json.dll is not in the program folder. Why this? I've tried adding the reference, added the file to the project root, added to the resources folder, but nothing worked. How to run the program without the Newtonsoft.Json.dll in the program folder? I'm developing in a Windows Form Application.
UPDATE
Problem solved, thanks to spender for introducing me the ILMerge, a really really nice NuGet package that can combine third party dlls to a single executable binary file. For who wants to make a standalone application, just use ILMerge. Rapid, easy and extremely useful. See ya!
Usually, if your program uses a DLL, then you'll need that DLL in the app folder (or in the user path, or the GAC).
The conventional method of distributing multiple files is with an installer. You can write one using either WiX or the VS Installer Projects extension. Now all your output files get installed in one go along with all the other goodness that comes with an installed program. I have a strong preference for this method.
However, there are alternatives:
Download the source and copy the source into your main project, then it will be compiled into your main assembly (make sure you check that this is permitted by the license).
Use ILMerge to combine all your assemblies into a single binary.
If you don't want to reference dll in your program, you can install it to GAC on client machine but I don't understand which the context you want
If you just need some JSON serialization. Can you switch out your functionality with the JavascriptSerializer class which is installed with .Net?
http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer(v=vs.90).aspx

A problem regarding dll inheritance

I have created a dll that will be used by multiple applications, and have created an installer package that installs it to the program files, as well as adds it to the Global Assembly Cache.
The dll itself uses log4net, and requires a xml file for the logging definitions.
Therefore when the installer is run, the following files get copied to the install directory within program files:
The main dll that I developed
- The Log4Net.dll
- the Log4Net.xml file
I am now experiencing a problem. I have created a test console application for experimentation. I have added my dll as a reference, and set the 'local copy' flag to false.
When I compile the test console exe however, I noticed that it has copied the log4net.dll and log4net.xml files to the bin directory. And when running the test console, it appears that it will only work if the log4net.dll is in the same directory as the exe. This is dispite the fact that the test console application does not use log4net, only the dll that was added as a reference does.
Is there some way to have it so that the log4net.dll & xml files used will be the ones that were installed to the program files, rather than any application needed to copy over local copies? The applications that will be using my dll will not be using log4net, only the dll that they are referencing uses it.
Many thanks
Don't install into the Global Assembly Cache! Even if your library dll is used by multiple applications each should have it's own local copy. Otherwise you get into a whole world of pain for saving a few KB of disk space.
Always copy the required dlls locally. If you are really sure that the application won't need it you can simply delete the unnessesary dlls later or don't include them in the installer. But if your application will call ANY reference there it will crash at runtime. So best option is to leave them there (after all they WERE referenced for a reason).
No, it's not possible (at least not without much efford) to have .Net load dlls from arbitrary locations on the disk. And it should be this way (look up DLL-hell if you want to know why).
I suspect your problem is the configuration. You must use fully qualified names if you want it to work from the GAC. As per the documentation at http://logging.apache.org/log4net/release/faq.html:
"When loading an assembly from the GAC the fully qualified assembly name, including the version, culture and public key must be specified. This is in the standard syntax supported by System.Type.GetType. See the next FAQ on how to get the version and public key for an assembly."
I managed to resolve this by adding Log4net.dll to the GAC as well. It will now run without needing a local copy the dll.
It does however require a local copy of the XML file, to correctly log.

Categories