Tuesday, August 25, 2009

Creating WCF Service

Add this code to Your App_Code/IService.cs Page
---------------------------------------------------
[ServiceContract]
public interface IService //This Will be By Dafault.
{
[OperationContract]
int AddVal(int i, int j);
}
public class TestClass
{
[DataMember]
public int IntVal1
{
get { return IntVal1; }
set { IntVal1 = value; }
}
[DataMember]
public int IntVal2
{
get { return IntVal2; }
set { IntVal2 = value; }
}
}
----------------------------------
2. Add the Below code to your App_Code/Service.cs File.
----------------------------------------------

public class Service : IService
{
#region IService Members
public int AddVal(int i, int j)
{
int k = i + j;
return k;
}
#endregion
}
---------------------
3. Build This Service.

4. Open IIS (Start==>Run==>Inermgr.

5. Goto the WebSite--> Default Website.

6. Right Click on Default Web Site==>New==>Virtual Directory.
7. Click Next, Give a Alise Name, Select the WCF Service Directory URL.
8. Give all the Permissions.
9. right Click on you Service Name, you just Created.
10. Click brows.
11. Copy the URL.
12. Open a new ASP.Net Web Application to Consume this Service.
13. Right Click on Solution Explorer, Add Service Reference.
14. Paste the URL, Click Add.
15. Now the Service has been added to you Project.
16. Now use the following code.
in your aspx page.
-------------------

in your .cs file
-------------------

ServiceClient s = new ServiceClient();
protected void btnAdd_Click(object sender, EventArgs e)
{
int i, j, k;
i = Convert.ToInt32(txtNum1.Text);
j = Convert.ToInt32(txtNum2.Text);
k = s.AddVal(i, j);
txtResult.Text = k.ToString();
}
}
}

Noe you're Done.

Build and Execute this.