I am betting that you have at least one if not more ASMX services available today within your applications. So how do you get going with Windows Communication Foundation (WCF) and maintain that existing code? In this article we will look at an example of how this can be done. We will start with an existing ASMX service and add the necessary configuration details to this service so that it talks both as an ASMX and WCF service. One advantage of this strategy is that existing ASMX clients are unaffected and your code is reusable within the WCF Service.
Updating the Service
In this example we start with a basic ASMX Web Service that contains the following files and exposes a single <WebMethod>.
Fundamentally, hosting WCF services within ASP.NET is very easy to do as we explored here and very similar to the ASMX model. You can either place your entire service implementation in a *.svc file just as with ASP.NET Web services *.asmx files, or you can reference a service implementation residing in a code-behind file or some other assembly. Fundamentally, creating WCF services doesn’t fundamentally differ for how you would typically create an ASMX Web Service. Even the attributes of the @Service directive are very similar to those for the @WebService directive. Technically one of the major differences to keep in mind between a WCF and ASMX service is that the WCF service doesn’t do anything until you specify how it should be exposed. ASMX services on the other hand are designed to start talking with the outside world once you place the *.asmx file into an IIS virtual directory.
The steps needed to modify this ASMX Web service to include WCF.
1. Set a reference to the system.Servicemodel namespace to include WCF
2. Next add in the WCF contract and end point information that is needed to expose the code through WCF. In this example, We started with the following code
Added in the <ServiceContract> and <OperationContract> information as shown by the arrows.
3. Add the .svc file. This is the main entry point for the WCF service. There are several ways to do this. In this example I added a text file and renamed it as shown below.
4. Edit the service.svc file and add the following declaration
<%@ ServiceHost Language="VB" Debug="true" Service="Service" CodeBehind="~/App_Code/service.vb" %>
5. Update the Web.Config file to include the WCF entries
As you can see WCF is definitely an evolution that can use existing ASMX technologies and code. Once you have completed this exercise you solution explorer should include the following. I have marked those files that are added or modified for WCF.
In order to test the changes and validate that you have done it correctly. You can select the service.svc file in the solutions explorer.
Start the application and you will be see the directory listing page
Click the service.svc file and you are running the WCF service
Click the service.asmx file and you are running the ASMX Web Service
Building the Client
Once the server is complete we can connect to the same code using either an ASMX Web Service reference or a WCF Service reference. In this example we will do this through a Windows Form project added to the existing solution.
Connect using the ASMX Web Service
1. Add a reference to the ASMX page using Add Web Reference
2. Select Web Services in this solution
3. Add the Web Reference
4. Add a button to the main form and enter the following code behind the button to call the Web Service using ASMX
Connect using the WCF Service
1. Adding the Service reference for the WCF service is similar to what we did for the ASMX page except we are adding a Service Reference.
2. Add the Service Reference information
This initiates the svcutil command that retrieves the configuration files needed to connect to the WCF service.
Once this is done the WCF configuration files are added to the Windows project.
3. Add another button to the project and add the following code to it.
In this article we looked at how an existing ASMX Web Service can be used to implement a WCF Service. Also, how the same client application can be used to connect to either type of implementation. Of course this was a simple example of some of the features available. The code example shown can be downloaded from here.