Adding reference in c# project...but it "goes away"? - c#

I have this c# project...i add this reference (a dll) to it, and try to use that library in my code.
I use what its called (its a database connector)
MiDB myDb;
it doesnt find that, i can right click and say "resolve" usings... that adds this line to the top:
using ThePackage.Database.AoNM.MiDB;
blah blah
So this works, it sees it, i can even then call a method that is seen:
myDb.InitDB("stuff here");
bingo. the minute I build this, it says it cannot find ThePackage, and its like that usings, or reference doesn't exists. In the solution explorer it is still there? I can remove the reference and add it again, and then this same thing happens, it "seems" like its there, but going to build and run it "goes away".
It might be cause its late, but I am honestly totally lost as to why C# (2008) is doing this to me?
DLL was built in 2008 to... maybe im missing some build option with the dll?

Check that the dll and the assembly run on the same CPU type (x86-x64). I used to have similar errors (and very strange error messages) when they were different.

I had same problem.
You need to change target framework of your project from ".Net Framework 4 Client Profile" to ".Net Framework 4"
It worked for me.

if the dll is using a higher .net framework version than the application, you would see this. it's probably unlikely the way i read what you said in the comment in that the application is in visual studio 2010 and the dll was built with visual studio 2008. not completely sure that's the case, though, as the quesiton cites visual studio 2008. anyway, check that you are using the same framework version or greater in the referencing project as in the referenced.

Related

Cannot define a class or member that utilizes 'dynamic' because the compiler required type 'System.Runtime.CompilerServices.DynamicAttribute'

