Windows Form Designer: Could not load file or assembly - visual-studio

Has anyone ever had the issue where trying to "View Designer" on a windows form in Visual Studio .NET causes the error: "Could not load file or assembly…" ?
In this case, the assembly in question was XYZ.dll. I managed to fix this by adding XYZ.dll and all its references to my project's references (even though my project doesn't directly depend on them) and rebuilding the whole solution. However, after that, I removed all those references from my project, rebuilt, and it still worked.
One other piece of information is that I use Resharper 2.5. Someone else pointed out that it might be Resharper doing some shadow copying. I'll look into this next time this happens.
Does anyone have a understanding of why this error happens in the first place, and possibly the 'correct' way to fix it?

We have same problem. Some Form/UserControl classes can not be viewed in designer and Visual Studio causes various exceptions.
There are one typical cause:
One of designed component thrown unhandled exception during initialization ( in constructor or in Load event or before ).
Not only for this case, you can run another instance of visual studio, open/create some independent project, go to menu -> Debug -> Attach to process ... -> select instance of devenv.exe process with problematic designer. Then press Ctrl+Alt+E, the "Exceptions" windows should be shown. There check "Thrown" in categories of exception.
Now active the visual studio with designer and try view designer. If the exception will be thrown, you will see callstack ( and maybe source code, if the exception was thrown from your code ) and other typical information about thrown exception. This information may be very helpful.
If you have something like TypeLoadException from Winforms designer, when debugging Visual Studio (devenv.exe process) with another instance of Visual Studio, have a look at the Debug > Modules panel to see exactly which version of your DLL is loaded. Turned out that it was an unexpected version for us, hence the issue.

This is an old question that still appears to have no answer, either here or in the wider forum pool, most advice relates to relentless clean>rebuilds or close>clean folders>reopen or restarting the machine. I don't have a solid answer at present though have done some research into it and thought I might share. Summarily, there is one location into which all designer files are copied when a control or form is designed, another location which old files can exist and a method is described to catch all designer exceptions before the designer can generate the error page.
There appears to be two cases where either an assembly cant be loaded or can't be found. The first is caused by files failing to copy to designer-required locations, the second is outdated files being left behind.
As mentioned above files can fail to copy when a project fails to directly reference all references required by its referenced references and their references, recursively, down to the framework. This can be alleviated by carefully tracking all references and their dependents, ensuring all are accounted for.
The Visual Studio designer uses a specific location to cache dlls for its use in the designer, isolated from the source /bin folders of the projects:
Windows XP:
C:\Documents and Settings\[user_name]\Local Settings\Application Data\Microsoft\VisualStudio\10.0\ProjectAssemblies
Windows 7:
C:\Users\[user_name]\AppData\Local\Microsoft\VisualStudio\10.0\ProjectAssemblies
In this location, compiled assemblies are copied to dynamically created folders, one folder per assembly. Checking the assembly version dates on this location, it seems to be quite up to date, being deleted when visual studio exits. All assemblies are copied when a designer is viewed with newly compiled files. A new copy of each assembly is made into this location for each designer, so the location may hold multiple identical copies of each assembly.
One other location exists however where assemblies may be copied, and is a part of the assembly search sequence, apparently ahead of the ProjectAssemblies folder and that is in:
C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE
I have no knowledge of how or when assemblies get copied to this location, but it is not often so what files do arrive here quickly become a source of outdated references. When a designer failed with the 'Failed to load file or assembly' error, the version sought by the designer was a version only referenced by the assembly at this location.
This was discovered by using a second Visual Studio instance debugging on the first, with all .net symbols loaded, and all known exceptions breaking on throw as opposed to when unhandled. This allowed the second instance to intercept the handled designer exceptions and reveal that file location. This was the resulting output of the designer error that I used:
=== Pre-bind state information ===
LOG: User = **************
LOG: DisplayName = ***********, Version=1.0.4275.22699, Culture=neutral, PublicKeyToken=null
(Fully-specified)
LOG: Appbase = file:///C:/Program Files/Microsoft Visual Studio 10.0/Common7/IDE/
LOG: Initial PrivatePath = NULL
Calling assembly : ***********, Version=1.0.4275.22699, Culture=neutral, PublicKeyToken=null.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\devenv.exe.Config
LOG: Using host configuration file:
LOG: Using machine configuration file from C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind).
LOG: The same bind was seen before, and was failed with hr = 0x80070002.

