Monday, May 4, 2009

Namespaces and Partial Classes

This post goes in the 'finally figured out why my code wasn't working like I expect it' category.

More and more, Visual Studio and various .NET project items use the .designer file as a partial class to put code generated by VS. A lot of time, you never need to worry about the .designer file.

I encountered a situation recently while creating a Windows Service in VS2008. In my Service class (which has a .designer file) I changed the Namespace. Everything compiled great, and the Windows Service installer would successfully deploy and register the service. But when I would try to start the service, nothing would happen. My code would never run.

In the end, I remembered that the Main() method of the Service is located in the .designer file. As soon as I added the matching Namespace to the .designer file, rebuilt and redeployed, the service started up without any problem.

I personally like to use Namespace entries on every class rather than using the Root Namespace entry on the Project properties. I now know that I need to make sure any Partial classes also have the matching Namespace designation. Lesson learned.

No comments:

Post a Comment