I'm trying to run a .NET MVC application on my local computer that I got from GitHub.
When I hit run on Visual Studio, everything complies and a new browser window opens with the error:
CS1980: Cannot define a class or member that utilizes 'dynamic' because the compiler required type 'System.Runtime.CompilerServices.DynamicAttribute'
The compiler section in the same window, shows the following error:
I've checked on google and this seems to be the same error.
This guy was using a dynamic type himself, on the other hand I'm not using any dynamic type, this is being auto generated by .NET's compiler, and I'm using .net 4.5 which should has support for dynamics.
I tried to apply the same solution (Changing all System.Core references to 4.0)
but the thing is that all of them are version 4.0 already.
Do you have any idea how could I resolve this?
I had a similar error on a project I was trying to migrate. Try re-targeting the framework of the project back a version or two, and once you find a target version where you do not get the error, re-target back to the version you originally had trouble with.
I was getting the same error on a website project targeted for 4.6.2 that was referencing some older libraries, re-targeted it back to 4.5.1 and that resolved the error. I immediately re-targeted back to 4.6.2 and did not encounter the error again.
I had this same issue, but none of the answers here solved it, but did point me in the right direction. I think I didn't update the .NET version on my machine to the newer one. After installing it, I rolled back the targeted framework (as was suggested), retargeted the newer framework, and did a few extra things also:
Reinstalled ALL packages by running this in the package manager:
update-package -reinstall
I also compared my project to one that was working for 4.5 and did this:
Opened my project file and removed all "BCL" related elements.
Uninstalled all "BCL" packages in the NuGet package manager for the project (guess it's not needed moving to 4.5+...?)
Removed an old package version of System.Net.Http, which did not get updated for some reason (see web.config - versions should show oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0").
Have to restart VS.
Rebuild solution.
Error is now gone. ;)
One other thing I did to resolve some other errors was to clear the component cache by deleting all files in here:
C:\Users\{USERNAME}\AppData\Local\Microsoft\VisualStudio\14.0\ComponentModelCache\
[rant] Wasted a whole day with all this. Thanks to MS for stealing one more day of my life fixing senseless issues that should never be. Every time I upgrade I always get a wonderful surprise, and a lovely game of fix the red herring. Would it not hurt to detect these conflicts for those upgrading old projects? Perhaps better error traces when things go wrong? I doubt it would be that difficult. Perhaps even something like AsmSpy, but more advanced, built in to VS? [/rant]
Saw this in VS 2019. I excluded and re-included the problematic file (in my case _Layout.cshtml) and that fixed the problem.
I got this error suddenly a couple days after updating to VS2015.3 using 'traditional' MVC app (not new .NET Core / vNext app).
Turns out this error can be a complete red herring.
Make sure:
You haven't got any other errors at all
You've checked the output window for any errors that don't show in the 'Output' window.
Make sure you don't have any conflicting references
In my case I had a typescript error that was causing an error and for some reason triggering this weird 'red herring' dynamic error too.
My fix was to go into the .csproj file and update the typescript tools version to 1.8 (need to unload project and edit by hand). As I said this is unrelated to the error, but I mention the specifics because someone else may end up with this same problem.
I believe that 1.7 didn't accept 'None' for the typescript module system whereas 1.8 does, hence the error.
Adding a reference to System.Dynamic.Runtime did it for me.
I hesitate to write this as an answer, but for me the error appeared in a .cshtml file. I closed the page, rebuilt, and the error vanished. Always best to try the simple things first.
I had the same error, there were just 2 unused tables of my databaseset that I had to delete. I wonder why it worked when I manually change sth in an auto-generated code work but after deleting, it worked.
So be aware that such inconsistencies can also cause this error.
My solution was to correct the version of MVC in web.config under Views folder. E.g. the project referenced MVC assembly version 5.2.2.0 but in the Views/web.config file was version 5.2.4.0 (see image).
Image
Tried a few other answers with no luck, but the overall consensus appears to be that this some sort of issue with cached build artifacts or compiler state or something. Here's what worked for me:
Delete the problem .cshtml file, watch error disappear
Open source control and revert the deletion
This error will come because of the different versions of your Sitecore.Web.Mvc.Dll in solution reference folder(Project name->References->Sitecore.Web.Mvc->right click and check properties) and the version of Sitecore.Web.Mvc.Dll in Web.Config in views Folder.
For solving this error you have to make sure that the version of Sitecore.Web.Mvc.Dll is same in both folders.
Thanx.
For my case, I fixed to run the command line "update-package -reinstall" on the Package Manager Controler.
My problem was in the _Layout.cshtml.
None of the solutions above did the trick for me. I am using VS2017 for this project, upgraded an ran into the same message.
I had commented out a 2 lines in 1 comment #* *#
My solution was to comment out each line separately. What a mystery!
A few hours lost on this.

The type or namespace name does not exist in the namespace ''are you missing an assembly reference ?"

I have a solution that worked fine until the last couple of days which contains around 15 projects that are DLLs or Webforms applications. I also use ReSharper which is another peculiarity related to this solution.
There are naturally projects referencing other ones within the solution and all but one compile independently and run OK except one Webforms app (A) that refers to namespaces within another Webforms app(B) DLL.
(A) has 7 exceptions when I try to debug this all along the lines of "the type or namespace name [MyClass] does not exist in the namespace '[(B) namespace]' (are you missing an assembly reference?)"
All the projects target the same .net version 3.5 all target the same processor CPU architecture, I have cleaned the solution, recreated the reference to (B) from (A), can see the namespace in the object explorer when looking at it from (A)\bin so it's there... but still (A) at build has these errors above and for over a day now I cannot for the life of me work out why... can anyone help?
If I run the Webform app (B), it builds and runs without a problem...
The mention of ReSharper is due to it also not working with this solution anymore (but does work with others solutions), the behaviour of ReSharper with this solution is that I cannot type characters into VS anymore unless ReSharper is suspended. Also when searching for types the list does not filter down at all - so there is some sort of corruption there although I have cleared caches related to ReSharper.
The problem might be your virtual directory in IIS, it was not pointing to the correct folder, so the page couldn't find the DLLs
Brad McA, have you unblocked that namespace? In REFERENCES folder, right click on the particular reference, and see if there is an option "Unblock", If u can see it, kindly unblock it. Thanx
Seems to be building now, reason for failure was a dependency on Newtonsoft.Json.dll which was built to target .net 4 (strange as this reference in (B) has been there for ages! why it would suddenly cause build issues).
Thanks to those who posted, have a lovely day.
the dll reference were origionally built in framework 4.7.1. when trying to add the reference in lower framework project say 4.5.2. It gives such errors. try to update your project framework from 4.5 (lower version) to the version of your reference.

When compiling solution, previously working assembly reference / using no longer works [duplicate]

My C# WinForms solution has two projects.
A DLL which is the main project I'm working on, and an executable WinForms I call "Sandbox" so that I can compile/run/debug the DLL easily in one go.
I'm working in .Net 4.0 for both projects.
Everything was working fine until I added some seemingly innocent code, and a reference to System.Web in the DLL.
Now my Sandbox project can't see the namespace of the DLL project. I didn't change anything which I believe should have affected this.
If I delete the project reference to the DLL from the Sandbox references and re-add it, then the red underlines all disappear and the colour coding comes back for all my classes etc; but as as soon as I try to build the solution, the whole thing falls apart again.
When I right-click the DLL project in the Sandbox's references and view in object browser, I can see the namespace and all the stuff in there.
I have a feeling this might be some sort of bug?
Is this some sort of VS2010 bug? I had this same issue a few months ago and I could only fix it at the time by making a whole new project and re-importing my files. This time, however, I have a bajillion files and will only do that as a last resort!
Edit:
After panickedly going through and undoing all my changes, trying to find what caused the problems, it seems to be this line:
string url = "http://maps.google.com?q=" + HttpUtility.UrlEncode(address);
If I comment out this line, then I get no namespace errors and the project builds fine. I can't see anything wrong with this line though.
I'm ready to declare this a bug in VS2010, this has bitten way too many programmers already. The fix is easy: Project + Properties, Application tab, change Target Framework to ".NET Framework 4" instead of the Client Profile that is selected by default.
System.Web is not included in the client profile. Having this option in the first place is quite silly, the client profile is only 15% smaller than the full version of .NET 4.0. Having it selected by default is even sillier. But I digress.
UPDATE: mercifully this all got fixed in VS2012. Which no longer makes the client profile the default for a new project. And the client profile got retired completely in .NET 4.5, good riddance.
Check to make sure that both projects are using the non-client profile for their target framework (go to each project's properties to do this).
One possibility is that the target .NET Framework version of the class library is higher than that of the project.
I faced this problem, and I solved it by closing visual studio, reopening visual studio, cleaning and rebuilding the solution. This worked for me. On some other posts, I have read the replies and most of users solved the problem by following this way.
Try building only the project with the Sandbox dll first independently.
Then point your executable project to the required dll and ensure copy local is set to true. in reference settings.
Tthen build the executable project.
Changing the target framework from the ".NET Framweork 4 Client Profile" to ".NET Framework 4" worked for me with a similar problem. I agree that the client profile doesn't seem to have much of an advantage to using it. I seem to get nailed with weird errors that I hunt for until I remember that Visual Studio defaults to the client profile. I guess the moral of the story when getting an error is: if "Rebuild Solution" doesn't work, check the Target framework...
If you tried already doing the Framework change, and still not worked, I hope this works for you (as it did for me): Simply add the necessary references from within your projects. Very obvious but I was doing it wrong until I found what was the issue.
I just had this issue and it turned out to be I had multiple namespaces being used that had the same object name (i.e. business objects had the same names as mvc models);
Fully qualifying the names fixed the issue for me.

Weird: C# Type or Namespace name could not be found - Builds successfully

I have a weird error showing up in my project when it is open in the VS2012 IDE. Everywhere where I make use of another referenced project it suddenly says "Type or Namespace name could not be found". And by "says", I mean it has the text underlined in red with the error when I hover over it. The intellisense doesn't work for that code. BUT (and here's the weird part), the errors do not show up in the error console and the project builds and runs fine.
I can even debug and step through the code and it works perfectly fine. So at runtime the project is referenced fine but at design time the IDE can't find it. This worked for the past 2 weeks, and only then suddenly went a little bonkers. It's really annoying because I am rubbish at coding without intellisense!
Has anybody ever seen anything like this or have any suggestions?
I had this. I referenced assemblies whose "Target Framework" were set to ".Net Framework 4" in the "Application" secion of the projects properties. I changed this to ".Net Framework 4.5" not just in the referenced assembly's project but also the project I was building and it worked. Give this a try.
I have found this is a known problem with VS2012. Check to see how you are building, 64 bit or 32 bit. It won't work with 64 bit but it will with 32 bit. It will say things are missing and design will not work, however the program will run fine. I have heard the new VS update that hasn't been released yet will fix it.
Manually delete all the references to the libraries of the other projects and re-add. Intellisense rebuilds whatever it needs at that point and doing this has helped me in the past.
Another source of this problem is a solution with multiple projects containing code for the same namespace. The compiler can handle this. Intellisense won’t.
Related to a couple other answers here, I had a "Data" project using a "Data" namespace. Built fine, but just started recently showing errors from intellisense (even though it continued building fine.) (VS 2015.)
I fixed this by changing my "Data" project and namespace to "MyCompany.Data".
Oddly, the problem didn't seem to show up until recently, but making the change did fix it. Presumably there was a conflict in namespace with another project or reference, which can build fine, but intellisense can't handle.
You can change this in the Application tab of Project properties. You can also open up an EDMX diagram, right click, choose model browser, choose the second collapsable item in the model browser tab, hit properties, and there you'll find the Namespace option for generated Entity Framework entities and contexts. (Similiarly, if you modify an Entity Framework Model's namespace, you might also need to change the related connection to match [in Web.Config for ASP.Net and MVC.])
Make sure that there is no class with the name same as Project default namespace.
make sure that you don't reference .net framework 4.5 projects in .net 4 projects!
Examine your .proj files in a text editor and make sure the paths to your references are correct.
This can also happen if you set the Build Action to "None" on the referenced file and forget about it.
I had the same problem, where it would build and run fine, it just would always show that error and I couldn't use intellisense with the class.
I actually used the automatic method of creating the class in a new file to resolve the issue, then just copied the code over from the real class. I deleted the old file, renamed the new file, and now it works.

