Tuesday, May 19, 2015

wcf demo appication-part 4


Please read my previous article :


  WCF Service-Part 1

 WCF Features-Part 2

 ABC of WCF-Part 3


Let's make a WCF application. In this application we are trying to add two numbers using a WCF service. I am using Visual Studio 2012. Open Visual Studio and go to "File" -> "New" -> "Project..." and select WCF Service Application and name it myFirstApp.

Visual Studio provides some auto-generated code. Let's delete the IService1.cs and Service1.svc files. Add a WCF service file and name it MyService.svc and provide it the following code. It adds the following two files.
  1. IMyService.cs
  2. MyService.svc
Delete all code of ImyService and write the following code.

  1. using System.ServiceModel;  
  2.   
  3. namespace myFirstApp  
  4. {  
  5.    [ServiceContract]  
  6.    public interface IMyService  
  7.    {  
  8.       [OperationContract]  
  9.       int AddTwoNo(int intFirstNo, int intSecondNo);  
  10.    }  
  11. }  
ServiceContract describes which operation the client can perform on the service.

OperationContract is defined within a ServiceContract. It defines the parameters and return type of the operation.

Now implement the ImyService interface in MyService.svc.cs. Let's delete all the code first from MyService.svc.cs and write the following:
  1. namespace myFirstApp  
  2. {  
  3.    public class MyService : IMyService  
  4.    {  
  5.       public int AddTwoNo(int intFirstNo, int intSecondNo)  
  6.       {  
  7.          return intFirstNo + intSecondNo;  
  8.       }  
  9.    }  
  10. }  
Hit F5 to run this application.

WCF test client

After running it successfully double-click on AddTwoNo() and fill in the value in intFirstNo and intSecondNo respectively. I am putting 5 and 6 and hitting the invoke button.

design view

Output:

response

Note: The default binding in WCF is basicHttpBinding. We can see all details of this service at http://localhost:36246/myservice.svc that is shown in the preceding image.

Now I am calling this WCF service using an ASP.NET application.

Open a new Visual Studio instance and go to "File" -> "New" -> "Project..." and select ASP.NET Empty Web Application and name it WCFClientApp.

Add a webform and name it default.aspx and add the service reference.

Right-click on the project (WCFClientApp).

add service reference

Click on Add service reference and fill in the address and click go. Then change the namespace as you want and click OK. I am keeping the WCFReference namespace.

my service

In default.aspx
  1. <table>  
  2.    <tr><td>First No</td><td><asp:TextBox ID="txtFirst" runat="server"></asp:TextBox></td></tr>  
  3.    <tr><td>Second No</td><td><asp:TextBox ID="txtSec" runat="server"></asp:TextBox></td></tr>  
  4.    <tr><td colspan="2"><asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="btnAdd_Click" /></td></tr>  
  5.    <tr><td colspan="2"><asp:Label ID="lblResult" runat="server"></asp:Label></td></tr>  
  6. </table>  
In default.aspx.cs
  1. using System;  
  2.   
  3. namespace WCFClientApp  
  4. {  
  5.    public partial class _default : System.Web.UI.Page  
  6.    {  
  7.       protected void btnAdd_Click(object sender, EventArgs e)  
  8.       {  
  9.          int intFirstNo = 0, intSecNo = 0, intResult = 0;  
  10.          intFirstNo = Convert.ToInt16(txtFirst.Text);  
  11.          intSecNo = Convert.ToInt16(txtSec.Text);  
  12.          WCFReference.MyServiceClient client = new WCFReference.MyServiceClient();  
  13.          intResult = client.AddTwoNo(intFirstNo, intSecNo);  
  14.          lblResult.Text = "Result is :"+intResult;  
  15.       }  
  16.    }  
  17. }  
Run the application.

Output

output

I hope this article is helpful for you.

If you are searching life partner. your searching end with kpmarriage.com. now kpmarriage.com offer free matrimonial website which offer free message, free chat, free view contact information. so register here : kpmarriage.com- Free matrimonial website