Delete ALL bin and obj directories for all the projects in the solution. Also delete the folders in C:\Users<User>\AppData\Local\Microsoft\VisualStudio\9.0\ProjectAssemblies. Use 9.0 for VS2008, 10.0 for VS2010 etc.

Was struggling with this issue for a few hours. Here's what I learned: CHECK IF THE DLL THE DESIGNER IS TRYING TO LOAD IS A 64-BIT DLL.
Turns out, well, obvious to me now, VS is a 32-bit application, therefore the VS Designer -- surprise! surprise! is also a 32-bit application so if you have a UserControl or other WinForms control that has a reference to a 64-BIT DLL -- THAT IS A BIG NO-NO which will cause your form not to render in the VS Designer and produce the could-not-load-file-or-assembly error. So the first thing you should do is make sure that the DLL the Designer is complaining about is NOT a 64-bit DLL.

Using VS 2005, I ran into this same problem. I performed the steps Chien listed in his original question, but it still didn't work until I closed VS and reopened the solution. Now the Designer view looks fine.

I guess this problem occurs for different reasons, but I thought I'd share my case anyway. I hope someone will find a clue to what's going on with their project.
My problem occured since Visual Studio (C# project) couldn't find the managed c++ dll and copy it to the location mentioned in J Collins post => the designer couldn't find the file. I noticed it wasn't copied there with the other DLL:s and found out that it had a different/non-standard output directory. Changing this to the standard made Visual Studio perform the copy.

It happened to me very frequently on VS2005, specially when adding custom controls to the winform. Usually I just needed to just rebuild, without needing to add extra references, or close and reopen VS.
There is no apparent cause for this, just VS bugs.

I had a similar problem.
In my case, I had a base form, which referenced a class in a mixed-mode dll (c++ managed wrapper to unmanaged library).
My derived form did not load correctly, giving the same error described above.
However, the following resolved the issue: http://support.microsoft.com/kb/967050
Build both the mixed-mode project and the ui project for Win32. Since VS is 32 bit, it cannot load x64 unmanaged code:
Clear the ProjectAssemblies folder (requires shutting down VS first)
When you reopen VS, the designer loads with no issues. Note that by default, C# projects are compiled as Any CPU which compiles to x64 on Windows x64.
Hope this helps someone.

I had this problem in a c++/cli project.
As other people have mentioned, apparently the Windows Form Designer instantiates some version of your Form/Usercontrol before rendering it.
If the Form Designer cannot instantiate the class for whatever reason, it will fail. So what I did was comment out the constructor of the offending Usercontrol, and rebuild my project.
This allowed me to use the Form Designer again.
Of course you could use this method to selectively comment out parts of the constructor until identifying the part that makes the Form Designer choke, and if possible fix it.

I'm using VS2005 and VS2013 and seen the same problem. Some Visual Studio form designers in my project work and others won't open in design mode. Some opening attempts even crash Visual Studio, before the error page appear, saying:
To prevent possible data loss before loading the designer, the
following errors must be resolved:
An observation:
If there are inherited components in the form, the designer might stop working
A pseudo code example of the observation:
...
using System.Windows.Forms; // UserControl
namespace MyNamespace
{
public class MyForm : Form
{
public MyForm()
{
InitializeComponent();
...
}
private void InitializeComponent()
{
//this.ctrl = new MyNamespace.MyCtrl(); // Inherited class
this.ctrl = new System.Windows.Forms.UserControl();
...
}
//private MyNamespace.MyCtrl myCtrl; // Inherited class
private UserControl ctrl;
}
public class MyCtrl : UserControl
{
...
}
}
In the non-pseudo-code implementation, I commented out the inherited component MyCtrl in MyForm, and instead used the base class UserControl. The Visual Studio Form Designer started working again!How to write a properly Visual Studio Form Designer -interacting, inherited component class in C# is beyond me. But, this observation might be a clue to someone, whom can work it out.

I concur with the Resharper comment. I'm running 4.1. I disabled it, restart VS2008, and tried the "Convert to Web Application" again, and it worked.

I've seen this happen in VS2005 for Window Forms, ASP.NET, and Compact Framework projects. The project I'm building has a dependency on another assembly in my solution, but complains that it can't load it when trying to generate the designer file.
I'm not sure on the exact cause, but this sometimes will happen after we bump up the version number of the assembly. For some reason Visual Studio won't see this assembly as "new" and won't drop the new version in the current project's bin/ folder. Most of the time it does though.
Deleting the bin/ folder (and the obj/ folder for good measure) of the project with the designer error, and then rebuilding, seems to make the hurt go away.

I'v faced with the same problem.
I'v removed the reference from the project and added again, and all works fine (looking in the ptoject file i saw that reference definition was changed, for ex. "SpecificVersion" tag was added and set to the "false").

I have found, with problems like this, and many others, the problem tends revolve around the .NET framework installation. Lots of times, like during a system crash, files can become corrupted esp. if you have virtual memory turned off. When files in the C:\WINDOWS\Microsoft.NET folder get corrupted, they don't work the way they should, since there are alot of these files, errors dont always happen. Some parts of a file might be ok and load, then others dont. Over the years I have found keeping a FULL backup of the Microsoft.NET folder in an archive that has some type of corruption protection works well for me. You would not believe the number of things that corrupted .NET files cause to go wrong. Just about every aspect of the IDE depends on parts of it as well as many other features. Of course, if you dont have a backup you should UNINSTALL ALL NET FRAMEWORK INSTALLATIONS (don't repair because this does not guarentee files being rewritten -- the files might pass checksum and length checks but still be corrupt). After uninstalling, reboot the system, ensure that the entire Microsoft.NET folder is deleted, if not, delete it yourself (I had to do this, some files still get left behind). Once this is done, reinstall the NET framework, depending on your OS, you might not be able to get rid of the whole thing. But with windows XP I know you can, i havent tested this on newer OSes youre on your own for that one as far as testing goes. I started out by installing 2.0, then 3.5 SP1, and so on, depending on which Visual Studio you are using. I stick with 2008 because its the fastest for me and still has support for some of the newer stuff like WPF, tr1, etc... hope this helps you an anyone else with .NET woes, the error messages are often misleading but for me 99% of the time it is Microsoft.NET file corruption.

