Creating a Simple WCF Service

Problem Statement

Assume that ABC Inc. is one of the leading mobile manufacturing companies in India. Currently, ABC Inc uses an XML-based web service that provides the details of the various mobile models available for sale. The management of ABC Inc wants their Web service to accommodate features such as reliable messaging, transactions, and security, and this web service should be accessible from any application of all platforms.

The developers of ABC Inc. know that they can fulfill all the above requirements by developing the service in WCF.


Create tblProduct in SqlServer for this Demo.



Task 1. Creating a WCF Service

To create a WCF service you need to perform the following tasks.

  1. Open Microsoft Visual Studio.
  2. Select "File" - "New" - "Website". A dialog box appears, as shown in the following figure.
  3. Select the WCF Service template under the Visual Studio installed templates section.
  4. Click on the "OK" Button. The "App_Code/Service.cs" file appears, as shown in the following figure.
  5. Add the following namespaces to the "App_Code/Service.cs" file.
    using System.Data;
    using System.Data.SqlClient;
  6. Remove the following code snippet from the Service class.
    public string GetData(int value)  
        return string.Format("You entered: {0}", value);  
    public CompositeType GetDataUsingDataContract(CompositeType composite)  
        if (composite == null)  
            throw new ArgumentNullException("composite");  
        if (composite.BoolValue)  
            composite.StringValue += "Suffix";  
        return composite;  
  7. Open "App_Code/Iservice.cs" and remove the following highlighted code from the interface.
    public interface IService
        string GetData(int value);
        CompositeType GetDataUsingDataContract(CompositeType composite);
        // TODO: Add your service operations here
    // Use a data contract as illustrated in the sample below to add composite types to service operations.
    public class CompositeType
        bool boolValue = true;
        string stringValue = "Hello ";
        public bool BoolValue
            get { return boolValue; }
            set { boolValue = value; }
        public string StringValue
            get { return stringValue; }
            set { stringValue = value; }
  8. Add the following namespace in the "App_Code/Iservice.cs" interface.
    using System.Data;
  9. Type the following code snippet for the "Iservice" interface as shown in the following figure.
    Code snippet
  10. Type the following code for the "Service.cs" file as shown in the following figure.
  11. Verifying WCF Service: Press F5 to run your service; you will get a browser window as shown in the following figure.
    WCF Service

Task 2. Creating ASP.Net client application

  1. Open Visual Studio then click on "File" - "New" - "Web Site" - "ASP.Net website".
  2. Drag and drop a TextBox, Button, and GrideView as shown in the following figure.
     ASP.Net Client
  3. Switch to the Solution Explorer. Right-click on the root directory (the solution) and click on "Add Service Reference". Provide the WSDL file link in the Address TextBox, as shown in the following figure.
    Solution Explorer
  4. Click the "OK" Button.
  5. For the Click event of the Show Button write the following code.
    int Id = Convert.ToInt32(TextBox1.Text);
    ServiceReference1.ServiceClient proxy = new ServiceReference1.ServiceClient();
    GridView1.DataSource = proxy.QueryProduct(Id).Tables[0];
  6. Press F5 to run your ASP.Net application. Enter a product ID in the TextBox and click on the Show Button. If everything goes well you will get the following output as shown in the following figure.

Similar Articles