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.

Advertisements
Posted in WCF. 12 Comments »

12 Responses to “WCF Sample code in asp.net 3.5”

  1. taniya Says:

    thanks

  2. Omm Says:

    Good sample for beginners.

  3. jatin Says:

    Thank simple and good to understand

    • Chandra Dev Says:

      You are welcome.

  4. rakesh de Says:

    pls give me one example of wcf project

    • Chandra Dev Says:

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

  5. Pradeep Says:

    pls give me one simple example of MVC project

    • deva Says:

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

      • Chandra Dev Says:

        I will send you on Sunday.

  6. Johnd972 Says:

    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 Says:

    Pretty! This has been an extremely wonderful post.

    Thanks for supplying this info.

  8. DanielCole Says:

    Nice post )
    http://wndbl0v3.com my blog


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: