WCF - The type name 'ServiceReference1' does not exist in the type '...'




Recently, I ran into another candidate for the WTF files having to do with an error message similar to the one in the title of this post. Here is the scoop.

I created a WCF service library implementing some basic functionality. Actually, to verify the error I am describing, I just created a new WCF Library project and used the default code contained therein.

Subsequently, I created another project that would become the WCF client. Here comes the iffy part..

Typically, I do not like keeping default file names generated by Visual Studio (i.e. Program.cs). Reasons for this include search ambiguity, dislike of non-descript code files, and just personal taste.

However, one of the things that Visual Studio offers during the name change, is to change the name of the class contained in the code file. So, if you rename Program.cs containing the following:

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
        }
    }
}

 

to, say, ConsoleApplication1.cs, the class name will change to ConsoleApplication1 as follows:

 

namespace ConsoleApplication1
{
    class ConsoleApplication1
    {
        static void Main(string[] args)
        {
        }
    }
}

 

Note the dubious arrangement of the namespace and class name being the same. As far as I recall, good old C++ would not let you get away with this sort of debauchery, but this code will compile in C#...

Until... you add a WCF service reference. When you do this, you  get asked what namespace should be created for the service reference items. Whatever you specify will result in a new sub-namespace being created within the default namespace of the project. 

Adding WCF Service Reference

So, in the case above, the service reference will be generated as follows (in ConsoleApplication1\Service References\ServiceReference1Reference.cs):

namespace ConsoleApplication1.ServiceReference1

But also, as a result of that, all service types (DataContracts, Interfaces, etc) will be referenced via with a fill namespace:

ConsoleApplication1.ServiceReference1.CompositeType GetDataUsingDataContract(ConsoleApplication1.ServiceReference1.CompositeType composite);

 

This generated code will result in an error. This is due to the fact that the file name change above, resulted in a class name change, and hence there now is a class called ConsoleApplication1 that is in the same project scope as our newly added reference. The compiler cannot determine whether the generated code is trying to reference a member of the class or the namespace, defaulting to the class and causing an error (since obviously the class does not contain type definitions from the service reference..)

Several solutions are available:

  • Change the class name of the WCF client application to be different from the namespace
  • Change the namespace encapsulating the WCF client application class (resulting the class being in a different namespace than the reference).
  • Close Studio and get a beer
So really, by doing something silly (creating namespace and class of the same name) but completely legal, you can break your WCF service reference.

 

Cheers!

 



Thanks Solved my problem ... after reading it carefully
:)



Solved my problem - thank you.



You did it. Thanks. Wasted a few hours trying to solve with soap. Just because of that error.



Thnks it worked for me



Thanks I was caught out by the very same issue.



User login