Visual studio C# packages - c#

this is my problem with VS :S
in the first project :
System.Security.Cryptography.AesCryptoServiceProvider obj;
everything is ok
in the second project:
System.Security.Cryptography.AesCryptoServiceProvider obj1;
it doesn't recognize the AesCryptoServiceProvider?!!
is VS using different packages or what ?!
updated: changed the variable name but still not working

var is a reserved keyword. Use a different identifier name or #var.
System.Security.Cryptography.AesCryptoServiceProvider #var;
This may not be the problem - you need to ensure that each project has a reference to System.Core the assembly containing System.Security.Cryptography.
You will also need to ensure that you are targeting a framework version that contains this class (.NET 3.5 and above) - this can be done in the project property pages.

Check if both projects are referencing System.Core. Probably only the first one has it. You've to add it on both to be able to use AesCryptoServiceProvider.
Moreover, as you can see here AesCryptoServiceProvider is only available since .NET 3.5. Check your project's properties, in particular the Target Framework.

Are the references the same between both project? Just open references and see. I bet you are missing one. However, you really should use a different variable name than var. Also, can you post the exact error?

You can't name a variable var because it's a reserved word, use a different name, this will not cause an error :
System.Security.Cryptography.AesCryptoServiceProvider _var;
Edit :
AesCryptoServiceProvider is only supported in .Net framework 4 and 3.5 SP1, change the target framework and it will work and be sure to have System.Security.Cryptography; in that file.

Related

Visual Studio 2012 how to get missing assembly references

I'm new to c# and visual studio..
currently i'm developing a metro application fro windows 8 that works as a proxy switcher application..
when I using this code
System.Net.GlobalProxySelection.Select = new System.Net.WebProxy(proxyURI);
it says GlobalProxySelection does not exist in the System.Net.... I imported system.net at the begining of the code..
Edit
no problem with System.net... no errors showing at there.. But the thing is I cant use "GlobalProxySelection" and "WebProxy" classes..
Also Target framework is locked in application properties..
It is likely you haven't added a reference to the System.Net assembly. See this article from MSDN which should give you more info on adding this reference.
The System.Net namespace is in the system.dll assembly. I would be surprised if that weren't already referenced when you started the project, but to be safe, you should ensure that a reference to this dll exists. If it doesn't, then add it.
You can always find this kind of information on MSDN: GlobalProxySelection Class. In the Inheritance Hierarchy section, there is namespace/assembly info.
The only other thing I can think of is maybe you've added a reference to some other assembly that has this namespace (seems unlikely, but possible).
If you go to your project's properties, what is your target framework? It might be that is's accidentally using the "Client Profile" version of the framework, which misses quite a few bits and pieces. If so, change it to the normal version and then it might suddenly exist.
You can change your Target Framework on Application section and don't fix to Client Profile

Can't compile c# project because of some tuple error when referencing an external dll

