RSS

Tag Archives: server activated singleton

.NET Remoting – Server Activated Single Call

Create an object for each request of single client or multiple clients. This is stateless

Server Application

Student.cs

[Serializable]
public class Student
{
public int stuId;
public string stuName;
public int total;
public float avg;

}

GradeApp.cs

public class GradeApp : MarshalByRefObject
{
public GradeApp()
{
Console.WriteLine("A new client connected");
}

public float calcAvg(int total)
{
return total / 8;
}
public Student getAvg(int total)
{
Student s = new Student();
s.avg = calcAvg(total);
return s;
}
}

Program.cs

TcpChannel tcp = new TcpChannel(8001);
ChannelServices.RegisterChannel(tcp,false);

RemotingConfiguration.RegisterWellKnownServiceType(typeof(GradeApp),"server",WellKnownObjectMode.SingleCall);
Console.ReadKey();

Client Application

Program.cs

TcpChannel tcp = new TcpChannel(0);
ChannelServices.RegisterChannel(tcp, false);

string url = "tcp://localhost:8001/server";
RemotingConfiguration.RegisterWellKnownClientType(typeof(GradeApp),url);

GradeApp ga = new GradeApp();
Student st1 =  ga.getAvg(756);
Console.WriteLine("Average is : " + st1.avg;)
Console.WriteLine("Average is : " + st1.avg;)
Student st2 = ga.getAvg(746);
Console.WriteLine("Average is : " + st2.avg);
Console.ReadKey();
 
Leave a comment

Posted by on March 21, 2012 in C#.Net

 

Tags: , ,

.NET Remoting – Server Activated Singleton

Use the same   object for every client. This is statefull.

Server Application

Student.cs

[Serializable]
public class Student
{
public int stuId;
public string stuName;
public int total;
public float avg;

}

GradeApp.cs

public class GradeApp : MarshalByRefObject
{
public GradeApp()
{
Console.WriteLine("A new client connected");
}

public float calcAvg(int total)
{
return total / 8;
}
public Student getAvg(int total)
{
Student s = new Student();
s.avg = calcAvg(total);
return s;
}
}

Program.cs

TcpChannel tcp = new TcpChannel(8001);
ChannelServices.RegisterChannel(tcp,false);

RemotingConfiguration.RegisterWellKnownServiceType(typeof(GradeApp),"server",WellKnownObjectMode.Singleton);
Console.ReadKey();

Client Application

Program.cs

TcpChannel tcp = new TcpChannel(0);
ChannelServices.RegisterChannel(tcp, false);

string url = "tcp://localhost:8001/server";
RemotingConfiguration.RegisterWellKnownClientType(typeof(GradeApp),url);

GradeApp ga = new GradeApp();
Student st1 =  ga.getAvg(756);
Console.WriteLine("Average is : " + st1.avg;)

Student st2 = ga.getAvg(746);
Console.WriteLine("Average is : " + st2.avg);
Console.ReadKey();
 
Leave a comment

Posted by on March 21, 2012 in C#.Net

 

Tags: , ,