WCF Sample code in asp.net 3.5


Hi

Here are some few steps to create WCF in asp.net 3.5

step1.

Create one asp.net project like this

Step2:

Add WCF file like this

Step 3:

Add the operation contract

int Add(int a, int b);

in  IService.cs file. as shown in Image

Step4

Go to Service.Cs file and click on “IService” as shown in Image

then Click on Explicitly Implement Interface.

then it will create interface add method.

Step 5: Write the code like this

int IService.Add(int a, int b)
{
return (a + b);
}

Step 6: For checking the service,click F5 you will get output like this

Step 7: Add Service Reference like this

Step 8:Click on Discover and Give the Namespace name like this

Step 9:Click on advance and Select “Generate Asyncronous Communication” like this

Step 10: Click ok.  Then Come to “Default page” and keep there 2 textbox and one Button control like this

<%@ Page Language=”C#” AutoEventWireup=”true”  CodeFile=”Default.aspx.cs” Inherits=”_Default” %>

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”&gt;

<html xmlns=”http://www.w3.org/1999/xhtml”&gt;
<head runat=”server”>
<title></title>
</head>
<body>
<form id=”form1″ runat=”server”>
<div>
<asp:TextBox ID=”TextBox1″ runat=”server”></asp:TextBox>

<asp:TextBox ID=”TextBox2″ runat=”server”></asp:TextBox>
<br />
<br />
<asp:Button ID=”Button1″ runat=”server” onclick=”Button1_Click” Text=”Button” />
</div>
</form>
</body>
</html>

Step 11. Click on” Button 1″ on write the following code in code behind for calling WCF service.

protected void Button1_Click(object sender, EventArgs e)
{
WcfTest.ServiceClient ws = new WcfTest.ServiceClient();
int result = ws.Add(int.Parse(TextBox1.Text), int.Parse(TextBox2.Text));
Response.Write(result);
ws.Close();
}

Step 12:

Now compile the code and pass the input in two textbox, you will get Output like this

I hope this will help to some one.

12 thoughts on “WCF Sample code in asp.net 3.5

  1. taniya June 28, 2011 / 9:16 pm

    thanks

  2. Omm July 26, 2011 / 7:44 am

    Good sample for beginners.

  3. jatin May 25, 2012 / 7:08 am

    Thank simple and good to understand

  4. rakesh de June 8, 2012 / 9:11 am

    pls give me one example of wcf project

    • Chandra Dev June 9, 2012 / 1:06 pm

      I m creating the sample project on wcf. I will share with you.

  5. Pradeep August 28, 2012 / 4:52 am

    pls give me one simple example of MVC project

    • deva May 28, 2013 / 5:15 am

      plz give me one 3tier asp.net with wcf sample

      • Chandra Dev May 28, 2013 / 7:13 pm

        I will send you on Sunday.

  6. Johnd972 May 28, 2014 / 6:29 pm

    I just ought to tell you which you have written an exceptional and distinctive post that I really enjoyed reading. Im fascinated by how nicely you laid out your material and presented your views. Thank you. gfegefbfddee

  7. Lolita June 10, 2014 / 8:16 pm

    Pretty! This has been an extremely wonderful post.

    Thanks for supplying this info.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.