Reference disappear upon debug/build (C#, VB.NET, VS2010)

I am using the MagicLibrary for a few components. I knew it was written in Visual Studio 2003 and recompiled it in Visual C# Express 2010. It compiled just fine and I could see the library in the folder.
I then created a new project in Visual C# Express 2010 and added the components MagicalLocalLibrary.dll to the Toolbox. They appeared fine with no errors. I put in a control from the Toolbox to my form, and it seemed fine. I then debugged (to see if any errors would occur) and I saw that the reference MagicalLibrary disappeared and I got the error:
The type or namespace name 'Crownwood' could not be found (are you missing a using directive or an assembly reference?)
The thing is that, the reference shows fine under References in Solution Explorer but I can't import it with using or use it in the code (simply is not listed in InstelliSense either) but if i go into the Object Browser and choose "My Solution" it is not listed. I can click on the Reference in Solution Explorer and just change the Copy Local property to True or False, then it will appear in the Object Browser but whenever I debug my project again it disappear.
Any idea why it does this? Thanks in advance.
Check that you are using the .Net 4 framework and not the client profile version
You find that setting when you right-click on your project, choose properties. And under application there is a setting for target framework. If you have chosen the .Net 4 framework Client Profile, then this behavior that you described can happen
I ran into a very similar issue once. In my case the problem turned out to be a missing dependency for the "vanishing" namespace's assembly. What finally gave me the information I needed was the suggestion in this StackOverflow answer to raise the MSBuild output verbosity level. After raising the verbosity setting the IDE should give you a more specific reason why the compilation fails.

Categories