Wednesday, January 11, 2012

Visual Studio referenced library namespace problem

Using Visual Studio 2010, I made a WCF Service Library. It took a while to figure out how to test the code. The solution was simple though: 
  • Make a WCF Service Library project, selected form the Visual C# / WCF tab on the New Project dialog.
  • In the same solution space, add a new App project selected from the Visual C# / Windows tab on the New Project dialog.
  • In the Windows App project, Add a Reference to the WCF library project.
  • In the main function of the Windows App, use intellisense to type out the library name space [dot] interface class name and make a new object of the same type.
  • Use any of the exposed methods or properties of the object.

If you need to make a WCF app, which may in turn use the WCF library, there is a problem.

If you use Visual Studio 2010 to build the app then debug it, it just shows the list of files in the folder. If you click the .svc file for the app, it shows you a help page showing you how to generate the client application code to use the WCF app. This is so because the WCF app needs a host. The debugger generates a virtual IIS host. But to invoke the methods of the WCF app, you still need a client app that calls its methods.
So how do you quickly start to debug a WCF app?
  • Make a WCF Service App project, selected form the Visual C# / WCF tab on the New Project dialog.
  • Add a new App project of type Visual C# in the same solution space.
  • Add a Service Reference to the WCF library project. Give it a namespace, say Appname.
  • Use intellisense to type out the library name space [dot] interface class name and make a new object of the type.
  • Use any of the exposed methods or properties of the object.
  • Build the solution and... you get an error of the sort "The type 'Appname' does not exist in the type 'Client.Appname.Client'". What gives?
The problem is due to a bug in the visual studio automatic code generator, and you need to fix it manually:
  • Double click the error to open the Reference.cs file that has the error.
  • Search and replace all error occurrences of Client.Appname with Appname (use your own client and app names). To make sure you are replacing the correct ones, make sure there is a squiggly line under that app name, and remove the stuff starting from the dot before it, back one word.

No comments:

Post a Comment