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:
    1. using System.Data;  
    2. using System.Data.SqlClient;
  6. Remove the following code snippet from the Service class:
    1. public string GetData(int value)  
    2. {  
    3.     return string.Format("You entered: {0}", value);  
    4. }  
    5. public CompositeType GetDataUsingDataContract(CompositeType composite)  
    6. {  
    7.     if (composite == null)  
    8.     {  
    9.         throw new ArgumentNullException("composite");  
    10.     }  
    11.     if (composite.BoolValue)  
    12.     {  
    13.         composite.StringValue += "Suffix";  
    14.     }  
    15.     return composite;  
    16. } 
  7. Open "App_Code/Iservice.cs" and remove the following highlighted code from the interface:
    1. [ServiceContract]  
    2. public interface IService  
    3. {   
    4.     [OperationContract]  
    5.     string GetData(int value);   
    6.     [OperationContract]  
    7.     CompositeType GetDataUsingDataContract(CompositeType composite);   
    8.     // TODO: Add your service operations here  
    9. }   
    10. // Use a data contract as illustrated in the sample below to add composite types to service operations.  
    11. [DataContract]  
    12. public class CompositeType  
    13. {  
    14.     bool boolValue = true;  
    15.     string stringValue = "Hello ";   
    16.     [DataMember]  
    17.     public bool BoolValue  
    18.     {  
    19.         get { return boolValue; }  
    20.         set { boolValue = value; }  
    21.     }   
    22.     [DataMember]  
    23.     public string StringValue  
    24.     {  
    25.         get { return stringValue; }  
    26.         set { stringValue = value; }  
    27.     }  
    28. } 
  8. Add the following namespace in the "App_Code/Iservice.cs" interface:
    1. 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:
    1. int Id = Convert.ToInt32(TextBox1.Text);  
    2. ServiceReference1.ServiceClient proxy = new ServiceReference1.ServiceClient();  
    3. GridView1.DataSource = proxy.QueryProduct(Id).Tables[0];  
    4. 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


Similar Articles