Simon Needham Simon Needham - 10 months ago 38
C# Question

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.

then when adding a Service Reference a Reference.cs file is created containing the namespace
with a lot of auto-gen code with fully qualified names, e.g.

The Reference.cs file will be full of compilation errors because the compiler starts treating the System namespace as sub member of the
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.
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
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.


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.

Answer Source

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...


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.