Wednesday, March 21, 2012

.NET Remoting - Client Activated Objects

Create an object for each client. Use the same object for multiple requests from the same client.

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.RegisterActivatedServiceType(typeof(GradeApp));
Console.ReadKey();
[/sourcecode]

Client Application

Program.cs

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

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

GradeApp ga = new GradeApp();
Student st1 =  ga.getAvg(756);
Console.WriteLine("Average is : " + st1.avg);
[/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...