To anyone who has this problem in the future and scrolled all the way down searching for it : Delete ComponentModelCache in Appdata/Local/Microsoft/VisualStudio/..

I have faced this issue several times. Most of the times clean+rebuild works (sometimes combined with restart of Visual studio).
Two times when clean+rebuild didn't work it was:
Issue #1
In one of the cases that I faced it had to do with C# and VB.NET.
I had several user controls in my Form which was not loading in designer. The user controls were in C#. Most of them were under the same namespace, but few of them had part of the namespace which didn't match in alphabet-case.
For example:
userContorl1 was in myapp.mynamespace1, and
userControl2 was in myapp.myNamespace1
For C# they are different namespaces as C# is case-sensitive. But VB.NET is case-insensitive. The error that I got was when trying to load myapp.mynamespace.userControl2. After struggling for long time, I noticed the namespace in error message and corrected in the user control, making them all same as 'myapp.myNamespace1', and viola designer opened after clean+rebuild.
Issue #2
My Form (which was not opening), had many user controls. One of the control was having a property of enum type. This enum was defined inside a generic class. The designer generated code fo this user control was something like:
myUserControl1.SomeType = somenamespace.SomeGenericClass(of Date).SomeEnum
The error that i got while opening designer, was like:
could not load type
somenamespace.SomeGenericClass[System.Date]+SomeEnum
I moved the enum outside the class and replaced the designer code to:
myUserControl1.SomeType = somenamespace.SomeEnum
And the designer opened. :)
I hope this helps somebody.

I will say the responses in this thread helped me somewhat, but didn't exactly nail down what was occurring in my custom user controls.
In my particular case, I have numerous helper class libraries that perform such things as styling on my controls, background logging, and just generic helper classes that perform routine things I do all the time.
I was using some of these other library static methods to perform logging in the case of error. Here's an example:
try
{
_InitializeStuff();
}
catch (Exception ex)
{
Logger.Instance.Log("Couldn't instantiate: " + ex.Messsage);
}
I did this in the Constructor, Load, and Property Set methods of my User Controls ... and the designer couldn't always build it's path to the static method calls, so the designer would fail.
I tried placing DesignMode checks around it, but the problem wasn't at runtime -- it was designtime, and the links couldn't be built. The only option for me was to remove all references to my static helper classes in the following places of my Custom User Controls:
Constructor
Load
Property Accessors
Trying to debug this with a secondary IDE and using Attach to Process did not work for me, unfortunately.

Also make sure you have a using declaration for the library with the control in your form or control. Once the designer knows about it, it write the full namespace in references to objects in the Form.designer.cs file.

I tried many of the suggestions above related to deleting files, rebuliding, restarting, etc. My problem was that it could not load a utility assembly that was used by a few projects in the solution. Another project had common controls. So, Form1 referenced ControlsAssembly1 referenced UtilityAssembly1. The .resx file had properties of types in UtilityAssembly1. I deleted the resources that contained those types. Tried to open the form again (got Null Reference exception because of the missing resource), hit Ignore and Continue and my problem was fixed.

