Como crear y consumir un web service en Asp.net

Como crear y consumir un web service en Asp.net

Hoy te muestro como crear y consumir un web service en Asp.Net y Visual Studio paso a paso en video, te muestro el código para que lo analices sin tener que copiarlo del video y te pongo el ejemplo en descarga directa. Los Web Service son algo fundamental hoy en dia en el mundo del desarrollo web, si aun no has escuchado sobre estos o no sabes como crear y consumir un web service este es tu lugar, estos aportan interoperabilidad entre aplicaciones, protocolos y estándares, permiten que servicios y software de diferentes compañías y ubicadas en diferentes lugares puedan ser combinados fácilmente, etc.. ¿Quieres saber como crear y consumir un web service? Sigue leyendo..

Crear y Consumir un Web Service Asp.net – VS | Parte 1

Crear y Consumir un Web Service Asp.net – VS | Parte 2

Codigo

Operaciones.cs (Proyecto WebService)

[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 Operaciones : System.Web.Services.WebService {

public Operaciones () {

//Elimine la marca de comentario de la línea siguiente si utiliza los componentes diseñados 
//InitializeComponent(); 
}

[WebMethod]
public int Sumar(int x, int y)
{
return x + y;
}

[WebMethod]
public int Restar(int x, int y)
{
return x - y;
}

[WebMethod]
public int Multiplicar(int x, int y)
{
return x * y;
}

[WebMethod]
public int Dividir(int x, int y)
{
return x / y;
}

}

Default.aspx (Proyecto WebService Consumidor)


¿Sabías que?

¿Necesitas aprender algo nuevo ? Ve a Udemy, busca el tema del que quieres aprender, selecciona el curso para ti y continua con tu aprendizaje.

Udemy

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table border="2" cellpadding="2" cellspacing="2">
<tr>
<td align="right">
<asp:Label ID="Etiqueta1" runat="server" Text="Ingresa el primer numero"></asp:Label>
</td>
<td align="left">
<asp:TextBox ID="txtPrimerNumero" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td align="right">
<asp:Label ID="Etiqueta2" runat="server" Text="Ingresa el segundo numero"></asp:Label>
</td>
<td align="left">
<asp:TextBox ID="txtSegundoNumero" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td align="right">
<asp:Label ID="Etiqueta3" runat="server" Text="Resultado"></asp:Label>
</td>
<td align="left">
<asp:Label ID="EtiquetaResultado" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td align="center">
<asp:Button ID="btnSumar" runat="server" Text="Sumar(+)" OnClick="btnSumar_Click" />
</td>
<td align="center">
<asp:Button ID="btnRestar" runat="server" Text="Restar(-)" OnClick="btnRestar_Click" />
</td>
</tr>
<tr>
<td align="center">
<asp:Button ID="btnMultiplicar" runat="server" Text="Multiplicar(*)" OnClick="BtnMultiplicar_Click" />
</td>
<td align="center">
<asp:Button ID="btnDividir" runat="server" Text="Dividir(/)" OnClick="btnDividir_Click" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>

Default.aspx.cs (Proyecto WebService Consumidor)

using WSOperaciones;

public partial class _Default : System.Web.UI.Page
{
OperacionesSoapClient operaciones = new OperacionesSoapClient();
int a, b, c;
protected void Page_Load(object sender, EventArgs e)
{

}

protected void btnSumar_Click(object sender, EventArgs e)
{
a = Convert.ToInt32(txtPrimerNumero.Text);
b = Convert.ToInt32(txtSegundoNumero.Text);
c = operaciones.Sumar(a,b);
EtiquetaResultado.Text = c.ToString();
}

protected void btnRestar_Click(object sender, EventArgs e)
{
a = Convert.ToInt32(txtPrimerNumero.Text);
b = Convert.ToInt32(txtSegundoNumero.Text);
c = operaciones.Restar(a, b);
EtiquetaResultado.Text = c.ToString();
}

protected void BtnMultiplicar_Click(object sender, EventArgs e)
{
a = Convert.ToInt32(txtPrimerNumero.Text);
b = Convert.ToInt32(txtSegundoNumero.Text);
c = operaciones.Multiplicar(a, b);
EtiquetaResultado.Text = c.ToString();
}

protected void btnDividir_Click(object sender, EventArgs e)
{
a = Convert.ToInt32(txtPrimerNumero.Text);
b = Convert.ToInt32(txtSegundoNumero.Text);
c = operaciones.Dividir(a, b);
EtiquetaResultado.Text = c.ToString();
}
}

Descargar Proyecto Completo

Descargar Proyecto Completo

Basado en how to create a simple web service and use it in Asp Net (Ingles)

Como crear y consumir un web service en Asp.net

Clic para valorar esta información
[Total: 0 Promedio: 0]