0% found this document useful (0 votes)
134 views

Tutorial Distributed Objects C#

This document provides instructions for creating a .NET remote object that allows performing CRUD operations on a database table from a client application. It describes creating a class library containing the remote object and entity classes linked to the database table. A server console application is created to host the remote object. Finally, a Windows Forms client application is created that references the class library to call methods on the remote object to retrieve and display data from the database table.

Uploaded by

lakatoszoltan92
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
134 views

Tutorial Distributed Objects C#

This document provides instructions for creating a .NET remote object that allows performing CRUD operations on a database table from a client application. It describes creating a class library containing the remote object and entity classes linked to the database table. A server console application is created to host the remote object. Finally, a Windows Forms client application is created that references the class library to call methods on the remote object to retrieve and display data from the database table.

Uploaded by

lakatoszoltan92
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Distributed Objects C#

Tutorial source: http://www.codeproject.com/Articles/14791/NET-Remoting-with-an-easyexample


Prerequisites: VS 2012 and SQL Server 2012.

1. Create the server side application


a) Create the server solution
Create a simple Console application project that will keep the server and the remote objects.

b) Create a class library for the distributed objects


In the newly created solution we can add another project. This project will keep the Remote Objects.

The project type will be Class Library.

c) Create the distributed objects


Now the solution explorer contains 2 projects. The main project is the server application.

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:

Create a repository folder

Add the ADO.NET Entity data model

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

The class must be declare public


It must extend the MarshallByReff class
We define the basic operations on the user table by using a DbContext object created above.
The code for the class is the following:
RemoteObjectsLibrary.Repository;
System;
System.Collections.Generic;
System.Linq;
System.Text;
System.Threading.Tasks;

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

d) Create the server application


The server application has two main roles:

publish the remote objects


wait for requests from the possible clients.

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.

Secondly, the application must reference the System.Runtime.Remoting library.

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)

2. Create the client application


a) Create a basic GUI
Create a simple Windows Forms application.

Create a simple interface to test the methods provided by the server.

b) Add reference to the class library


Add a reference to the .dll file from the remoteLibrary/bin/debug. Each time the remote library is
modified, it must be rebuilt and the reference must be updated. VS2012 automatically updates the
references after the library is rebuilt.

c) Call the remote objects


For instance when pressing a button return the list of objects from the table:

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

private RemoteObject server;

private void button3_Click(object sender, EventArgs e)


{

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);
}
}

You might also like