473,320 Members | 1,978 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Remoting Tutorial?

Can anyone suggest a good (current) tutorial on how to do basic
remoting with C# (2005 express edition)?

Dec 15 '06 #1
4 6603
There are some on CodeProject. Here's a mini program that won't run as
I've cut out all the gui stuff and replaced with ..., but it should
give you the idea. There are four projects in the solution, Client,
Server, Interface and Shared, the salient code for each is below.
Client, Server and Shared reference Interface, Server also references
Shared.
Server creates a real object, Client uses an interface so it is never
exposed to the remote object directly, Interface contains that
interface and shared the real implemenation of it that allows the
server to do things.
There are a lot of comments though, I'd trim it down properly but I
must go now, sorry!

Server-------------------------------------------------------------------------------------

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Messaging;
using System.Runtime.Remoting.Channels.Tcp;

namespace Ian.Remoting
{
public class ServerGUI : System.Windows.Forms.Form
{
private delegate void UpdateTextDelegate(string pText);

//ObjRef transfers an object reference across appdomain boundaries
private ObjRef _refCalcRequest;
//Our Remoted object Remoted is the class name, it's not some cunning
keyword.
private Remoted _calcRequest;

...
private void ServerGUI_Load(object sender, System.EventArgs e)
{
//Set up the channel.
ChannelServices.RegisterChannel(new TcpChannel(50050));
//Using singleton model to get our Remoted instance.
_calcRequest = Remoted.Instance;
//Get the ObjRef
_refCalcRequest = RemotingServices.Marshal(_calcRequest, "Remoted");
//Hook up to the event so we know when a client is trying to do a
calculation
_calcRequest.CalcRequestEvent+=new
Ian.Remoting.Remoted.CalcRequestEventHandler(_calc Request_CalcRequestEvent);
}

private void _calcRequest_CalcRequestEvent(object sender,
CalcRequestEventArgs e)
{
Random r = new Random();
//Invoke should always be required in this scenario, but this is
good practice.
if(this.InvokeRequired)
{
//Thread safely update the GUI
this.BeginInvoke(new UpdateTextDelegate(UpdateText), new object[]
{e.Name + " Submitted: " + e.Calculation});
}
//Populate the EventArgs results field so when we exit the Remoted
can return the results.
e.Results = new double[] {r.NextDouble() * 10 , r.NextDouble() *
10};
}
private void UpdateText(string pText)
{
textBox1.Text+="\r\n" + pText;
}
}
}
Shared------------------------------------------------------------------------------------------
namespace Ian.Remoting
{
public class Remoted : MarshalByRefObject , IRemoted
{
//The event and delegate used when the user calls calculate. This
notifies the server.
public delegate void CalcRequestEventHandler(object sender,
CalcRequestEventArgs e);
public event CalcRequestEventHandler CalcRequestEvent;

//Singleton
private static Remoted _instance;

private Remoted()
{
}
public static Remoted Instance
{
get
{
if(null==_instance)
{
_instance=new Remoted();
}
return _instance;
}
}
public override Object InitializeLifetimeService()
{
// Allow this object to live "forever". Even singletons are
collected after a default of 5 minutes normally.
return null;
}
double[] IRemoted.Calculate(string pCalculation, string pName)
{
//The user has access to IRemoted and has called Calculate on it. We
manage concurrency by creating a new EventArgs to hold
//our info then raise the event which the server consumes.
CalcRequestEventArgs calcRequest = new
CalcRequestEventArgs(pCalculation, pName);

if(null != CalcRequestEvent)
{
CalcRequestEvent(this,calcRequest);
//This would of been populated by the server.
return calcRequest.Results;
}
else
{
return new double[] {-1.0};
}
}
}

public class CalcRequestEventArgs : EventArgs
{
private string _calculation;
private double[] _results;
private string _name;

public string Name
{
get
{
return _name;
}
set
{
_name = value;
}
}
public string Calculation
{
get
{
return _calculation;
}
}
public double[] Results
{
get
{
return _results;
}
set
{
_results = value;
}
}
public CalcRequestEventArgs(string pCalculation, string pName)
{
_calculation = pCalculation;
_name = pName;
}
}

}

Interface
---------------------------------------------------------------------------------------------

using System;

namespace Ian.Remoting
{

public interface IRemoted
{
double[] Calculate(string pCalculation, string pName);
}
}

Client
-----------------------------------------------------------------------------------------------------------------

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Messaging;
using System.Runtime.Remoting.Channels.Tcp;