In order to get your From back.
First of all go to Visual studio 2008 command prompt.
type devenv /resetsettings
type devenv /resetSkippkgs
In solution explorer click the "Show All files"
Now Open Form1.vb by double clicking, then click + to expand it.
Open Form1.Designer.vb
you can see both tab in you editor window (IDE).
Now Right Click tab "Form1.vb" and save it
Similarly Right click the tab Form1.vb [Design] and save it also
Re-Build your project.
Restart Visual Studio.

I have faced this problem. I did what is said above but it didn't make any sense. Then I added the assembly to the references. Rebuild the project. Closed the Visual Studio. Then reopen the screen and the designer appeared as normal.
Regards,

Just to Chime in on this. I build a new version of my UserControl whilst my other project was open and referencing it. When I went back to view the designer in the form referencing the user control, it said it couldn't find the .dll of a specific version.
I tried to remove the reference to the control and from the toolbox, with no luck. The code would compile just fine, but the designer wouldn't show without the error.
Tried all the above and it didn't work.
The .res file for the form has some XML:
<data name="EventBar1.EventCheckedSubscriptions" mimetype="application/x-microsoft.net.object.binary.base64">
<value>
AAEAAAD/////AQAAAAAAAAAMAgAAAJoBbXNjb3JsaWIsIFZlcnNpb249NC4wLjAuMCwgQ3VsdHVyZT1u
ZXV0cmFsLCBQdWJsaWNLZXlUb2tlbj1iNzdhNWM1NjE5MzRlMDg5XV0sIG1zY29ybGliLCBWZXJzaW9u
PTQuMC4wLjAsIEN1bHR1cmU9bmV1dHJhbCwgUHVibGljS2V5VG9rZW49Yjc3YTVjNTYxOTM0ZTA4OQUB
AAAANlN5c3RlbS5Db2xsZWN0aW9ucy5HZW5lcmljLkxpc3RgMVtbU3lzdGVtLkV2ZW50SGFuZGxlcgMA
AAAGX2l0ZW1zBV9zaXplCF92ZXJzaW9uAwAAFVN5c3RlbS5FdmVudEhhbmRsZXJbXQgIAgAAAAkDAAAA
AAAAAAAAAAAHAwAAAAABAAAAAAAAAAMTU3lzdGVtLkV2ZW50SGFuZGxlcgs=
</value>
</data>
<data name="EventBar1.EventLengthSubscriptions" mimetype="application/x-microsoft.net.object.binary.base64">
<value>
AAEAAAD/////AQAAAAAAAAAMAgAAAJoBbXNjb3JsaWIsIFZlcnNpb249NC4wLjAuMCwgQ3VsdHVyZT1u
ZXV0cmFsLCBQdWJsaWNLZXlUb2tlbj1iNzdhNWM1NjE5MzRlMDg5XV0sIG1zY29ybGliLCBWZXJzaW9u
PTQuMC4wLjAsIEN1bHR1cmU9bmV1dHJhbCwgUHVibGljS2V5VG9rZW49Yjc3YTVjNTYxOTM0ZTA4OQUB
AAAANlN5c3RlbS5Db2xsZWN0aW9ucy5HZW5lcmljLkxpc3RgMVtbU3lzdGVtLkV2ZW50SGFuZGxlcgMA
AAAGX2l0ZW1zBV9zaXplCF92ZXJzaW9uAwAAFVN5c3RlbS5FdmVudEhhbmRsZXJbXQgIAgAAAAkDAAAA
AAAAAAAAAAAHAwAAAAABAAAAAAAAAAMTU3lzdGVtLkV2ZW50SGFuZGxlcgs=
</value>
</data>
<data name="EventBar1.EventLengthTypes" mimetype="application/x-microsoft.net.object.binary.base64">
<value>
AAEAAAD/////AQAAAAAAAAAMAgAAAKABY3RybENhbGVuZGFyU2lkZUJhciwgVmVyc2lvbj0xLjAuNzEy
MS4yMTIzNCwgQ3VsdHVyZT1uZXV0cmFsLCBQdWJsaWNLZXlUb2tlbj1udWxsXV0sIG1zY29ybGliLCBW
ZXJzaW9uPTQuMC4wLjAsIEN1bHR1cmU9bmV1dHJhbCwgUHVibGljS2V5VG9rZW49Yjc3YTVjNTYxOTM0
ZTA4OQwDAAAAUWN0cmxDYWxlbmRhclNpZGVCYXIsIFZlcnNpb249MS4wLjcxMjEuMjEyMzQsIEN1bHR1
cmU9bmV1dHJhbCwgUHVibGljS2V5VG9rZW49bnVsbAUBAAAAT1N5c3RlbS5Db2xsZWN0aW9ucy5HZW5l
cmljLkxpc3RgMVtbY3RybENhbGVuZGFyU2lkZUJhci5FdmVudEJhcitFdmVudExlbmd0aFR5cGUDAAAA
Bl9pdGVtcwVfc2l6ZQhfdmVyc2lvbgQAAC5jdHJsQ2FsZW5kYXJTaWRlQmFyLkV2ZW50QmFyK0V2ZW50
TGVuZ3RoVHlwZVtdAwAAAAgIAgAAAAkEAAAAAAAAAAAAAAAHBAAAAAABAAAAAAAAAAQsY3RybENhbGVu
ZGFyU2lkZUJhci5FdmVudEJhcitFdmVudExlbmd0aFR5cGUDAAAACw==
</value>
</data>
<data name="EventBar1.EventSettingsSubscriptions" mimetype="application/x-microsoft.net.object.binary.base64">
<value>
AAEAAAD/////AQAAAAAAAAAMAgAAAJoBbXNjb3JsaWIsIFZlcnNpb249NC4wLjAuMCwgQ3VsdHVyZT1u
ZXV0cmFsLCBQdWJsaWNLZXlUb2tlbj1iNzdhNWM1NjE5MzRlMDg5XV0sIG1zY29ybGliLCBWZXJzaW9u
PTQuMC4wLjAsIEN1bHR1cmU9bmV1dHJhbCwgUHVibGljS2V5VG9rZW49Yjc3YTVjNTYxOTM0ZTA4OQUB
AAAANlN5c3RlbS5Db2xsZWN0aW9ucy5HZW5lcmljLkxpc3RgMVtbU3lzdGVtLkV2ZW50SGFuZGxlcgMA
AAAGX2l0ZW1zBV9zaXplCF92ZXJzaW9uAwAAFVN5c3RlbS5FdmVudEhhbmRsZXJbXQgIAgAAAAkDAAAA
AAAAAAAAAAAHAwAAAAABAAAAAAAAAAMTU3lzdGVtLkV2ZW50SGFuZGxlcgs=
</value>
</data>
I removed this from the .res file and all is well. I did backup the whole folder before I tried this though!

