Outdated Project Assembly loaded when running in Experimental Instance - c#

I have two C# projects, A and B, in VisualStudio 2010. Project A is a class library. Project B is a ReSharper-Plugin project that references project A (ProjectReference). Both projects compile and worked property when I ran project A in the Experimental Instance.
Now, recently I renamed a static class, with some extension methods, in project A. Both projects still compile. But when I run project B I get an TypeLoadException for the new class name. When I change the class's name back, everything works fine again.
My guess is that project B is compiled against the new code, but run against an outdated dll. I tried everything from clear, over rebuild, to restart, but to no avail. I also manually checked that a fresh dll from project A is placed in project B's bin/Debug folder, which is the case. And I copied over the dll manually, after building project A. The problem persists.
Now I ask myself whether there is another location where the dlls are copied to before running the Experimental Instance. Is this the case? If so, where to? Are there any other places where the old version could come form? Or is there even something completely different that could cause the problem?
Thanks in advance!

Ok. Did you reference the DLL or the Project? In a Visual Studio Solution you can reference projects. If you reference project A in project B then B should always use the latest code from project A.

I managed to figure it out myself, thanks to the right keywords on Google (MissingMethodException and ProjectReference) that led me to a blogpost about a solution to the same symptom. In my case it was not the GAC that caused the problem, but the cached assemblies under
C:\Users\{name}\AppData\Local\Microsoft\VisualStudio\10.0Exp\
This is the location where all the assemblies of the Experimental Instance go. Here lay an outdated VsExtension that depended on project A and within it there was an outdated copy of project A that was loaded. The loading of the new version from the project reference was simply skipped, since a copy of the assembly (with the same version) was already loaded.
So the problem was ultimately caused by the removal of the extension from my solution. From this point of the extension was no longer updated by VS, but it was also not removed! Lessons learned: After major changes of your infrastructure you may want to delete all cached assemblies below the above path...
Thanks for the help, everyone!

Related

Referenced DLL not copied