namespace Ian.Remoting
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class ClientGUI : System.Windows.Forms.Form
{

private IRemoted _remoted; //Remoted implements IRemoted. We hold a
reference the the interface not the concrete object

private delegate void SetTextDelegate(string pText); //Used to
thread safe update the GUI as we're using Async callbacks
private delegate double[] DoCalculationDelegate(string pName, string
pCalculation); //Used to call the Calculate method on IRemoted

...

private void button1_Click(object sender, System.EventArgs e)
{
txtResults.Text = string.Empty;

//Our Async callback will call CalculationCallback on completion
AsyncCallback acb = new AsyncCallback(this.CalculationCallback);
//DoCalculationDelegate has the same signature as IRemoted.Calculate
DoCalculationDelegate d = new
DoCalculationDelegate(_remoted.Calculate);
//Invoke the Delegate with the parameters. the AsyncCallback and
null (not sure what object represents)
IAsyncResult ar = d.BeginInvoke(txtName.Text,txtCalculation.Text,
acb, null);

}
public void CalculationCallback(IAsyncResult ar)
{
//The call to the remote object has ended and called here to let us
know.
//Get our delegate out so we can end it.
DoCalculationDelegate del =
(DoCalculationDelegate)((AsyncResult)ar).AsyncDele gate;
try
{
//del's signature is the same as calculate, so we expect an array
of doubles out.
double[] results = (double[])del.EndInvoke(ar);
string output="";
foreach(double r in results)
{
output+=r.ToString() + " ";
}
//Thread safely call Updateoutput with our new output.
this.BeginInvoke finds this control's thread and fires the delegate on
it.
this.BeginInvoke(new SetTextDelegate(UpdateOutput),new
object[]{output});
}
catch(Exception e)
{
this.BeginInvoke(new SetTextDelegate(UpdateOutput),new
object[]{e.Message});
}
}
private void UpdateOutput(string pText)
{
txtResults.Text = pText;
}
private void ClientGUI_Load(object sender, System.EventArgs e)
{
//Register a channel
ChannelServices.RegisterChannel(new TcpChannel());
//retrieve the remote object. On the server side it will be a
concrete object of type Remoted
_remoted = (IRemoted)Activator.GetObject(typeof(IRemoted),
"tcp://localhost:50050/Remoted");
}
}
}


Rich wrote:
Can anyone suggest a good (current) tutorial on how to do basic
remoting with C# (2005 express edition)?
Dec 15 '06 #2

Perhaps I have a more basic problem. I have tried the TCP approach (as
you show here) and I have tried an IPC approach. The first problem I
have is that VS2005 Express chokes on either of the following:

using System.Runtime.Remoting.Channels.Ipc;
or
using System.Runtime.Remoting.Channels.Tcp;

I get a compile error:
The type or namespace name 'Ipc' (or 'Tcp') does not exist in the
namespace 'System.Runtime.Remoting.Channels' (are you missing an
assembly reference?)

Is this a limitation of the Express version?

Thanks,

Dec 16 '06 #3
JS
I'm not sure what the express edition supports, but check that you have
added a reference to the System.Runtime.Remoting dll.

Dec 16 '06 #4
Yep, I got exactly the same error the first time I palyed with
remoting, and it's because you need to add the reference. I checked in
Express and the assembly is available and can be added to the project.

Something I wanted to say the other day was the above hacked up example
is a Server activated object, there are also client activated objects.
The difference is basically SAO's can't hold state, but CAO's can. That
might help when you're googling.
As mentioned previously, there are lots of tutorials on CodeProject.
It's a while ago, but I think the code above was based on a couple of
their tutorials. It's part of a series of tech primers I wrote for
work.
JS wrote:
I'm not sure what the express edition supports, but check that you have
added a reference to the System.Runtime.Remoting dll.
Dec 17 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: michael_hk | last post by:
Hi, I am learning .NET remoting and following the tutorial http://www.csharphelp.com/archives2/archive460.html Everything works fine. Now I want to modify it a bit for my personal use but...
1
by: f00sion | last post by:
I found a good tutorial of how to supply the objects without having the implementation files on the client. This was working great until I realized that I couldnt use any constructors with server...
0
by: bettervssremoting | last post by:
To view the full article, please visit http://www.BetterVssRemoting.com Better VSS Remote Access Tool This article makes a detailed comparison among SourceAnyWhere, SourceOffSite, VSS...
5
by: mayamorning123 | last post by:
A comparison among six VSS remote tools including SourceOffSite , SourceAnyWhere, VSS Connect, SourceXT, VSS Remoting, VSS.NET To view the full article, please visit...
0
by: bettervssremoting | last post by:
To view the full article, please visit http://www.BetterVssRemoting.com Better VSS Remote Access Tool including SourceOffSite, SourceAnyWhere and VSS Remoting This article makes a detailed...
4
by: Steve | last post by:
I'm trying to implement this tutorial (http://www.csharphelp.com/bio/luke.html) in my code. When I build, I'm getting compiler error: error CS0234: The type or namespace name 'Tcp' does not exist...
2
by: batista | last post by:
Hi there, I want to push data from server to client.(No pulling and no "smart pulling" like using xmlhttp) I heard that one possible solution is to use TCP connection using ActX Controls or...
11
by: kiln | last post by:
I am starting a project that may be suitable for vb.net, using windows forms. I want a rich client, thus win forms vs web forms. Most users will access the app data over a LAN, but some will be...
8
by: Raju Joseph | last post by:
Hi All, I am just trying to get an opinion here. I know this is always a tough choice to make. We are in the process of converting our VB6 based Healthcare Information System (a full-fledged...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.