Related

How to handle legacy OCX files in a Visual Basic Project

I have inherited an old VBP project that is ostensibly usable in Visual Basic 6.
It has two .ocx files which, research seems to indicate, need to be registered with regsvr32. Loading VB6 onto a Windows XP installation seems to allow me to successfully register them both.
However, VB6 still chokes on them. When I load the project, two errors are logged:
Line 228: Class CvsInSightDisplayOcx.CvsInSightDisplay of control Camera1 was not a loaded control class.
Line 5841: Class Xtimer01.Xtimer of control Xtimer1 was not a loaded control class.
And, naturally, the project will not compile. What steps can I take to get these .ocx files to play well?
#Bob77's comment led me to a two part resolution.
1) One of the components needed to be formally installed. (Thankfully it was still available.)
2) Both needed to be manually removed from the .vdp file using Notepad, and added via the VB6 UI, through Project -> Components... and 'Browse...'

How to share VB project with another programmer overcoming the vbp "reference" issue?

I have this old VB6 project that is composed of a few DLLs, OCXs, and GUIs.
There is a GUI component that includes this in it's VBP file:
Type=Exe
Reference=*\G{00020430-0000-0000-C000-000000000046}#2.0#0#C:\Windows\SysWOW64\stdole2.tlb#OLE Automation
Object={EAB22AC0-30C1-11CF-A7EB-0000C05BAE0B}#1.1#0; ieframe.dll
Object={3050F1C5-98B5-11CF-BB82-00AA00BDCE0B}#4.0#0; mshtml.tlb
Reference=*\G{64E54C86-D847-48F7-9AE5-D6C9B8E6A3A2}#3.0#0#..\..\bin\Crypt.dll#Crypt
Reference=*\G{B3E7F95C-B6D9-458E-B4D4-5272759B139A}#4.0#0#..\..\bin\SpeechMike.dll#SpeechMike_DLL
Object={831FDD16-0C5C-11D2-A9FC-0000F8754DA1}#2.1#0; MSCOMCTL.OCX
Object={AB4F6C60-4898-11D2-9692-204C4F4F5020}#29.0#0; Ccrpsld.ocx
Object={48E59290-9880-11CF-9754-00AA00C00908}#1.0#0; msinet.ocx
Object={9C526969-AA6E-4D61-BAFA-117FD20C2B38}#3.0#0; SpeechMike.ocx
The Reference settings are a pain since they always change from one machine to the other. I mean, the GUID '9C526969-AA6E-4D61-BAFA-117FD20C2B38', for the last one as an example, will be something on my system, but something else on somebody else's machine.
For now, to make it work, I erase References to Crypt.dll and SpeechMike.dll. Also Object SpeechMike.ocx. Otherwise, Visual studio looks for something that does not exists. Then in "project > references" I check both Crypt and SpeechMike and the Reference goes back to the VBP with the proper GUID and version. Finally, in 'project > components' I add the OCX and I'm good to go.
Am I wrong about that? How can I share the project with some else without going through hoops and loops to start the project?
I'm using MS Visual Basic 6 (part of VS 6 enterprise).
This sounds like "failure to maintain binary compatibility." Normally you only do this to yourself, but of course it can be a bigger headache if multiple people are compiling your libraries from the source Project files.
When you create ActiveX EXEs, DLLs and OCXs you need to create a "base" version where type and class ID values (GUIDs) get assigned. The documentation even suggests that you do this leaving the procedures empty: just a comment line or something so the IDE does not remove the empty declarations.
You don't have to use an "empty" base reference library, it can be one with full code in it.
Once you have compiled this baseline library, you'd exit and save your Project. Then rename this "empty" library as something else and from there keep it along with your Project source files.
After this you re-open the Project and go into Project Properties and on the Component tab change the Compatibility setting to Binary Compatibility and in the box there enter the full path and name of your compiled baseline library. Save the Project. Now you can add code and compile the "real" library to be used by other programs.
When you distribute these libraries (DLLs, OCXs) to somebody else in source code form so that they can compile them you must provide this renamed compiled baseline library along with the source code files, VBP file, resource files, etc.
From there your GUIDs will be stable until you make a change to something that breaks binary compatibility (changing a method's argument list, etc.).
There is more detail on this in the online Help (MSDN Library). See:
Using Visual Basic|Component Tools Guide|Creating ActiveX Components|Debugging, Testing, and Deploying Components|Version Compatibility in ActiveX Components

Visual Studio breakpoints break in the wrong source file (or multiple files simultaneously) if multiple files have the same name

In a team project I'm working on, setting a breakpoint in a file (say IdeasController.cs) will lead to erratic debugger behaviour if there's another file with the same name in the solution. I've reproduced the problem on several developers' workstations.
Example
I set a breakpoint in IdeasController.cs in our Web API:
Another file called IdeasController.cs exists in our separate MVC 4 web project. In the screenshot below, the debugger shows the Api->IdeasController source code, but the line highlight matches the code structure of Web->IdeasController. The breakpoint is duplicated, with one of them in the middle of a comment block.
The Breakpoint window shows the breakpoint in both files simultaneously:
On some workstations the debugger steps through the correct lines (regardless of the line highlight); on others it cheerfully steps through irrelevant lines (including comments and whitespace). I'm guessing this depends on which source file it chooses to display.
What I've tried
I've trawled the Internet. This kind of problem seems to occur when there's a mismatch between the debug file (*.pdb), the source file, and the compiled code. There are a lot of possible causes: duplicate file names (which can confuse the debugger[5]), outdated project build files, invalid solution cache, or incorrect build configuration.
These are the solutions I've found and tried:
Checked my build configuration.
Made sure the project isn't built in release mode.
Made sure we don't have code optimization enabled.
Made sure the project's debug module was loaded correctly. (Started debugging the project and checked Debug > Windows > Modules. Both assemblies are listed, not optimized, and have a symbol status of "Symbols loaded".)
Reset the debugging metadata & Visual Studio cache.
Closed Visual Studio and deleted the solution cache file (*.suo).[1]
Deleted each project's build output (the bin and obj folders). (For future reference: open the solution folder in Windows Explorer and type this in the search box: "type:folder AND (name:=bin OR name:=obj)".
Deleted the assembly cache folder (C:\Documents and Settings\<user>\Local Settings\Application Data\dl3).[2][3]
None of these had any effect. I can rename one of the files (without renaming the class) to temporarily work around the problem, but that's far from ideal.
Where I am now
Page 14 of my latest Google search. Suggestions would be much appreciated. :)
If no better alternatives exist, you could put the breakpoint in code:
System.Diagnostics.Debugger.Break();
Just don't forget to remove it afterwards...
I'm so glad I found this post, thought I was the only one and was going insane! I'm having the same problem in VS2012 with VB.Net and have tried everything the OP mentioned.
Unique naming of the files seems to be the only 100% fix that I've found. Disabling all breakpoints until the application has loaded and then re-enabling the breakpoints you need works most of the time. Breakpoints in Lambda functions can still give you issues.
I just had the exact same problem. What solved it for me was deleting the .suo files belonging to the solution that contained the affected project/source file.
I also deleted my local symbolcache but I don't think that had anything to do with it.
(My solution contains multiple projects, one file (DataAdapter.cs) in one project was affected by this (VisualStudio put my breakpoints in the pdb belonging to System.Data.DataAdapter). I opened the .csproj file directly and was able to correctly set the breakpoint.)
I had the same problem today. I was able to trace it back to the fact that I had forgotten to set the platform target to x86 while debugging. Unfortunately the others (x64 / Any CPU) can be problematic while debugging. At least VS 2008 doesn't like them. I guess this is yet another reason to stay away.
Some speculation... I think the debugger (while running a 64 bit app) somehow "steals" breakpoints away from a file in certain cases. For me it was because another assembly was loaded first which had the same file name. I was able to avoid the issue, even in 64 bit mode, if I first manually loaded the assembly with my breakpoints: Assembly.Load("MyAssemblyWithBreakpoints");
Hope this (my first stackoverflow contribution) helps.
Although renaming one of the files will work, I found that the simplest solution is to temporarily disable automatic loading of symbols for the "other" assembly.
Start the debugger and continue until you hit the erroneous breakpoint.
Find where the debugger actually set the breakpoint using the Call Stack window:
Right-click on the row with the yellow arrow and enable Show Module Names. (The row should also have the red breakpoint symbol on it.)
The assembly name is now visible on that row.
Find that assembly in the Modules window (Debug > Windows > Modules).
Right-click on the assembly and disable Always Load Automatically.
Stop the debugger.
Start debugging again.
By doing this, you're preventing the Visual Studio debugger from mapping the breakpoint to the wrong assembly. It will then load the symbols from the other [presumably] correct assembly first, therefore mapping the breakpoint to the correct assembly.
Why does this happen?
This seems to occur when two different symbol files (PDB files) — for two different assemblies — both reference a source file with the same name. Although the source files are completely different, the Visual Studio debuggger seems to get confused.
For example, imagine there are two different files both with the name IdeasController.cs. The first one compiles into assembly Api.dll, and the second one compiles into assembly Web.dll.
When the debugger loads symbols, it will either load Api.pdb or Web.pdb first. Let's say it loads Api.pdb first. Then even if you set a breakpoint in Web\IdeasController.cs, it will find a match for IdeasController.cs in Api.pdb. It then maps code from Web\IdeasController.cs to Api.dll. This won't map correctly, of course, and so you see all sorts of odd issues while debugging.
I just had this issue on Visual Studio 2017 (Version 15.9.7), were break points were skipped and the debugger just "jumped" over return statements etc.
After a while I noticed, that I've recently added a .runsettings file to the project - and it turned out, that in my case configuring the CodeCoverage data collector is causing this problem.
As soon as I removed this section:
<DataCollector friendlyName="Code Coverage" uri="datacollector://Microsoft/CodeCoverage/2.0" assemblyQualifiedName="Microsoft.VisualStudio.Coverage.DynamicCoverageDataCollector, Microsoft.VisualStudio.TraceCollector, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"> ... </DataCollector>
from the .runsettings file, it worked like a charm again.
I just backed up and deleted the file and then added back to the project, that solved the problem. I just whish i did it before going through the beforementioned list :)
You may also try to Clean and Rebuild (not Build) all projects.
I was hitting this issue in Visual Studio 2015.
I had a sub-folder with a DLL I wanted to save as Version1. It seems even after removing the reference to that DLL, and then adding a reference to another project studio pulled in the existing reference and went to the wrong source file. I removed that DLL in the sub-folder then Studio got the correct source.
I found a helpful link on [MSDN that shows how to clear prior associated source files in studio at this link][1].
Summary:
In the Solution Explorer, right click on the solution name (ex: Solution ‘TestApplication’) and select Properties This will bring up the Solution Property Pages dialog
Under Common Properties, select Debug Source Files
In the Search these paths for source code files (Visual Studio .NET 2003) / Directories containing source code (Visual Studio 2005) box, add, remove and/or reorder the directories as desired
Click the OK button
I was having the same issue. In my case both the projects had same port numbers. I was able to resolve it by changing the port number of the project whose file's breakpoints were not hitting.
My guess is that IIS Express was caching the pdb file from the second project since both files had the same name, and the projects had the same port number.
What worked for me (VS2017) was disabling this option in Tools --> Options... --> Debugging --> General: "Require sources files to exactly match the original version", which is enabled by default but I had it turned on.
That was not enough though, I also had to manually remove obj and bin folders for all projects in solution.
Delete all the .pdb files of the project where the break point is hitting wrongly. This will solve the issue.
It happened to me (in VS 2008) to have two child breakpoint with the same memory address and the same associated file.
Those breakpoints were spawned at a certain time during the running of the process.
I noticed that I had duplicated .dll files in my project folders, and resolved removing the duplicated .dll, while keeping only one .dll per name in the debugging folder structure. (As example in my case I had /bin/Example.dll and /bin/Plug-in/Example.dll both present under my debug folder structure).
I had a very similar problem. In my case the problem was a different target .net framework in one of the projects causing VS2017 to wrongly load a source file (with the same name) of another project, not the one being activated with
ObjectHandle handle = Activator.CreateInstance
Changing the project's target framework to be the same in all projects fixed it.
I had a similar issue with the breakpoint being set in another file with the same filename in a different project.
It was caused by the fact that the debugging was started for that other project, while it was not started for the project where I tried to set the breakpoint. The breakpoint creation worked correctly after doing the Debug > Start New Instance for the intended project.

