Sei sulla pagina 1di 8

Creating a web service using asp.

net client and server

 To create a web service :


o Create a project by using file -> new -> project

o Create a web application by using web -> asp.net web application under .net
framework 4
o In the solution explorer right click the file name -> Add -> New Item -> web
service
o In the file created include the code for performing basic arithmetic operations
within the class

[WebMethod]
public string calculate(string first, string second, char
sign)
{
string result;
switch (sign)
{
case '+':
{
result = (Convert.ToInt32(first) +
Convert.ToInt32(second)).ToString();
break;
}
case '-':
{
result = (Convert.ToInt32(first) -
Convert.ToInt32(second)).ToString();
break;
}
case '*':
{
result = (Convert.ToInt32(first) *
Convert.ToInt32(second)).ToString();
break;
}
case '/':
{
result = (Convert.ToInt32(first) /
Convert.ToInt32(second)).ToString();
break;
}
case '%':
{
result = (Convert.ToInt32(first) %
Convert.ToInt32(second)).ToString();
break;
}
default:
result = "Invalid";
break;
}
return result;
}
 By doing this a service is created

 Now create a client to use the service create a website by the file -> new -> website
 Add the service reference to the created website by right clicking the website name in
the solution explorer ->

 You will get the following screen


 Add the service by running the web service created before and copy the url

 Paste the url in the add service dialog box in the address textbox and click go
 Then you get the notification as service is running name the namespace and click OK

 Now design the default.aspx page


 Double click the button to write the action code for the button and insert the following
snippet in the default.aspx.cs file

protected void Button1_Click(object sender, EventArgs e)


{
String a = TextBox1.Text;
String b = TextBox2.Text;
char c = Convert.ToChar(TextBox3.Text);
TextBox4.Text = ws.calculate(a, b, c);
}
 Create a object for the added service namespace

Test.WebService1SoapClient ws = new
Test.WebService1SoapClient();
 Run the web application for getting the output

Potrebbero piacerti anche