Namespace disappears when building RELEASE, but present on DEBUG - c#

The System.Windows.Interactivity namespace vanishes when I swap from DEBUG build to RELEASE build. My project fails to build (naturally), and gives the error and warning visible in the images below. The namespace simply disappears from the Object Browser as soon as I switch build modes, even though the reference is still present in the Solution Explorer.
What's going on here?! And how do I fix it?

The reason can be you have linked to the dll that is placed inside Debug folder. If link shows relative path like ../my.dll when this dll will be not accessible from Release folder. You can replace relative link by full. But better solution will be moving dll above Debug and Release folder in folders hierarchy:
place dll somewhere above Debug and Release folders
remove old dll from References
add new dll to References

Take a look to your build configurations; you can open the .csproj file with some good editor (Vim, Sublime, Notepad++) and take a search the reference you're missing. It is possible to reference different libraries depending on the build configuration, so maybe the reference of this library is not included in the "Build | Release" mode, or it is defined specifically in the "Build | Debug" configuration section instead of the common one.

Related

VS solution's props and target setting not copying files to output directory [duplicate]

I have a visual studio solution.
I have many projects in the solution.
There is one main project which acts as the start up and uses other projects.
There is one project say "ProjectX". Its reference is added to main project.
The ProjectX references another .NET dll (say abc.dll) that isn't part of the solution.
Now this abc.dll should be copied to bin/debug folder of main project, but it isn't getting copied there. Why is it not getting copied, any known reasons ?
I found that if ProjectX referenced the abc.dll but didn't directly use any of the types DEFINED in abc.dll, then abc.dll would NOT be copied to the main output folder. (It would be copied to the ProjectX output folder, to make it extra-confusing.)
So, if you're not explicitly using any of the types from abc.dll anywhere in ProjectX, then put a dummy declaration somewhere in one of the files in ProjectX.
AbcDll.AnyClass dummy006; // this will be enough to cause the DLL to be copied
You don't need to do this for every class -- just once will be enough to make the DLL copy and everything work as expected.
Addendum: Note that this may work for debug mode, but NOT for release. See #nvirth's answer for details.
Just a sidenote to Overlord Zurg's answer.
I've added the dummy reference this way, and it worked in Debug mode:
public class DummyClass
{
private static void Dummy()
{
var dummy = typeof(AbcDll.AnyClass);
}
}
But in Release mode, the dependent dll still did not get copied.
This worked however:
public class DummyClass
{
private static void Dummy()
{
Action<Type> noop = _ => {};
var dummy = typeof(AbcDll.AnyClass);
noop(dummy);
}
}
This infomation actually costed me hours to figure out, so I thought I share it.
Yes, you'll need to set Copy Local to true. However, I'm pretty sure you'll also need to reference that assembly from the main project and set Copy Local to true as well - it doesn't just get copied from a dependent assembly.
You can get to the Copy Local property by clicking on the assembly under References and pressing F4.
It looks slick when you make it an assembly attribute
[AttributeUsage(AttributeTargets.Assembly)]
public class ForceAssemblyReference: Attribute
{
public ForceAssemblyReference(Type forcedType)
{
//not sure if these two lines are required since
//the type is passed to constructor as parameter,
//thus effectively being used
Action<Type> noop = _ => { };
noop(forcedType);
}
}
The usage will be:
[assembly: ForceAssemblyReference(typeof(AbcDll.AnyClass))]
Ran into this same issue. Background info: before building, I had added a new Project X to the solution. Project Y depended on Project X and Project A, B, C depended on Project Y.
Build errors were that Project A, B, C, Y, and X dlls could not be found.
Root cause was that newly created Project X targeted .NET 4.5 while the rest of the solution projects targeted .NET 4.5.1. Project X didn't build causing the rest of the Projects to not build either.
Make sure any newly added Projects target the same .NET version as the rest of the solution.
Not sure if this helps but for me, many times I reference a DLL (which automatically adds it to the bin folder of course). However that DLL might need additional DLLs (depending on what functions I'm using). I do NOT want to reference those in my Project because they just simply need to end up in the same folder as the DLL I am actually using.
I accomplish this in Visual Studio by "Adding an existing file". You should be able to add it anywhere except the Add_data folder. personally I just add it to the root.
Then change the properties of that file to ...
Build Action = None (having this set to something like Content actually copies the "root" version to the root, plus a copy in the Bin).
Copy to output folder = Copy if Newer (Basically puts it in the BIN folder only if it is missing, but doesn't do it after that)
When I publish.. my added DLL's only exists in the BIN folder and nowhere else in the Publish location (which is what I want).
You could also check to make sure the DLLs you're looking for aren't included in the GAC. I believe Visual Studio is being smart about not copying those files if it already exists in the GAC on the build machine.
I recently ran in this situation where I'd been testing an SSIS package that needed assemblies to exist in the GAC. I'd since forgotten that and was wondering why those DLLs weren't coming out during a build.
To check what's in the GAC (from a Visual Studio Developer Command Prompt):
gacutil -l
Or output to a file to make it easier to read:
gacutil -l > output.txt
notepad.exe output.txt
To remove an assembly:
gacutil -u MyProjectAssemblyName
I should also note, that once I removed the files from the GAC they were correctly output in the \bin directory after a build (Even for assemblies that were not directly referenced in the root project). This was on Visual Studio 2013 Update 5.
If you right Click the referenced assembly, you will see a property called Copy Local. If Copy Local is set to true, then the assembly should be included in the bin. However, there seams to be a problem with Visual studio, that sometimes it does not include the referenced dll in the bin folder... this is the workaround that worked for me:
In my case, it was the stupidest thing, caused by a default behavior of TFS/VS that I disagree with.
Since adding the dll as a reference to the main project did not work, I decided to add it as an "Existing Item", with Copy Local = Always. Even then the file was not there.
Turns out that, even though the file is present on the VS Solution and everything compiled both locally and on the server, VS/TFS did not add actually add the file to source control. It was not included on the "Pending Changes" at all. I had to manually go to the Source Control Explorer and explicitly click on the "Add items to folder" icon.
Stupid because I've been developing for 15 years in VS. I've run into this before, I just did not remember and somehow I missed it because everything still compiled because of the file being a regular reference, but the file that was added as Existing Item was not being copied because it did not exist on the source control server.
I hope this saves someone some time, since I lost 2 days of my life to this.
Issue:
Encountered with a similar issue for a NuGet package DLL (Newtonsoft.json.dll) where the build output doesn't include the referenced DLL. But the compilation goes thru fine.
Fix:
Go through your projects in a text editor and look for references with "Private" tags in them. Like True or False. “Private” is a synonym for “Copy Local.” Somewhere in the actions, MSBuild is taking to locate dependencies, it’s finding your dependency somewhere else and deciding not to copy it.
So, go through each .csproj/.vbproj file and remove the tags manually. Rebuild, and everything works in both Visual Studio and MSBuild. Once you’ve got that working, you can go back in and update the to where you think they need to be.
Reference:
https://www.paraesthesia.com/archive/2008/02/13/what-to-do-if-copy-local-works-in-vs-but.aspx/
Make sure that the dependent DLL used by you does not have target .NET Framework higher than the target .NET framework of your project's Application.
You can check this by selecting your project, then press ALT+ENTER, then select Application from left side and then select Target Framework of your project.
Suppose,
dependent DLL Target Framework = 4.0 and
Application DLL Target Framework = 3.5 then change this to 4.0
Thank you!
This is a slight tweak on nvirth's example
internal class DummyClass
{
private static void Dummy()
{
Noop(typeof(AbcDll.AnyClass));
}
private static void Noop(Type _) { }
}
I would do add it to Postbuild events to copy necessary libraries to the output directories. Something like XCopy pathtolibraries targetdirectory
You can find them on project properties -> Build Events.
TLDR; Visual Studio 2019 may simply need a restart.
I encountered this situation using projects based on Microsoft.NET.Sdk project.
<Project Sdk="Microsoft.NET.Sdk">
Specifically:
Project1: targets .netstandard2.1
references Microsoft.Extensions.Logging.Console via Nuget
Project2: targets .netstandard2.1
references Project1 via a Project reference
Project2Tests: targets .netcoreapp3.1
references Project2 via a Project reference
At test execution, I received the error messaging indicating that Microsoft.Extensions.Logging.Console could not be found, and it was indeed not in the output directory.
I decided to work around the issue by adding Microsoft.Extensions.Logging.Console to Project2, only to discover that Visual Studio's Nuget Manager did not list Microsoft.Extensions.Logging.Console as installed in Project1, despite it's presence in the Project1.csproj file.
A simple shut down and restart of Visual Studio resolved the problem without the need to add an extra reference. Perhaps this will save someone 45 minutes of lost productivity :-)
You may set both the main project and ProjectX's build output path to the same folder, then you can get all the dlls you need in that folder.
NO NEED FOR DUMMY IN CODE
Just :
add a Reference to the Executeable Project
or/and ensure that the reference in the executeable project has "Copy Local" set to TRUE (which was my "fault") is seems that this "overwrote" the setting in the base referenced library-project...
Other than the common ones above, I had a multi-project solution to publish. Apparently some files target different frameworks.
So my solution: Properties > Specific Version (False)
Add the DLL as an existing item to one of the projects and it should be sorted
VS2019 V16.6.3
For me the problem was somehow the main .proj file ended up with an entry like this for the project whose DLL wasn't getting copied to the parent project bin folder:
<ProjectReference Include="Project B.csproj">
<Project>{blah blah}</Project>
<Name>Project B</Name>
<Private>True</Private>
</ProjectReference>
I manually deleted the line <Private>True</Private> and the DLL was then copied to the main project bin folder on every build of the main project.
If you go to the reference of the problem project in the references folder of the main project, click it and view properties there is a "Copy Local" setting. The private tag equates to this setting, but for me for some reason changing copy local had no effect on the private tag in the .proj file.
Annoyingly I didn't change the copy local value for the reference, no idea how it got set that way and another day wasted tracking down a stupid problem with VS.
Thanks to all the other answers that helped zone me in on the cause.
HTH
I had a similar issue in which a DLL I had included in the project as content and 'Copy always' set, wasn't being copied to the bin folder. I solved this by adding a dependentAssembly reference to the DLL in the app.config.

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.

Referenced DLL in VisualStudio can't find its own DLL C#

I'm using an external DLL to handle PDF exports in a C# project. I've added the external DLL reference in Visual Studio, added appropriate using statements, Intellisense shows all sorts of methods and properties when I type out the namespace, and the same is reflected in the object explorer.
However, when I run the application, the program throws a run-time exception because my external DLL can't find another DLL that lives in the same directory as the external DLL.
To make matters more confusing, the program doesn't complain if I just copy every single DLL from the directory of the one I want to reference into the Debug folder of my program, but obviously I shouldn't have to do this.
Thanks in advance.
What you need is to check "copy to output directory", "always copy" in the properties of your DLL in VS.
Otherwise the DLL is not automatically copied in the output, and the program can not run.
I think this other question might help you: MSBuild doesn't copy references (DLL files) if using project dependencies in solution
Looks like your dependency has another reference you want to include into your drop. You have two options:
- Either add a reference to that implicit dependency as well (and set "Copy Local" = true)
or
- Add the dependent dll as a project item and mark it as "Copy to output directory" = true.

Setting up a project for release in visual studio 2013

I'm working on a C# project that is nearing release. As part of this, I have started building the project and testing it on another machine. This has revealed some odd problems. My biggest concern, though, is that my project is failing to run. I can do some basic things, but when I try to use my projects primary functionality it crashes. Using Visual Studio, I was able to determine the exception that was causing the crash.
Essentially, I'm getting a FileNotFoundException on the dll that contains most of my project's functional code. I'm not sure if I've made an error in adding the dll to my project, or if there's a problem in one of the files in the dll.
The dll was added as a reference using the Project -> Add Reerences feature of the user interface.
The dll contains three files which contain absolute file paths (these are for #import statements). Example follows.
#import "C:\Users\Me\Documents\Projects\MyProject\Delegates\bin\MyDelegate.tlb" raw_interfaces_only
My hang up is I'm not exactly sure what I'm doing wrong here. I suspect that those import statements are causing problems, but I'm not exactly sure how to fix them if they in fact are the problem. This is my first c#/c++ project so any help would be appreciated.
Adding the dll as a reference DOES NOT include the dll with your project--you are simply telling your project to use the library for your code. The dll will need to be installed on all computers that run your application, for your application to use the dll.
If the dll also uses three files (as you specified), then those files must also be included, and be installed in the expected path.
Presuming you have redistribution rights on the dll you mention, you can include the dll in your project. Be sure to set the "copy" property as "copy always" or "copy if newer" and change the reference to use the copy that ends up in you bin folder. Then you only need to be sure to include that dll and install it in the same folder as your application.

How to debug a referenced dll (having pdb)

I have two solutions in my workspace, say A and B.
Solution A is an older project which I finished coding some time ago.
In solution B, I need to use some classes from Solution A. To do so, I add a reference to the dll of one of the projects in solution A.
The problem is when I try to debug. I want to be able to step into A's code as well. Visual studio is not able to load the code for these classes ("There is no source code available for the current location.") and I can only view the disassembly, which is not useful.
The only way I know to debug classes from solution A is by running solution B, detach all processes (in the Debug menu item) and attach the process from solution A.
However, this is very inconvenient and I can only debug A OR B at once.
Is there a way to allow stepping into the code of referenced dlls (for which I do have the source code)?
Solution: My mistake was that I thought that a project can only be part of a single solution. In fact, a project can be part of any number of solutions.
When you need to reference the old project, you should simply add the project to the solution. This is done by right clicking the new solution in the Solution Explorer > Add > Existing Project.
Then, you'll be able to add the project reference. As others wrote, you should probably completely avoid using dll references to your own code (or other code you might need to change and debug).
A very good reference to how solutions should be designed can be found in MSDN.
If you have a project reference, it should work immediately.
If it is a file (dll) reference, you need the debugging symbols (the "pdb" file) to be in the same folder as the dll. Check that your projects are generating debug symbols (project properties => Build => Advanced => Output / Debug Info = full); and if you have copied the dll, put the pdb with it.
You can also load symbols directly in the IDE if you don't want to copy any files, but it is more work.
The easiest option is to use project references!
I had the same issue. He is what I found:
1) make sure all projects are using the same Framework (this is crucial!)
2) in Tools/Options>Debugging>General make sure "Enable Just My Code (Managed Only) is NOT ticked
3) in Tools/Options>Debugging>Symbols clear any cached symbols, untick and delete all folder locations under the "Symbols file (.pdb) locations" listbox except the default "Microsoft Symbol Servers" but still untick it too. Also delete any static paths in the "Cache symbols in this directory" textbox. Click the "Empty Symbols Cache" button. Finally make sure the "Only specified modules" radio button is ticked.
4) in the Build/Configuration Manager menu for all projects make sure the configuration is in Debug mode.
Step 1: Go to Tools-->Option-->Debugging
Step 2: Uncheck Enable Just My Code
Step 3: Uncheck Require source file exactly match with original Version
Step 4: Uncheck Step over Properties and Operators
Step 5: Go to Project properties-->Debug
Step 6: Check Enable native code debugging
Another point to keep in mind, be sure the referenced dlls are not installed in the GAC. After testing, I installed my dlls into the GAC to do system level testing. Later, when I had to debug my code again, I couldn't step into the referenced assemblies until I deleted them from the GAC.
I had the *.pdb files in the same folder and used the options from Arindam, but it still didn't work. Turns out I needed to enable Enable native code debugging which can be found under Project properties > Debug.
When you want to set a breakpoint in source code of a referenced dll, first make sure that you have a pdb file available for it. Then you can just open the related source code file and set a breakpoint over there. The source file does not need to be part of your solution.
As explained in How can I set a breakpoint in referenced code in Visual Studio?
You can review your breakpoints through the breakpoints window, available via Debug -> Windows -> Breakpoints.
This approach has the benefit that you are not required to add an existing project to your solution just for debugging purposes as leaving it out has saved me a lot of build time. Evidently, building a solution with only one project in it is much faster than building a solution with lots of them.
Make sure your DLL is not registered in the GAC. Visual Studio will use the version in the GAC and it will probably have no debugging information.
I don't want to include an external class library project in some of my solutions, so I step into assemblies that I consume in a different way.
My solutions have a "Common Assemblies" directory that contains my own DLLs from other projects. The DLLs that I reference also have their accompanying PDB files for debugging.
In order to debug and set breakpoints, I set a breakpoint in the consuming application's source where I'm calling a method or constructor from the assembly and then step INTO (F11) the method/constructor call.
The debugger will load the assembly's source file in VS and new breakpoints inside of the assembly can be set at that point.
It's not straight forward but works if you don't want to include a new project reference and simply want to reference a shared assembly instead.
The most straigh forward way I found using VisualStudio 2019 to debug an external library to which you are referencing in NuGet, is by taking the following steps:
Tools > Options > Debugging > General > Untick 'Enable Just My Code'
Go to Assembly Explorer > Open from NuGet Packages Cache
Type the NuGet package name you want to debug in the search field & click 'OK'
From the Assembly Explorer, right-click on the assembly imported and select 'Generate
Pdb'
Select a custom path where you want to save the .PDB file and the framework you want
this to be generated for
Copy the .PDB file from the folder generated to your Debug folder and you can now set
breakpoints on this assembly's library code
The following solution worked for me. It involves copy pasting the .dll and .pdb files properly from project A to B:
https://stackoverflow.com/a/16546777/5351410
It must work. I used to debug a .exe file and a dll at the same time !
What I suggest is
1) Include the path of the dll in your B project,
2) Then compile in debug your A project
3) Control that the path points on the A dll and de pdb file....
4)After that you start in debug the B project and if all is ok, you will be able to debug in both projects !
Visual Studio 2022 added a new top-level node: External Sources to solution explorer, which you will find while in debug mode. You can look at all the loaded dlls from there. You can also look at the loaded modules from Debug -> Windows -> Modules in debug mode. From there, right click on your desired dll, and click open file location, and then copy the pdb file to that location. This should allow you to step into any methods of the external dll from the same visual studio window.
Reference: https://devblogs.microsoft.com/visualstudio/debugging-external-sources-with-visual-studio/

Categories