Link to home
Start Free TrialLog in
Avatar of jxbma
jxbmaFlag for United States of America

asked on

What is the NameSpace attribute in a WCF DataContract and why should I use it?

Hi:

I'm a Newbie to the world of WCF.

I've got some very basic questions regarding namespaces.

When you specifiy a DataContract on a class, you have the ability to specify the NameSpace attribute.

I've got some basic WCF services defined within VS2010 and am successfully calling the services within a client in the same solution. I'm not explicitly defining the NameSpace attribute.

In straightforward terms, what is the NameSpace attribute?
Why do I need to use it?

I'm fiddling around with trying to implement some generic ErrorHandling using the IErrorHandler interface. One of the parameters for creating a new SenderFaultCode is to provide a namespace. I'm not clear on what I should be using here.
ASKER CERTIFIED SOLUTION
Avatar of Darren
Darren
Flag of Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Vel Eous
Vel Eous

A namespace can be thought of as an abstract container for logical unique identifiers (your objects).  An object created within a namespace is associated with that namespace and that namespace alone.

One reason that this is of interest is due to the requirement that identifiers must be unique.  Namespaces allow the use of the same identifier name assuming they are not in the same namespace.  So for example if you were developing an application which had the identifier "MyClass" in the namespace "MyDefaultNamespace", you would get the error 'MyDefaultNamespace' already contains a definition for 'MyClass' if you were to attempt to define "MyClass" again.  However if in the same application you defined another namespace "MySecondNamespace", you could happily define another "MyClass" within this namespace and receive no error.

Obviously that is a very basic example, however, it gives you an idea of that principle.  It is most beneficial when you are interacting with other developer APIs/systems which may themselves have an identifier that you want to use in your application.  Thanks to namespaces this is possible.

Another benefit of namespaces is the grouping aspect, if you take a look at the .NET namespace schema you will notice that associated controls are grouped together in the same namespace for which the namespace provides its description.  For example System.Windows.Controls contains user interaction controls.