Simple Steps to Start WCF with Visual Studio 2008
WCF, as you may have known, stands for Windows Communication Foundation. It is an interface provided in .NET framework for building connected and service-oriented application to support Service Oriented Architecture, in short SOA. However I won’t dig into detail about WCF and the relevant topics such as SOA. If you are interested to find out more about this technology, check out the WCF whitepaper or feel free to ask Uncle Google for help ;).
The objective of this post is to assist and demonstrate you on how you can start WCF using Microsoft Visual Studio 2008 in just few simple steps. FYI I just started experimenting this technology yesterday so forgive me should any of these steps are not achievable.
Before I start, please make sure you have the following installed on your computer.
- Internet Information Services
- Microsoft Visual Studio 2008
To start, first of course is to launch Microsoft Visual Studio 2008 which is then followed by creating a new project. Next is to select one of the available Web Templates, that is WCF Service Application. Refer to Figure 1 below.
As depicted in figure 1, I chose C# as my preferred programming language for this project. If you prefer otherwise, then expand your desired language from the Project types panel and locate the same project WCF Service Application from the tree. Then type your preferred name. For this case JLoeService is used as the project name. Finally click Ok to confirm. Upon confirmation you should expect to see the following screenshot.
As depicted on the left, a new project called JLoeService is created along with the necessary files: IService1.cs, Service1.svc, Service1.svc.cs, and Web.config. IService1.cs refers to an interface or the service contract provided by the application. Service1.svc is merely a service markup whereas Service1.svc.cs contains the code implementation of the contract defined in IService1.cs. Finally Web.config stores all your application settings or configuration values such as connection string, service declaration, etc. I will not explain here on some of advanced features you can define in Web.config like transforming the service envelop to JSON. JSON stands for Java Script Object Notation and is very popular for integration with AJAX (Asynchronous JavaScript and XML). The collaboration gives the most lightweight interaction for data interchange between web client and server.
Enough for that. Now lets try to build and run the new project in Debug mode by clicking Play button in the toolbar or press F5. If there is a prompt like shown in figure 3 and figure 4, click Ok and Yes respectively to ignore.
Upon successful build, please take note of the URL of ASP .NET Development Server is running as depicted in a tool tip located on top of your Windows task bar. In this example, the development server is running at http://localhost:2105/. A successful build could then be verified by a launch of new Internet Explorer or other default browser with a new page as illustrated in figure 6 and figure 7. Figure 7 is achieved by clicking Service1.svc from the figure 6.
With this address, proxy class and contract could be generated by svcutil.exe which will be used by a client to access the service. Svcutil.exe could be located inside C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin folder. To generate the files, use your command prompt and move the current directory to the specified folder and run the command “svcutil.exe http://localhost:2105/Service1.svc?wdsl“. The execution will generate two new files and they are Service1.cs and output.config.
Next step is to create a simple client to access the service. To begin with, a new client project has to be created. Here I’m going to create a console application called JLoeConsole. To create, stop the running project in previous step by clicking the Stop button and then click File > Add > New Project from the menu bar to create a new Console Application and click Ok to confirm.
Upon creation, move the generated proxy files: Service1.svc and output.config to the folder where the console application was previously created. As an example, I’m going to move the files from C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin to my project folder D:\Jonathan Documents\Visual Studio 2008\Projects\JLoeService\JLoeConsole.
Remember to rename output.config to app.config as the default configuration file for any console application. And finally include these files into the project. To include, first select the Show All Files in the Solution Explorer, select the two hidden files, right-click, and select Include In Project from the right-click menu. After the inclusion we need to include new references as these references are required for the compilation. Those references are System.Runtime.Serialization and System.ServiceModel. To include these references, right click on the References under JLoeConsole tree, select the mentioned references in the .NET tab, and click Ok. Next we need to write some codes into the main function in Program.cs. Copy and paste the following codes into the function.
static void Main(string[] args)
{
Service1Client client = new Service1Client();
string output = client.GetData( 10 );
Console.WriteLine( output );
}
Now the project is ready for compilation. Select Build > Build JLoeConsole from menu bar to compile. A successful build is indicated by a text “Build succeeded” located at the bottom left of the status bar.
Finally both the service and the client are now ready for testing. To test, the service needs to run before the execution of client. To run the service, simply press the Play button or press F5 on the JLoeService project and ignore any warning prompts. Let the Internet Explorer run indefinitely to simulate as the running service. Last is to execute the client JLoeConsole.exe located in bin/Debug folder using Command Prompt. Before the execution, make sure the current directory in your Command Prompt point to the folder where the compiled JLoeConsole.exe is resided. And finally, execute the command JLoeConsole.exe. By then you should expect an output from the execution. That is “You entered: 10″ as an output in your Command Prompt. This indicates that the console application JLoeConsole has successfully communicated with the service JLoeService.
Lastly I hope the above explanation is easy to follow and you’re able to accomplish what I’ve accomplished without any unexpected encounters. I also hope this sample can help you kick-start your development on WCF. Feel free to download this example from this link. Have fun! :)
Don't leave just yet! You may also be interested to take a quick look at my other posts.










