Last Updated: February 25, 2016
·
10.46K
· edtheprogrammer

The type name 'X' does not exist in the type 'Y.X'

This seems to be an old issue on the internets but it affected me recently...

I created a WCF service in a .NET project (Project A). In another project (Project B) in the same solution I attempted to add a service reference to said WCF service. Everything was kool and the gang until I tried to build Project B. I got several errors saying "The type name 'X' does not exist in the type 'Y.X'" in the Reference.cs file.

Taking a look at the Reference.cs file showed that the namespace for Project B had been appended to the namespace I specified when creating the service reference. So, of course Visual Studio wouldn't be able to find the type, because it is looking in the wrong namespace. I did some googling and found this helpful stackoverflow question:

http://stackoverflow.com/questions/1200346/wcf-service-reference-namespace-differs-from-original

In short, you need to specify a URI as a namespace in the data contract of the service. If you don't do that .NET will just append the namespace of the project where the service reference is being created. According to the top answer to that stackoverflow question you need to update the svcmap file in project B after you create the reference so it maps the URI namespace to a CLR namespace like so:

<NamespaceMappings>
 <NamespaceMapping 
  TargetNamespace="http://foo.com/ServiceReference.X" 
  ClrNamespace="foo.X" />
</NamespaceMappings>

Now just update the service reference and you should be in business.

What bugs me is, why do I need to jump through these hoops to add a service reference? Having to add a namespace to the data contract is all good, but I shouldn't have to manually update my svcmap file.