Namespace Resolving Issue Visual Studio 2013 - c#

I am having some namespace issues. In my solution I have the following solutions ;
Services
Services.WebApi
Now, as an example in my WebApi controller I want to reference a sub namespace of the above Services solutions namespace, i.e.;
using Services.Data;
However, it is not resolving the Services from the project reference? Instead its trying to reference from a small namespace inside the Service.WebApi.Services of the current solution? So, it is looking like VS is automatically assuming the local namespace in stead of the referenced one? (i.e. if I hover over the Services text of Services.Data, intellisense is showing 'namespace Services.WebApi.Services'.
I've used this before with no issues, any ideas whats going on here?
To note the Services project is added as a reference and both are running .NET 4.6.

If you wish to indicate that your intention is to start at the "top" of the namespace hierarchy, you can use the global alias:
using global::Services.Data;

You can try using an alias for your Services reference. In the References List, select your Reference properties, And you'll see an Alias property. Change in to the name you want and then change your using to that name. Let's say you set the Alias to ExternalServices. You'll need to add this to your code:
extern alias ExternalServices;
using ExternalServices::Services.Data;
That would solve your problem.

Related

How to set a namespace for custom control

I have been implementing this solution explained here to make an outlinedTextbox.
I have created a test project and added directly in the main namespace and it works.
Now I want to add it to a library (HelperLib) and want to use it in whatever program of mine I want. For example here the program is called pcdLoggerS2.
but when I add it to my xaml it says that
so it's a matter of namespaces.
Therefore I have add this
xmlns:local="clr-HelperLib"
in my definition of my Base:WindowViewBase but nothing has changed but it's really in that namespace!
--ADD FOR DAVID---
You need to make sure that you've added the HelperLib project as a reference to your test project.
Open PcdLogger and right click on References, and add that project as a reference. Until you do this, the XAML in your test project will not be able to find the correct assembly.
Additionally, when you reference a namespace, from another assembly, you need to add that information to the namespace declaration in your XAML.
(See: https://msdn.microsoft.com/en-us/library/bb514546%28v=vs.90%29.aspx)
As an example:
(I would suggest leaving local for your local namespaces)
xmlns:helper="clr-namespace:HelperLib;assembly=HelperLib"
EDIT:
Additionally, Visual Studio's intellisense is fond of telling you that the namespace, or class, does not exist until you have built the project. You may have to rebuild and/or close/reopen the xaml file for intellisense to cooperate again.
check Namespace of you class.
HelperLib its name of a class, but not namespace.
Try to write this:
xmlns:local="clr-namespace:HelperLib"

I can't see a referenced class of another namespace in C#

I have 2 projects:
ConstrainedScheduleInterfaces
ConstrainedSchedule that has a folder (Testing) with my class that need the reference, here's the code:
Tests.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using
using NUnit.Framework;
using Ninject;
using ConstrainedScheduleInterfaces;
namespace ConstrainedSchedule.Testing
{
internal static class Configuration
{
...........
}
}
I added the reference to the ConstrainedSchedule project, but the using ConstrainedScheduleInterfaces; is marked red as not found.
Both the project has destination framework set .NET Framework 4.5
Any help? Thanks
Does the project contain a reference to the other project? You can't just add the namespace, the project itself needs an assembly reference to the other project which has that namespace.
In Visual Studio, open the Solution Explorer. Right-click on the ConstrainedSchedule project and select something along the lines of "Add Reference." In the dialog, select project references (depending on the version of Visual Studio it may be a tab or some other interface indicating projects as part of the solution). Select the ConstrainedScheduleInterfaces project and add the reference.
More information here.
For other people who have this problem, who have already added the reference to the dll and have made sure you have the right namespace at the top, I've found another thing that can cause this.
Select-click the file in visual studio and choose properties. In the Advanced part of properties, look for Build Action. Change it to compile if it's not already on that, and try to build again.
There might be another reason: different target frameworks.
My main project had ".NET Framework 4.7.2" and the referenced project (linked from another solution) had ".NET Framework 4.5.1".
Select the project properties and change the target framework.
I was having this issue after renaming a project and namespace due to a naming conflict.
Basically my main project had a reference to a project I made called Common, but then I needed to reference another project called Common that another dev had written. After renaming my project something like SpecificCommon, I was able to have references to both Common and SpecificCommon in my main project. However, all references to SpecificCommon namespace were now broken.
At first I thought this was some problem with the namespace refactor, but it turns out that it was because SpecificCommon and Common both still had the same assembly name (Common.dll). Changing the assembly name in SpecificCommon's properties and rebuilding solved the issue for me.

I Can't access my class library in a web application

I have added Class Library in my web Project through Add reference option, but i am facing an error
Error 2 The type or namespace name 'UserDetailsDll' could not be
found (are you missing a using directive or an assembly reference?)*
I hope some one will help me.
Thanks in Advance.
There are a few possibilities as to why that is happening:
You're missing a using statement. Try using the whole name of the class (namespace, then class name) you're trying to use.
Your DLL is targeting a different processor architecture. Check the properties of your web application and your DLL and see if they are both targeting the same CPU.
Your DLL is for a different .NET framework than your web application. Check the target framework in the project properties.
Finally, check the references in Visual Studio for a little yellow warning icon next to your referenced DLL. It's presence indicates that the DLL couldn't be found / referenced in the first place.
You need to either add a using in your code file or specify the full name to the class you want to use.
Both solutions can be autogenerated. Rightclick on the class that won't get resolved and click on Resolve -> using XXXX

Building a re-usable class library in C#

I have a class library I've built for a coworker to use that, after giving her the .dll file, I cannot get to work. The name of the .dll file is "BatchDashboard." The name of the namespace is "BatchDashboard," and the name of the class (there is only one) is "BatchDashboard." Is it a problem to have all three similarly named? Visual Studio is able to add the reference just fine. However, this statement:
using BatchDashboard;
spits out the following error:
The type or namespace name 'BatchDashboard' could not be found (are you missing a using directive or an assembly reference?
Likewise, I cannot instantiate a new 'BatchDashboard' object.
Could someone tell me what I am doing wrong?
Thx!
EDIT
I have tried adding the reference to another test project on my computer and receive the same results.
SECOND EDIT
Changing the access modifier to public for the "BatchDashboard" class fixed the issue with the using statement. However, when I try to instantiate an object like so:
BatchDashboard batch = new BatchDashboard();
I got the following error:
'BatchDashboard' is a namespace but is used like a 'type'
It was necessary for me to have different class and namespace names to work. Thank you!
I think, I know what problem you have. When you created your class file, you probably didn't change access to your class to "public". In .Net, unless you have public members in the assembly, the namespace wouldn't be found in "using" directive.
Basically, check if the class you have is a "public class"
Here are my best guesses:
You compiled it against the wrong CPU target
You have a dependency on a Runtime library that should be added as a reference to the
project also.
Make sure that:
The library is added as reference to the project
That library is in a equal or prior .NET version compared to your project's .NET version.

Compilation errors in Reference.cs after adding a Service Reference caused by multi-part namespace

I hit this weird namespace issue when adding my first 'Service Reference' to a client project in Visual Studio 2010.
If my project's default namespace uses two or more parts, e.g. MyCompany.MyApp then when adding a Service Reference a Reference.cs file is created containing the namespace MyCompany.MyApp.ServiceReferenceName with a lot of auto-gen code with fully qualified names, e.g. System.SerializableAttribute, System.Runtime.Serialization.DataContractAttribute.
The Reference.cs file will be full of compilation errors because the compiler starts treating the System namespace as sub member of the MyCompany.MyApp namespace. You get an awful lot of errors along the lines of:
The type or namespace name 'Runtime' does not exist in the namespace 'MyCompany.MyApp.System'...
If I amend the namespace at the top of the Reference.cs file to something simple, e.g. MyCompanyMyApp.ServiceRefernceName then the compiler behaves and recognises the System namespace references as decleration of .net's System namespace.
I'm using a different workaround for now as I really want to keep my multi-part namespaces. My current alternative is to append global:: in front of the System namespace references to force the complier to do the right thing. In fact, if the 'Add Service Reference' wizard uses T4 templates I may just amend those to embed my workaround at the source.
Questions
I'd really like to understand what's going on here and why a multi-part namespace causes this issue. Presumably there's more to namespaces than I thought. Secondly, would really like to work out a better solution than performing a global Find/Replace every time I add a Service Reference or mucking around with some T4 templates.
I found the answer here somewhat unclear, so I thought I would add this as an example (I would do it in the comments but it looks better here):
So I have this as my default namespace:
namespace RelatedData.Loader
But I also add a class named:
public class RelatedData
{
}
Because the class name matches a portion of the namespace when it generates your proxy with Add Service Reference it gets confused.
The answer here was to rename my class:
public class RelatedDataItem
Ahh well I found the cause eventually.
I'm working against a very large third party WCF API and ... one of their namespaces is LameCompany.System (!!) Carnage then ensues...
Arrrgghhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
The lesson to learn here is when Visual Studio/.net compiler stops recognising the BCL's System namespace you have a namespace/type in your project called System. Find it, remove it, shoot the developer that created it.
I found that having a class name similar to your namespace causes this.
Try renaming your class name
I ran into a similar issue with VS2012 described by jabu.hlong and Simon Needham after minor changes in the client project that has the references to the WCF services after updating the reference to the services. I got lots of errors compiling the Reference.cs files generated and so on (the generated files of the XAML as well).
I have selected to reuse types from specific assemblies in my solution and got a similar problems with the namespaces.
The error I get is that the namespace of the reused assembly and the namespace of the generated types can not be found when used in the Reference.cs. Both namespaces have in common the first parts, as they are from the same solution. My namespaces in the solution are like appname.tier.technology.project. Both conflicting namespaces are Appname.Dto.Modulename (the reused assembly) and Appname.Client.Wpf.ServiceName (the namespace in the client project using the services for the generated types).
The problem arises after a minor change in the client project, when I created a new utility class in the namespace Appname.Client.Wpf.Appname. I choose that namespace because the Appname is also the name of a module in the client project. This seems to confuse the compiler and can not resolve both namespaces in the generated Reference.cs. After changing the namespace of the utility class to avoid using two identical parts in it and updating the service reference, the compiler errors in Reference.cs dissapears.
I tried different things (and tried different namespaces when adding the service reference), but nothing worked for me except this brute force fix - in my case it was OK but I am aware it's ugly (and needs to be repeated if you use "Update Reference" in the future):
Since the WCF service namespace is added to your default namespace, just search and replace all mentions of the newly added
MyNamespace.ServiceNamespace
with
ServiceNamespace
in the whole solution (use your own namespaces of course), including the auto-generated Reference.cs file.
Basically, the problem is a name conflict where one name is hiding another. A folder or class named "System" can do that, but if you also have a class with the same name as your project, you'll see the same thing. Sure, you can rename everything in the reference.cs, but it's probably better to rename your conflicting class.
I had folder in my project called "System" (yes, very stupid of me) and that caused some issues in the references.cs.
Renaming the folder (and the namespace), fixed the issue.
Here is how I solve this issue on VisualStudio 2017 trying to add a reference to a web service in a test project.
After trying adding the references, rebuilding, closing, reopening and spending some time on the issue, I noticed that VS had put the files it creates to reference the WS in a folder named "Connected Services".
I renamed the folder without the space then opened all the files in the folder and the csproj with a text editor, replaced all the occurrences of "Connected Services" to "ConnectedServices" and reopened the project.
I then added references to System.Runtime.Serialization and System.ServiceModel and everything now works fine.
This is a bug in Visual Studio (still is at version 2022). To fix, remove the namespace in the reference.cs file. So if your namespace is "myapplication" and your service is "myservice", you'll see myapplication.myservice in the reference.cs file. just delete "myapplication." everywhere and make sure it isn't auto-generated again (lest you have to re-delete everything).

Categories