Creación de un WebService en ASP.NET con C# y su consumo desde una aplicación web
Creación del WebService
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// Para permitir que se llame a este servicio web desde un script, usando ASP.NET AJAX, quite la marca de comentario de la línea siguiente.
// [System.Web.Script.Services.ScriptService]
public class Service : System.Web.Services.WebService
{
public Service () {
//Eliminar la marca de comentario de la línea siguiente si utiliza los componentes diseñados
//InitializeComponent();
}
[WebMethod(Description = "Suma de dos valores enteros")]
public int Sumar(int v1, int v2)
{
int resultado;
resultado = v1+v2;
return resultado;
}
[WebMethod(Description = "Resta de dos valores enteros")]
public int Restar(int v1, int v2)
{
int resultado;
resultado = v1-v2;
return resultado;
}
[WebMethod(Description = "Multiplicación de dos valores enteros")]
public int Multiplicar(int v1, int v2)
{
int resultado;
resultado = v1*v2;
return resultado;
}
[WebMethod(Description = "División de dos valores enteros")]
public int Dividir(int v1, int v2)
{
int resultado=0;
if (v2 != 0)
{
resultado = v1 / v2;
}
return resultado;
}
[WebMethod(Description = "Total de vocales en una cadena")]
public int VocalesEnCadena(string c1)
{
int total=0;
for (int i=0; i<c1.Length; i++)
{
if (c1.Substring(i,1).ToUpper() == "A" || c1.Substring(i,1).ToUpper() == "E" ||
c1.Substring(i,1).ToUpper() == "I" || c1.Substring(i,1).ToUpper() == "O" ||
c1.Substring(i,1).ToUpper() == "U")
{
total +=1;
}
}
return total;
}
}
Aplicación que Consume del WebService
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
MiWebService.Service ws;
protected void Button1_Click(object sender, EventArgs e)
{
if (TextBox1.Text.Length == 0)
{
Mensaje.Text = "Ingrese al menos un valor...";
return;
}
if (RadioButtonList1.SelectedIndex != 4)
{
if (TextBox2.Text.Length == 0)
{
Mensaje.Text = "Ingrese los dos valores...";
return;
}
}
ws = new MiWebService.Service();
int tot = 0;
if (RadioButtonList1.SelectedIndex == 0)
{
int resp = 0;
int val1 = int.Parse(TextBox1.Text);
int val2 = int.Parse(TextBox2.Text);
resp = ws.Sumar(val1, val2);
TextBox3.Text = resp.ToString();
}
else if (RadioButtonList1.SelectedIndex == 1)
{
int resp = 0;
int val1 = int.Parse(TextBox1.Text);
int val2 = int.Parse(TextBox2.Text);
resp = ws.Restar(val1, val2);
TextBox3.Text = resp.ToString();
}
else if (RadioButtonList1.SelectedIndex == 2)
{
int resp = 0;
int val1 = int.Parse(TextBox1.Text);
int val2 = int.Parse(TextBox2.Text);
resp = ws.Multiplicar(val1, val2);
TextBox3.Text = resp.ToString();
}
else if (RadioButtonList1.SelectedIndex == 3)
{
int resp = 0;
int val1 = int.Parse(TextBox1.Text);
int val2 = int.Parse(TextBox2.Text);
resp = ws.Dividir(val1, val2);
TextBox3.Text = resp.ToString("#0.00");
}
else if (RadioButtonList1.SelectedIndex == 4)
{
tot = (ws.VocalesEnCadena(TextBox1.Text));
TextBox3.Text = tot.ToString("##0");
}
else
{
Mensaje.Text = "Seleccione una operación...";
}
}
}