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 the ABC Inc wants their Web service to accommodate features such as reliable messaging, transactions, security and this web service should be accessible from any application of all platforms.

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

Prerequisite: Create tblProduct in SqlServer for this Demo.

image1.gif

Solution

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.

    image2.gif

  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.

    image3.gif

  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:

    [ServiceContract]
    public interface IService

        [OperationContract]
       
    string GetData(int value); 
        [OperationContract]
        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.
    [DataContract]
    public class CompositeType
    {
       
    bool boolValue = true;
       
    string stringValue = "Hello "
        [DataMember]
       
    public bool BoolValue
        {
           
    get { return boolValue; }
           
    set { boolValue = value; }
        } 
        [DataMember]
       
    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.

    image4.gif

  10. Type the following code for the "Service.cs" file as shown in the following figure.

    image5.gif

  11. Verifying WCF Service.

    Press F5 to run your service; you will get a browser window as shown in the following figure.

    image6.gif

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.

    image7.gif

  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.

    image8.gif

  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];
    GridView1.DataBind();
     
  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. 

    imagenew9.gif

Up Next
    Ebook Download
    View all
    Learn
    View all