I have an error, when I try to build my .net 4, c# project. Everything works great, but when I add an external reference to a given DLL, it stops working, it can't build, throws this type of some errors:
Error 36 The type 'System.Tuple' exists in both 'C:\Program Files\Reference Assemblies\Microsoft\Framework.NETFramework\v4.0\mscorlib.dll' and 'C:\Projects\Project1\ExternalRefernces\SharpSNMP\SharpSnmpLib.dll' C:\Projects\Project1\CheckerStore.cs 17 21
Note, I did not do anything with the new library, just added as a reference. Any ideas?
You can solve this problem by specifying an external alias. Select the SharpSNMP reference in your project. In the properties window change Aliases from global to say SharpSNMP. In your code type this
extern alias SharpSNMP;
...
System.Tuple<T1,T2> sysTulpe;
SharpSNMP::System.Tuple<T1,T2> sharpTulpe;
or
extern alias SharpSNMP;
using SharpSystem = SharpSNMP::System;
...
System.Tuple<T1,T2> sysTulpe;
SharpSystem.Tuple<T1,T2> sharpTulpe;
See Aliases: overcoming name conflicts part 2: extern alias
What you can do is either change the target version to 3.5 or make some changes in the SharpSNMPLib. The source can be fetched from here or here.
The changes you need to make is specifically moving the System.Tuple type somewhere else.
Edit:
I belive you have added a reference to a precomplied DLL. A DLL that is NOT compiled for framework version 4. What you need to do is download the source code (see links above) and compile the project with target version 4.
Why you need to do this is because there are conditional build parameters depending on the framework target version. The SharpSNMPLib System.Tuple is used for version <= 3.5 and the framework System.Tuple is used for version >= 4.
Edit:
Reproduced your problem using framework System.Tuple and SharpSNMPLib.dll.
Successfully built SharpSNMPLib targeted on version 4.
Successfully built application using framework System.Tuple and the new SharpSNMPLib.dll.
Simply go to your CheckerStore.cs file, line 1721 (if I'm right). Find the Tuple class, and reference it using it's fully qualified name.
The library for some reason re-implements some system types. Likely reason is to make code to be source level compatible when using older versions of the framework.
Most likely there is a version of this SharpSNMP library that works with 4.0 framework. Check if you already have correct on in your source tree. Check with creators of the library what versions of the assembly you need to use with given framework version and what is recommended way of doing it.

Why can't I reference System.ComponentModel.DataAnnotations?

I'm trying to use DataAnnotations in my WPF project to specify a maximum length of strings, with the following:
using System.ComponentModel.DataAnnotations;
However, I get the error
The type or namespace name 'DataAnnotations' does not exist in the
namespace 'System.ComponentModel' (are you missing an assembly
reference?)
I've seen other examples where DataAnnotations does exist in this namespace. I'm using C#4. Is there any reason why I can't use this? What can I do to fix it?
You have to reference the assembly in which this namespace is defined (it is not referenced by default in the visual studio templates). Open your reference manager and add a reference to the System.ComponentModel.DataAnnotations assembly (Solution explorer -> Add reference -> Select .Net tab -> select System.ComponentModel.DataAnnotations from the list)
If using .NET Core or .NET Standard
use:
Manage NuGet Packages..
instead of:
Add Reference...
To Reference System.ComponentModel.DataAnnotations
In a code file to have Using System.ComponentModel.DataAnnotations; at the top of the file such as:
using System.ComponentModel.DataAnnotations;
Add a .NET reference to your project by right clicking the project in solution explorer:
Hope this helps! This question helped me.
If you don't have it in references (like I did not) you can also add the NuGet System.ComponentModel.Annotations to get the assemblies and resolve the errors. (Adding it here as this answer still top of Google for the error)
I also had the same problem and I resolved by adding the reference in one of my projects which didn't had the mentioned reference. If you have 2-3 projects in your solution, then check by adding this reference to the other projects.
I found that I cannot reference System.ComponentModel.DataAnnotations from Silverlight 5 with the below version at (1). I found that Silverlight 5 assemblies cannot use .NET assemblies, it gives the error "You can't add a reference to System.ComponentModel.DataAnnotations as it was not built against the Silverlight runtime. ..." I plan to workaround this by hopefully installing the Silverlight 5 package found at (2) below. If this fails I will update this post.
[UPDATE: it failed. I installed everything relating to Silverlight 5 and I don't have the Silverlight version of the .dll assembly System.ComponentModel.DataAnnotations . Too bad. UPDATE II: I found an old .dll having this name from a previous installation of Silverlight developer's kit for Visual Studio 2008 or 2010. I added this file and it seems to 'work', in that IntelliSense is now recognizing attributes on class members, such as [Display(Name = "My Property Name")]. Whether or not this works for everything else in this .dll I don't know.]
(1)
Microsoft Visual Studio Professional 2013
Version 12.0.21005.1 REL
Microsoft .NET Framework
Version 4.5.51641
Installed Version: Professional
(2)
http://go.microsoft.com/fwlink/?LinkId=229318
I searched for help on this topic as I came across the same issue.
Although the following may not be the Answer to the question asked originally in 2012 it may be a solution for those who come across this thread.
A way to solve this is to check where your project is within the solution. It turns out for my instance (I was trying to install a NuGet package but it wouldn't and the listed error came up) that my project file was not included within the solution directory although showing in the solution explorer. I deleted the project from the directory out of scope and re-added the project but this time within the correct location.
Use the FrameWork version 4.5 and above for your project then problem solved.Because this namespace is under 4.5 and above.
System.ComponentModel.DataAnnotations is contained in its own assembly so you need to make sure you have it refernced. Just simply:
1). Right click on Soloution and choose add.
2). Choose reference from the list.
3). Search " System.ComponentModel.DataAnnotation " and tick the check box on its left hand side and press ok.
Job done, shouldnt have any refernce errors.
If you tried to update visual studio from vs2008 to vs2010. And your app uses framework 3.5 (and you don't want to upgrade it), and also used WCF RIA Services BETA... I have bad news... you MUST upgrade to WCF RIA Services v1 (BETA does not work on vs2010)... and due to this... you also have to install Silverlight 4 + upgrade to framework 4.0
See this:
http://blog.nappisite.com/2010/05/updating-visual-studio-2008net-35-ria.html
I upgraded from Silverlight 4 to Silverlight 5 and then I was having this issue. Although I had a reference to "System.ComponentModel.DataAnnotations" under "References" in my project, it had a yellow yield sign by it that indicated the previously referenced assembly could not be found. It turned out that the properties of the "System.ComponentModel.DataAnnotations" reference indicated "Specific Version = True", when I changed this to "Specific Version = False" it fixed the issue. Right click on the "System.ComponentModel.DataAnnotations" assembly under "References" and select "Properties" from the context menu. Check that the property value for "Specific Version = False".
It must have been referencing the old Silverlight 4 assembly which was no longer available after the upgrade to Silverlight 5.
I also have this problem.
That is very stupid when i add a namespace the same with System. I try to remove all references, but it is not resolved. I use "global::System.ComponentModel", it is working as well.
When i remove my namespace, this problem has been resolved.
For .Net Core in Visual Studio 2019 try this.
see VS suggestion
It worked for me, hope it'll work for you as well.
I was moving from .Net Framework 4.7.2 to .Net Standard 2.0.
In my case, I had to change DataAnnotations's reference from an Assembly reference to a Nuget package.
This error occurs when the reference to the "System.dll" got removed.Solution to the problem is very simple add the reference to "System.dll".The dll is normally available in the following location
"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727" .Add the reference your problem will get solved .
There was a problem using System.ComponentModel.DataAnnotation in net40 so I just did:
#if !NET40
using System.ComponentModel.DataAnnotations;
#endif
#if !NET40
[StringLength(256)]
#endif
The NET40 must be a predefined macro definition for .Net Framework 4.0
I had same problem, I solved this problem by following way.
Right click on page, select Property. in build action select Content.
Hope that this solution may help you.

Reference added but Namespace is not recognized

I added a DLL to my project. The DLL contains the namespace test.security. Now, test.security is not recognized. Why is this?
I am using this DLL in other projects and I have no other problems.
Are you using Client Profile as a project target? Consider this scenario:
Project A -> Project targets .NET Framework 4.0
Project B -> Project targets .NET Framework 4.0 Client Profile
Project A is referenced by Project B. Namespaces in Project A are not recognised in Project B.
If ths scenario matches, this is due to a target mismatch. Client Profile supports a subset of the BCL of the full framework. If an assembly is dependent on using the full framework (like requiring types from System.Web, etc.) then it won't be useable from an assenbly that only supports Client Profile.
Easy solution, change Project B to .NET Framework 4.0 (not Client Profile).
It often depends on what is in that namespace; for example, if nothing is in there then the namespace doesn't really exist.
It is also possible that you are missing some other dependency which means that the compiler can't use (until the reference is added) any of the types in that namespace (for example, if the types in that namespace all depend on some type from Another.dll, and you haven't referenced Another.dll).
It is possible you have referenced the wrong version of the dll, and the version you are referencing doesn't have that namespace.
It is possible the compiler is already telling you about a reference problem, meaning it can't use it - look in the errors/warnings list. For example, it could be a physically missing file, or a .NET version mismatch, or a strong-naming issue, that means it can't use the reference.
1.remove the reference and add it again
2.Close the solution and re-open it
3.Create a new solution and add all old ones in it
Way late to the party, but obviously this came up in a recent search, so this is to help the newbie who lands here. Here's one more thing to verify.
As quoted from Dummy01's comment to his answer to this question:
Pack C# project to dll or another library
"DLL is in your project's bin or release folder. If it looks empty is because your classes are defined as private or internal. You should change the names you need to see outside your dll to public."
Check your DLL,s .NET version and your host project's . NET version. Most probabbly there are different and somehow it creates problems in your specific case.
Regards.
I also faced this problem. In my case I tried removing the reference, rebuilding the referenced project, and then adding it again, but the problem still persisted.
The problem in my case was the classes in the targeted project's namespace were not public. This meant there was nothing accessible in that namespace, so it didn't really exist.
Setting them to a public access level solved the problem. Hope it helps someone! :)
I had the same problem. I changed a console application to a class library in the project properties. That fixed it.
I would like to add a cause for this, found in VB.NET (Visual Studio 2010, in my case; yours may vary).
As an example, I have two projects: P1 and P2.
P1 is an Application, and P2 is a Class Library.
Stipulations:
There is a reference in P1 to P2
Both P1 and P2 are targeting .NET 4.0 (full, not Client)
Both P1 and P2 target x86 (not that it likely matters)
There are 0 errors and 0 warnings
However, in P1, it is not possible to declare an 'Imports P2...' expression, nor to use any Shared methods found in P2. It exactly as if the Namespace P2 does not exist, though the reference is there.
Cause: P2 was converted to a separate assembly from code in which all methods were contained in a VB.NET Public Module. However, the 'Module' was not re-typed as a Public Class.
No errors whatsoever, but the P2 Namespace was entirely unavailable to P1 until a Public Class was created.
Of note, it was not actually necessary to convert the original Module to a Class. It was only necessary to declare some Public Class (even if it is empty) within the P2 Namespace, and then all methods found in that Public Module were available.

