You are on page 1of 4

REMOTING SIN BASE DE DATOS CON CSHARP 4.

0
APLICACION REMOTING SIN BASE DE DATOS CON CSHARP 4.0
1) Proyecto de Windows: WinAppRemoting01
Solucion: SISRemoting01
Formulario: Frm_Matematicas.cs

Cada RadioButton
tendr en su propiedad
TAG el valor: 1,2,3,4
respectivamente.

Referenciar al Proyecto: Servicio y a la librera: System.Runtime.Remoting


2) Proyecto de Librera de Clases: Servicio perteneciente a la solucin: SISRemoting01
Archivo de Clase: Matematicas.cs

public class Matematicas:MarshalByRefObject


{
public int Sumar(int n1, int n2)
{
return n1 + n2;
}
public int Restar(int n1, int n2)
{
return Math.Abs(n1 - n2);
}
public int Multiplicar(int n1, int n2)
{
return n1 * n2;
}
public decimal Dividir(int n1, int n2)
{
decimal m = 0;
if (n2>0)
m=((decimal)n1 / (decimal)n2);
PROFESOR:LIAN RODRIGUEZ, JULIO CESAR

REMOTING SIN BASE DE DATOS CON CSHARP 4.0


return m;
}
}
Creamos una nueva solucin que utilice un proyecto de Consola con el nombre: Host_Servidor
Luego referenciar al resultado del Proyecto de Librera de Clase (DLL): Servicio y a la librera:
System.Runtime.Remoting, Luego en el archivo de clase escibir:

using
using
using
using
using

Servicio;
System.Runtime.Remoting;
System.Runtime.Remoting.Channels;
System.Runtime.Remoting.Channels.Tcp;
System.Runtime.Remoting.Channels.Http;

namespace Host_Remoting
{
class Program
{
static void Main(string[] args)
{
//SingleCall: Se crea una instancia de la clase para
cada llamada realizada a travs del canal (comunicacin sin
estado, como en el protocolo HTTP).
//Singleton: Existe una nica instancia de la clase
comn para todos los clientes. Este singleton sirve de gateway a
la lgica de la aplicacin.
RemotingConfiguration.RegisterWellKnownServiceType(
typeof(Matematicas), "MiServicio",
WellKnownObjectMode.SingleCall);
TcpChannel canal1 = new TcpChannel(1234);
HttpChannel canal2 = new HttpChannel(8888);
Console.WriteLine("Servidor Remoting Activado");
Console.ReadLine();
}
}
}
Volvemos al formulario: Frm_Matematicas.cs del proyecto de Aplicacin de Windows
WinAppRemoting01, y escribiremos dentro del formulario:

using
using
using
using
using

Servicio;
System.Runtime.Remoting;
System.Runtime.Remoting.Channels;
System.Runtime.Remoting.Channels.Tcp;
System.Runtime.Remoting.Channels.Http;
PROFESOR:LIAN RODRIGUEZ, JULIO CESAR

REMOTING SIN BASE DE DATOS CON CSHARP 4.0


namespace WinAppRemoting01
{
public partial class Frm_Matematicas : Form
{
public Frm_Matematicas()
{
InitializeComponent();
}
Matematicas objS = null;
private void cboOperaciones_SelectedIndexChanged(object
sender, EventArgs e)
{
try
{
// llamada simple
//objS = new Matematicas();
// llamada con Remoting
objS = Activator.GetObject(typeof(Matematicas),
"tcp://localhost:1234/MiServicio") as
Matematicas;
decimal res = 0;
int n1 = int.Parse(txtNum1.Text);
int n2 = int.Parse(txtNum2.Text);
switch (cboOperaciones.SelectedIndex)
{
case 0:
res = objS.Sumar(n1, n2);
break;
case 1:
res = objS.Restar(n1, n2);
break;
case 2:
res = objS.Multiplicar(n1, n2);
break;
case 3:
res = objS.Dividir(n1, n2);
break;
}
lblResultado.Text = res.ToString();
}
catch (Exception)
{
MessageBox.Show("Error de Conexion");
}
}

PROFESOR:LIAN RODRIGUEZ, JULIO CESAR

REMOTING SIN BASE DE DATOS CON CSHARP 4.0


private void Operacion_Matematica(object sender,
EventArgs e)
{
objS = Activator.GetObject(typeof(Matematicas),
"http://localhost:8888/MiServicio") as
Matematicas;
RadioButton r=sender as RadioButton;
decimal res = 0;
int n1 = int.Parse(txtNum1.Text);
int n2 = int.Parse(txtNum2.Text);
switch (r.Tag.ToString())
{
case "1":
res = objS.Sumar(n1, n2);
break;
case "2":
res = objS.Restar(n1, n2);
break;
case "3":
res = objS.Multiplicar(n1, n2);
break;
case "4":
res = objS.Dividir(n1, n2);
break;
}
lblResultado2.Text = res.ToString();
}
}
}

PROFESOR:LIAN RODRIGUEZ, JULIO CESAR

You might also like