HomeASP.NET Developing a WCF Service Library and Hosti...
Developing a WCF Service Library and Hosting it as WCF Web Service Using VS2K8
In this article, we will focus on developing a Windows Communication Foundation (WCF) library, turning it to a WCF web service, hosting it on an IIS web server and finally test it by developing a simple WCF client using Windows Forms. This article can serve as an excellent walkthrough resource for beginners.
WPF, WCF, WWF and Silverlight are the next generation components from Microsoft which revolutionize the current .NET Framework with great new and innovative architectures. I will not look into the details of the architectures of these new components as Microsoft has already added great content on the subject on its MSDN.
Please be aware that we are not directly creating a WCF web service in this article. In fact, Visual Studio 2008 beta 2 includes two templates, "WCF Service Application" and "WCF Service Library." "WCF Service Application” itself is a direct web service application relying on WCF technology (which is similar to the ASP.NET web service development). "WCF Service Library" on the other hand is a compiled component which can be deployed as a web service or a Windows service or even as a part of a customized hosting application.
In this article, we will create a WCF service library (simply a compiled WCF library), turn it into a WCF web service, host it on IIS and finally access it using a WCF client. As we would like to create a WCF service library and host it as a web service on IIS, "web.config" will be necessary during deployment.
If you have not configured IIS after installing Visual Studio 2008 beta 2, it is highly advised that you run the following (for proper mappings and automated IIS configuration):
ServiceModelReg.exe /i /x
To make this article simple, I added the "emp" to the Northwind database.
The entire source code for this article is available in the form of a downloadable zip file. The solution was developed using Microsoft Visual Studio 2008 Beta 2 on Microsoft Windows Server 2003 Enterprise Edition. I didn’t really test it in any other environment. I request that you post in the discussion area if you have any problems with execution.
As we created a WCF Service Library (instead of a WCF Service Application), Visual Studio adds an "app.config" file (as opposed to a "web.config" as it would with a WCF Service Application) to the solution. Modify "app.config" to match the following:
Turning a Windows Communication Foundation (WCF) Service Library into a WCF Service Application (web service)
In the previous section, we worked with "app.config" to configure the WCF Service Library. To turn a WCF service library into WCF service application (or WCF Web Service), the first step is to add "web.config" to the solution and make modifications to the file to match the following (look for the "added by me" comments):
Once the "web.config" is added, the second step is to add an "EmpService.svc" file to the solution. Just add a text file (using "add new item") named "EmpService.svc" as shown below (fig 03).
Once the service file is added, the final step is to change the "build output path" of the project to "bin" (as shown below in fig 04):
Figure 04
Close all tabbed windows of Visual Studio (I suspect it could be a bug as it could not compile properly on my machine, without closing all windows) and build the solution.
Once the solution is built successfully, we need to deploy/host it on IIS. Let us start with creating a Virtual Directory in IIS to host our solution. Go through the following steps to create the same in the simplest manner:
Go to the application folder (not the solution folder), right click on it and then click on "Properties" (fig 05).
Switch to the "Security" tab and make sure that all privileges are provided for both the "ASP.NET machine account" and the "NETWORK SERVICE" accounts (fig 06).
Switch to "Web Sharing" and click on "Share this folder" (fig 07).
Accept the "default alias" ("WCFSampleService") as follows (fig 08) and click OK twice (to apply all the properties).
Figure 05
Figure 06
Figure 07
Figure 08
Deploying and Hosting WCF Service application (WCF web service), continued
Open IIS (Start || Administrative Tools || Internet Information Services Manager), and then open "Default Web Site." You should see the "WCFSampleService" configured as a web application.
Right click on "WCFSampleService" and go to "Properties" (fig 09).
Switch to the "ASP.NET" tab and select the ASP.NET version "2.0.50727" (fig 10).
Switch to "Directory Security," click on "Edit" of "Authentication and Access control," make "Enable anonymous access" checked (fig 11) and click OK twice to apply all the properties.
If the solution is configured, built and hosted properly, you should be able to access the web service from the URL http://localhost/WCFSampleService/EmpService.svc and the output should look like the following (fig 12).
To test the WCF web service created in previous sections, we need to develop a WCF client. In this case, for simplicity, I would like to work with Windows Forms.
Open a new Visual Studio environment, create a new project, select "Windows Forms Application" as the template, provide the name "WCFSampleClient" and hit OK (fig 13).
Figure 13
Design a form which looks like the following (fig 14):
Figure 14
Add a reference to WCF Service by right clicking on project and selecting "Add Service Reference" (fig 15).
Figure 15
Provide the previously created WCF Web Service URL as http://iisservername/WCFSampleService/EmpService.svc?wsdl and click "Go." Once it is found, designate the namespace as "EmpService" and hit OK (fig 16).
Figure 16
Testing the WCF Web Service: source code
For the Windows form previously created, make modifications to the existing code so it matches with the following:
Imports System.ServiceModel
Public Class Form1
Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
Dim proxy As New EmpService.EmpServiceClient("WSHttpBinding_IEmpService")
Dim objEmp As EmpService.Employee = proxy.GetEmployeeInfo(Me.txtEmpno.Text)
proxy.Close()
With objEmp
Me.txtEname.Text = .Ename
Me.txtSal.Text = .Sal
Me.txtDeptno.Text = .Deptno
End With
End Sub
Private Sub btnGetAllNames_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetAllNames.Click
Dim proxy As New EmpService.EmpServiceClient("WSHttpBinding_IEmpService")
Me.lstNames.DataSource = proxy.GetNames
proxy.Close()
End Sub
End Class
Finally, hit F5 to execute and test the application (which accesses and retrieves information from the WCF web service hosted earlier). If everything works fine, you should be able see the output as follows (fig 17):
Figure 17
I hope you enjoyed the article and any suggestions, bugs, errors, enhancements etc. are highly appreciated at http://jagchat.spaces.live.com