Wednesday, March 21, 2012

.NET Remoting - Server Activated Singleton

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

Server Application

Student.cs

[sourcecode language="csharp"]
[Serializable]
public class Student
{
public int stuId;
public string stuName;
public int total;
public float avg;

}
[/sourcecode]

GradeApp.cs

[sourcecode language="csharp"]
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;
}
}
[/sourcecode]

Program.cs

[sourcecode language="csharp"]
TcpChannel tcp = new TcpChannel(8001);
ChannelServices.RegisterChannel(tcp,false);

RemotingConfiguration.RegisterWellKnownServiceType(typeof(GradeApp),"server",WellKnownObjectMode.Singleton);
Console.ReadKey();
[/sourcecode]

Client Application

Program.cs

[sourcecode language="csharp"]
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();
[/sourcecode]

No comments:

Post a Comment

How to enable CORS in Laravel 5

https://www.youtube.com/watch?v=PozYTvmgcVE 1. Add middleware php artisan make:middleware Cors return $next($request) ->header('Acces...