Why can't I find or use UrlEncode in Visual Studio 2010?

I have a string that I'd like to encode into the standard URL format. From what I've found, I should be able to do this via the httpUtility.urlEncode method, but I don't seem to have that available.
I've added "using" references to both System.Web and System.Net to no avail. I've also seen other references to server.urlEncode amongst other variants, but I don't see the method anywhere.
I'm using the latest version of C# in Visual Studio 2010. Is the method called something different in this version, hidden somewhere else, or am I completely off base?
By default, new projects in Visual Studio 2010 target the .NET Framework 4.0 Client Profile, which does not include the System.Web assembly.
You can change the version of the Framework that your project targets in your project's Properties. Under the "Application" tab, select ".NET Framework 4.0" from the combobox labeled "Target framework".
Then, make sure that you have added a reference to System.Web using the "Add Reference" dialog.
Finally, add a using directive to the top of your class for the System.Web namespace:
using System.Web;
You'll find the various overloads of the UrlEncode method in the HttpUtility class. Sample code:
HttpUtility.UrlEncode("http://www.google.com/");
In .Net 4.5 you can (should?, 'please use' says a Katana comment) use the System.Net.WebUtility.UrlEncode method.
It can't be named differently since Visual Studio doesn't supply the class or method names, the .NET framework does.
All I can tell you is that the System.Web.HttpUtility AND System.Web.HttpServerUtility classes contain a method called UrlEncode(string).
If your project target ".NET Framework X Client Profile",you cannot not use "System.Web",but you can use "Uri.EscapeUriString | Uri.UnEscapeUriString" instead.
Yes, adding the reference was my answer. But be sure you double check the project, that it is in, if you have more than 1 project in your solution. I had a solution with 3 projects. System.Web was added to 2 projects but not the 3rd project.
I spent an hour trying to figure out why I couldn't use HttpUtility since it was a Reference in the main project. But I didn't check the sub-projects of the Solution.
Hope it helps someone.
Because you only see AspNetHostingPermission, AspNetHostingPermissionAttribute, and AspNetHostingPermissionLevel, I strongly suspect (like the other guys) that you're missing a reference.
The best you can do is start a new project, because it's pretty complicated to add/remove references without ruining your entire project.
How to: Add or Remove References in Visual Studio (MSDN) shows how to add/remove references. In your case, you should check/add the System.Web reference.

Categories

Resources