Friday, 20 January 2012

How To make a Web Services ( To Calculate Circle area using C#),

How To make a Web Services ( To Calculate Circle area using C#)
Step 1. Start a New Web Site


Click on ASP.Net Web Services and named your service CircleArea and press on OK
Step 2. The code written for your application which return area of circle (Service.cs). This service contain a CircleArea method which calculate the area.




using System;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{
    public Service () {  }
 [WebMethod (Description="To calculate Area of Circle")]
    public double CircleArea(int a) 
    {return 3.14 * (a * a);}
}
Step 3. Run your service.


Click on CircleArea. Service is run and when you input value the result be shown.

If you want to see Service Description. Click on Service Description







Create a Web Service For Calculate Age when user input Date
Step 1. Open a new Web Site and click on ASP.Net Web Services press on OK as shown below in fig.



Step 2. The code (Service.cs) written for Calculating Age , Calculate Factorial and Square Root of a Number.

using System;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{[WebMethod (Description="Calculating Age")]
    public string calAge(DateTime date)
    {   DateTime nowDate = System.DateTime.Now;
        System.TimeSpan span = nowDate - date;
        int days = (int)span.TotalDays;
        int yea = days / 365;
        string age = yea.ToString() + " years ";
        return age;}
    [WebMethod(Description = "Calculating SquareRoot")]
    public double calSquareRoot(int a)
    {   double i = 0;
        double x1, x2=0;
        while ((i * i) <= a)
        i += 0.1;
        x1 = i;
        for (int j = 0; j < 10; j++)
        {   x2 = a;
            x2 /= x1;
            x2 += x1;
            x2 /= 2;
            x1 = x2;}
        return x2;}
    [WebMethod(Description = "Calculating Factorial")]
    public double calFactorial(int a)
    {if (a == 0)return 1;
        else return (a*calFactorial(a-1));}}

Step 3. Debug The service

Click on each link and check your service
Step 4. To use this service, go to your project solution right click and click on Add Web Reference. A new window open

Copy the URL of your running web service and paste it in the new window place as shown in below fig.


Note: Remember the web reference name, it will be added in your project (.cs ) file e.g. this reference help to call the method of your web services. You will call the methods to create an object of SERVICE. To know more click Here
using localhost;
Click on Add Reference, The service will be added in your web site. It will be show on your page like below fig.





How to Use web Services in ASP. Net using C#
Step 1. Run Your Service and open a new Website, Add a label and one textBox to enter radius of Circle

Click on Add web Reference

Copy the URL of Your running web Service and paste it in Your Web Reference window

Click on Go
Step 2. You can change your Web reference name but you remember this. click on add Reference

The service will be added in your application

Step 3. Now add the Web reference (name space)  in your application (shown below). written the following code in your .cs file (Default.aspx.cs).
using System;
using System.Configuration;
using System.Web;
using System.Data;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using localhost;
public partial class _Default : System.Web.UI.Page 
{
    Service ser = new Service();
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        double area = ser.CircleArea(int.Parse(TextBox1.Text));
        Label2.Text = "Area is :  " + area.ToString();
    }
}
 Step 4. Run your application (Necessary to running your web service which you use in your application)







How to Use web Services in Console Application using C#
Step 1. Open a new project and select console Application. press on Ok.

Step 2. Click on Add service reference a new window open Click on Advance,

A new window  open click on Add Web reference and copy the URL of your running service and paste it to the URL space in new window shown below and click on Go. Click on Add reference but remember your Web reference name

The Service will be added in your project

Step 3. Written the following code in your Program (Program.cs)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
namespace Circlearea
{
    class Program
    {
       static void Main(string[] args)
        {
            localhost.Service lo = new localhost.Service();
            Console.Write("Please enter the value=");
            int area = Convert.ToInt32(Console.ReadLine());
            double fr=lo.CircleArea(area);
            Console.WriteLine("Area is =" + fr);
            Console.ReadLine();
        }
    }
}
Step 4. Run the Application




Post a Comment