Tutorial Distributed Objects C#
Tutorial Distributed Objects C#
We start by creating the distributed objects. In this tutorial we create a simple object for CRUD
operations on a user table used in the MVC tutorial.
We start by adding the ADO.NET Entity object that connects to the database:
The following classes are created: User corresponding to a row of the table and UserContext
that keeps a set of User objects and is equivalent to the database table.
Now we can add a distributed object that will contain the operations that we want to perform
remotely.
using
using
using
using
using
using
namespace RemoteObjectsLibrary
{
public class RemoteObject : System.MarshalByRefObject
{
UserContext db = new UserContext();
public User addUser(User u)
{
db.Users.Add(u);
db.SaveChanges();
return u;
}
public List<User> getAllUsers()
{
return db.Users.ToList();
}
public void deleteUser(int id)
{
User user = db.Users.Single(j => j.Id == id);
db.Users.Remove(user);
db.SaveChanges();
}
}
If the User object is sent as parameter for remote methods, it must be declared as Serializable.
So the following code must be added to the class declaration:
[Serializable()]
public partial class User
To begin with, the Server application must reference the remote library created. In order to do this, we
firs BUILD the remote library, and then add it as a reference to this project.
The server application can contain a single class with a main method that publishes the objects:
using
using
using
using
using
using
using
using
using
using
using
RemoteObjectsLibrary;
System;
System.Collections;
System.Collections.Generic;
System.Linq;
System.Runtime.Remoting;
System.Runtime.Remoting.Channels;
System.Runtime.Remoting.Channels.Tcp;
System.Runtime.Serialization.Formatters;
System.Text;
System.Threading.Tasks;
namespace ServerConsoleApplication
{
class Program
{
static void Main(string[] args)
{
//The TcpServerChannel is one of the two types of channels
//supported by .NET remoting. This will set up the port number
//we want our object to respond to requests on, and the ChannelServices.
//RegisterChannel will bind that port number to the TCP/IP stack
//on the operating system.
Console.WriteLine("Starting server...");
Console.WriteLine("Opening channel ");
BinaryServerFormatterSinkProvider provider = new
BinaryServerFormatterSinkProvider();
provider.TypeFilterLevel = TypeFilterLevel.Full;
IDictionary props = new Hashtable();
props["port"] = 9937;
TcpChannel channel = new TcpChannel(props, null, provider);
ChannelServices.RegisterChannel(channel);
Console.WriteLine("Listening on channel " + ch);
Console.WriteLine("Posting remote services");
RemotingConfiguration.RegisterWellKnownServiceType
(typeof(RemoteObject),
"RemoteObject", WellKnownObjectMode.Singleton);
Console.WriteLine("Server listening...");
//do not close the console
Console.ReadLine();
}
}
}
Finally, because the application uses indirectly the database connection from the Remote Library, the
App.config file of the server application must be overwritten with the remote library app.config file (to
add the connection string to the database)
Declare a RemoteObject
Connect the remote object to the server: use object type, connection string (tcp or http + server
+ port + object name)
Call remote method
Process results
server = (RemoteObject)Activator.GetObject(typeof(RemoteObject),
"tcp://localhost:" + 9937 + "/RemoteObject");
List<User> userList = server.getAllUsers();
foreach (User u in userList)
{
listView1.Items.Add(u.Id + " " + u.nume);
}
}