Importing / referencing an external Silverlight dll library from a workflow 4.0 Activity

I have created a Silverlight class library which holds a lot of the common utility methods I use day to day in my Silverlight development.
I am starting to play around with Workflow and would like to reuse this common dll. After referencing this dll in my workflow project I see a yellow warning icon beside it.
I can use the functionality from this dll when creating ‘Code Activities’ without issue. After adding the using statement for it all works AOK.
using EquinoxeAISManagementSystem.Common.Helpers;
BUT when I try to import the dll from the activity designer, I do not see the dll in the import window.
If I edit the XAML and add it directly, I get a warning.
Is it possible to reuse Silverlight dlls?
I believe I can answer my own question.
The project EquinoxeAISMAnagementSsystem.Common is part of another solution. I had added this single project for the other solution into my new workflow solution (which I was having this issue in).
When I added the reference to the EquinoxeAISMAnagementSsystem.Common.dll initilly, it was by referencing the project in the workflow solution.
This seems to be the cause of the issue (for workflow anyway).
I was able to correct this issue by adding the EquinoxeAISMAnagementSsystem.Common.dll to a common folder where I keep all my external dlls (for ease of use).
..\Projects\EquinoxeAISManagementSystem.DLL\
I set up the output destination folder for this EquinoxeAISMAnagementSsystem.Common project to the folder above. I was then able to add the reference to Common.dll by browsing directly to the folder above.
Hay presto, all works.
I’m not sure id this is the ‘correct’ way to do things, but I needed to get over this hump.
I would be interested in anyone’s comments and if there is a correct(er), why to fix this issue.
Thanks ….

Debugging across projects in VS2008?

We have a DLL which provides the data layer for several of our projects. Typically when debugging or adding a new feature to this library, I could run one of the projects and Step Into the function call and continue debugging code in the DLL project. For some reason, that is no longer working since we switched to Visual Studio 2008... It just treats the code from the other project as a DLL it has no visibility into, and reports an exception from whatever line it crashes on.
I can work around that by just testing in the DLL's project itself, but I'd really like to be able to step in and see how things are working with the "real" code like I used to be able to do.
Any thoughts on what might have happened?
Is the pdb file for the dll in the same directory as the dll? This should all work -- I do just this on a regular basis. Look in the Modules window which will show you whether it's managed to load symbols for the dll. If it hasn't then you won't be able to step into functions in that dll.
It sounds like you have "Just My Code" enabled and VS is considering the other projects to not be your code. Try the following
Tools -> Options -> Debugger
Uncheck "Just my Code"
Try again
I've gotten around this issue by opening a class that will be called in the project you need, placing a breakpoint, keep the file open, and run the debugger. The debugger will hit the breakpoint and the relative path that VS uses will be updated so that future classes will be opened automagically.

Resources