I have a project (call it A, it's an exe) which references another project (call it B, it's a class library) which references a dll (call it C). When debugging A.exe from visual studio I get a run time error:
An unhandled exception of type 'System.IO.FileNotFoundException' occurred in B.dll. Additional information: Could not load file or assembly 'C', Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.
When I go to my executing bin\debug directory, sure enough, C.dll isn't there.
I've explicitly called classes in C.dll from B.dll, so there isn't any weird run time adding of references or reflection being used to call it. I've also set "Copy Local" to true on C.dll. If I reference C.dll from A.exe directly, then it runs with no problem, but I'd rather not have to do that.
I've tried to repro the problem in a new solution, but I've been unable to.
Does anyone know what might be preventing this dll from copying to the final output directory?
The best thing you can do is have the projects A, B, and C all in one Visual Studio Solution, then you can reference the other projects that you need without referencing the dlls.
Once you have that, then add a Project Reference like this:
project B references project C
project A references project B
Because you are then referencing the projects, it will pull the necessary dlls along for the ride when you compile the whole thing.
However, as neo said, if you keep C as a dll, then A will have to reference it as well, because VS is not smart enough to copy it as well.
I've encountered similar problems so I'd recommend to try these things:
Toggle "copy local" to "false" and then to "true" again, it should resolve the problem, if the problem is missing
< Private > True < /Private > node in project file
Check Target Framework of your project, I'have similar problem with several projects targeted "4.0 framework" and other "4.0 client profile", after switching to "4.0" on every project was resolved
Ok finally got it...I think.
It looks like the C project didn't have any version information under Application-->Assembly Information. It was also missing a guid.
For the record: You CAN reference the way I described (A refs B which refs C) and have the output copied. I did go back to a dll reference rather than a project reference and it still worked after a clean and build.
I'm still not sure why I was able to get this to work in a new solution (before making changes), but I'm up and running now.
Edit:
One other note, it looks like there was no assembly.cs in the project (which is why it didn't have any version info). I wonder if it was just a recompile of this project that could have fixed it rather than updating the assembly info.

why would a .cs file not see updates to an included dll file

I have a dll containing classes to access data in SQL (a sort of ORM system) included in my .cs page with a using statement. For some reason the dll (with definition for a new field) isn't seen by the cs code, though I've uploaded the new dll in bin. It won't see my new field in the dll's helper classes (now compiled into the dll).
Is there a way to troubleshoot the dll, or the cs to tell why this won't see the class I updated and rebuilt? The class works fine locally and on another server, but on my prod server, it bombs.
This is using Sitefinity 3.7 with a Subsonic/Substage module if that sheds some light on it.
If you are using Visual Studio, verify 2 things, first:
try deleting csproj.user and .suo files (visual studio will recreate them)
The second thing is the version of the framework your project is running, and the version of the framework the dll was compiled in.
If your project is using .NET 4.0 but the DLL was built using 2.0 or similar you may not be able to use it, you can add it, but it wont be loaded.
This sounds so familiar... have you check to see if there is another dll on the path that gets resolved? Dynamic-Link Library Search Order
Make sure that your dll was not registered on the production service in the GAC.
How to extract an assembly from the GAC?
Perhaps you have a local copy of the DLL in your project and the DLL that gets updated is elsewhere.
I tend to think the dll you build is 32 bit (X86) dll. where as you are trying to consume it from project that targets "Any CPU".
Is your production server a 64 bit ?
If answer is yes, goto project properties => Build tab (of your cs code's project which is not understanding the dll) and set the Platform target as X86.
If the updated DLL has a different version number, you may need to update the Project Reference to it by deleting and re-adding a reference to the DLL in the bin folder.
If the project generating the DLL is present in the same solution, you may have an issue in creating a file reference (may not be updated) instead of a project reference (will be updated).
fuslog.exe is a great tool when troubleshooting assembly (dll) binding issues.
http://msdn.microsoft.com/en-us/library/e74a18c4.aspx
Another .net developer helped me figure this out. I had a rogue ToString() in there where there should have been a cast to string, allowing nulls. My dll was okay after all. Thanks everyone for your suggestions, I learned a lot.

"An assembly with the same simple name has already been imported" error without duplicate reference

I'm getting the following error:
error CS1704: An assembly with the same simple name
'Interop.xxx.dll, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null has already been imported. Try removing one of the
references or sign them to enable side-by-side.
Everything I've seen says that I am referencing two assemblies with the same name and I need to remove one of them. However, I've checked and I'm only referencing it once.
This also only happens when I'm using msbuild to build from the command line on my dev box. If I build through Visual Studio or do a clean build on our CI server I don't see this error.
I've tried completely removing all of my source and building from scratch to more closely resemble the build machine but no luck.
So it looks like I can't read today!
The project had a reference to the Interop and a COM reference that generated the "same" interop. So there were two and I just didn't search very well. I still don't understand why it worked in other places but this did fix it.
In the Error List window, the project that was triggering this error was listed in the Project column. I got around the error by doing the following:
I unloaded the listed project (right-click => Unload Project)
Opened the XML for edit (right-click the unloaded project => Edit {ProjectName.csproj}).
Searched for the offending .dll, and noticed it was listed multiple times in the XML
Removed the entire Reference tag related to the offending dll, and did so for every copy of the reference except the first one listed
The reason it was listed multiple times was because several referenced libraries used that dll. This shouldn't be a problem, in and of itself, so I'm not sure what caused this error to suddenly pop up for me. I'll update this answer if I figure that out.
In my case the duplicate entry was caused by a NuGet package reference and a direct file reference to the same assembly in the packages folder. I am not sure how the project got into this state, but unloading the project and searching the XML file for the offending assembly name resolved the issue for me.
Note that in my case this started happening after updating a NuGet package to a newer version with no other changes to the project, so this maybe caused by a bug in NuGet.
If this is a web project, are there any strong-named references to the other version there? Those won't show up as a project dependency, but will cause a run-time error like you describe. Hope that helps
I had this problem but in my case, I had an old copy placed in the current folder for the EXE loading my component, that was loaded together with the current one, that was loaded by hand from my projects folder. Deleting that old copy solved my problem.
I used Debug > Windows > Modules window to see which modules were loaded at that time and that solved my problem.
For others facing the same as me: if building via command line using property AssemblyName, it will overwrite all assemblies generated by all solution projects - in other words, you will end up with (N -1) assemblies named the same where N is the no. of projects - the startup one (which generally will generate an exe).
This happens because all build command line properties are global and overwrite any project-specific setting. See this and this.
From the msdn link mentioned above:
Global properties are properties that are set by using the
/property switch on the command line, or properties that are set by
the integrated development environment (IDE) before a project is
built. These global properties are applied to all projects that are
built by using this Engine.
In my specific case, where Jenkins is the CI tool, I ended up adding a windows batch command at the end to rename the .exe only to what I originally intended when passing the AssemblyName parameter.
For those developing UWP projects that have project references that include specifically the Microsoft.Windows.SDK.Contracts nuget package (or other dependencies that reference it), this is a common error when the version of the SDK contracts is targeting a different version of the runtime to how your project is configured.
For instance, when targeting Windows 10, version 1903:
Any dependencies or reference projects should target or at least support the same runtime version.
it is common thought process to update all NuGet packages when a new stable version is available, but this is not always a helpful practise on its own. Just because a new stable version of a package is available does not mean that you should or that you can easily use that version.
Even though this package for SDK contracts has a stable update, it is not compatible with my main project configuration, Nuget does not know this so it allows the update.
This package is specifically designed to provide windows dlls for project types that DO NOT have windows platform targeting support, it copies the same dlls that are included by the UWP targeting config. By installing later versions of the package the references from the satellite project will be included in the output along with those provided due to platform targeting, ultimately causing OPs error.
There are similar SDK and targeting packs for Windows IoT Device Runtimes, this information should help you identify and resolve those issues if you get stuck on this issue as my team often does :)
In my case, the issue was on wrong characters in the ProjectReference section of my csproj file.
Background
I have a project that references another library I maintain, which I publish as a NuGet package.
Whenever I make changes to my library, I usually reference the local dll in my project to test and make sure everything looks good before I publish the library as a NuGet package.
When testing, I just comment out the PackageReference line and uncomment the ProjectReference one so it references my local dll, like so:
<ProjectReference Include="..\..\my-class-library\MyClassLibrary.csproj" />
<!--<PackageReference="MyClassLibrary" Version="2.0.1"/>-->
Root cause
I had the slashes inverted, so I was using / rather than \ in the path, like so:
<ProjectReference Include="../../my-class-library/MyClassLibrary.csproj" />
Once corrected, the issue went away.
Try this instead: remove Interop.xx.dll from the reference section in Solution Explorer and Rebuild the project
In our case this error was shown when we had a duplicate reference inside the .csproj file (although I have no idea how this happened).
The difference to an already posted answer is that, in our case, one was a project reference and another one was direct binary reference to a dll.
Once we removed one of those, project correctly compiled.

How to set "Specific Version" property for project references in Visual Studio

I have got a Visual Studio Solution containing several projects and have set up the references between the projects as project references.
When a reference to an assembly is added, the references' properties contain a setting for
Specific Version = True|False
This property is missing for project references. How can it be set? I'd like my solution to load any available assembly (no matter what version) in the bin folder.
I've had a problem when a workflow instance (Workflow Foundation) was deserialized and the dependencies were updated meanwhile.
I think the problem is that what you are asking is not possible directly with a project reference, I think that always implicitly turns into an 'explicit version', because of the nature of the link.
The way you could do this (calling the currently referenced project A, and the referencing project B):
Have the project you want to reference in your solution, just like you do now with the project reference
Explicitly set the dependency chain so the 'referenced' project is built first
Build the referenced project A once first manually
Create an assembly reference in project B to the results from the build in project A
Set the assembly reference to 'Specific Version = false'
The build order (dependency) will guarantee that A is always built before B, and B will reference it from the binary output directory of A.
(altho, it's a little brittle and I would not recommend it since it's easy to get wrong results if the settings are not all right, or the sun aligns with the stars wrong, or some such)
I might be misunderstanding your question, but when you add a project reference, the project's assembly is always loaded into any project that references it when the project is built. Therefore, you'll always have the latest available assembly in the bin folder for that project. VS treats projects differently than other assemblies in that regard.
You can add references to project output dlls instead of projects. Then you will be able to set Specific Version setting.
I have found the solution to my problem. It's described pretty detailed here.
The problem is not a matter of wrong project references, but more a de/serializing of workflow instances question.
Thanks to everybody who tried to help.

What scenarios are possible where the VS C# compiler would not compile a reference of a reference?

I'm probably asking this question wrong (and that may be why Google isn't helping), but here goes:
In Visual Studio I am compiling a C# project (let's call it Project A, the startup project) which has a reference to Project B. Project B has a reference to a Project C, so when A gets built, the dlls for B gets placed in the bin directory of A, as does the dll for C (because B requires C, and A requires B). However, I have apparently made some change recently so that the dll for Project C does not go into the bin directory of Project A when rebuilding the solution. I have no idea what I've done to make this happen.
I have not modified the setup of the solution itself, and I have only added additional references to the project files. Code wise, I have commented out most of the actual code in Project B that references classes in Project C, but did not remove the reference from the project itself (I don't think this matters). I was told that perhaps the C# compiler was optimizing somehow so that it was not building Project C, but really I'm out of ideas. I would think someone has run into something similar before
Any thoughts? Thanks!
Have you changed your build configuration? In Visual Studio 2008, the default Solution Configurations are Debug and Release while the default Solution Platform is Any CPU. My experience suggests the Solution Configuration/Platform pair has a unique build configuration. In other words, Debug/Any CPU and Release/Any CPU are two independent build configurations, each with their own settings. If you've selected a different configuration, the settings for the original configuration do not automatically apply; you'll need to set the dependencies for all of your configurations, as well as any new projects you add to your solution, in order to seemlessly switch between them.
You can right click on the solution in the solution explorer, and check your project dependencies.
My guess is that, somehow, you've flagged that B doesn't rely on C.
Alternatively: In the solution properties, make sure that the current Configuration is set to build Project C.

Categories

Resources