tutorial distributed objects c#

10
Distributed Objects C# Tutorial source: http://www.codeproject.com/Articles/14791/NET-Remoting-with-an-easy- example 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.

Upload: lakatoszoltan92

Post on 18-Jul-2016

18 views

Category:

Documents


5 download

DESCRIPTION

Tutorial Distributed Objects C#

TRANSCRIPT

Page 1: Tutorial Distributed Objects C#

Distributed Objects C#

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

example

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.

Page 2: Tutorial Distributed Objects C#

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.

Page 3: Tutorial Distributed Objects C#

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

Page 4: Tutorial Distributed Objects C#

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.

Page 5: Tutorial Distributed Objects C#

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:

using RemoteObjectsLibrary.Repository; using System; using System.Collections.Generic; using System.Linq; using System.Text; using 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(); } }

Page 6: Tutorial Distributed Objects C#

}

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.

Page 7: Tutorial Distributed Objects C#

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 RemoteObjectsLibrary; using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Runtime.Remoting; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Channels.Tcp; using System.Runtime.Serialization.Formatters; using System.Text; using 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.

Page 8: Tutorial Distributed Objects C#

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)

Page 9: Tutorial Distributed Objects C#

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.

Page 10: Tutorial Distributed Objects C#

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