Richard210363
October 24th, 2005, 02:02 PM
I have a problem with my remoting app.
I have a Console app that acts as a server and sets up the remoting channel.
I have a remote object that is called by a client through the channel.
This remote object is a singleton object.
I have a client that uses the channel to call the remote object via the channel.
The remote object runs Windows Media Encoder by instantiating it and then running it.
I run the remote object first from the Console app server using the servers Main method.
I then want any client that uses the channel to use the same singleton remote object.
In this way I can get the client to remotely control an already running app.
However, using the code below, the Console app server starts the remote object that runs the Windows Media Encoder BUT if I then use the remote client the remote object appears to re-start and a new instance of the Windows Media Encoder appears.
My questions are:
Is this the right way to get a remote client to 'talk' to an alrerady running application on a different machine?
Can I do a remote client version of:
SampleObject obj = SampleObject.GetInstance();
so that the Singleton code runs.?
Thanks for any help.
Richard
The code is included below.
Console App Server:
############################################
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
namespace CodeGuru.Remoting
{
/// <remarks>
/// Sample server to demonstrate the use of .NET Remoting.
/// </remarks>
public class SampleServer
{
public static int Main(string [] args)
{
// Create an instance of a channel
TcpChannel channel = new TcpChannel(8080);
ChannelServices.RegisterChannel(channel);
// Register as an available service with the name HelloWorld
RemotingConfiguration.RegisterWellKnownServiceType(
typeof(SampleObject),
"HelloWorld",
WellKnownObjectMode.SingleCall );
//start the encoder
SampleObject obj = SampleObject.GetInstance();
Console.WriteLine(obj.Reference_String);
System.Console.WriteLine("Press the enter key to exit...");
System.Console.ReadLine();
return 0;
}
}
}
##########################################
Remote Object:
############################################
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using WMEncoderLib;
namespace CodeGuru.Remoting
{
/// <remarks>
/// Sample object to demonstrate the use of .NET Remoting.
/// </remarks>
public class SampleObject : MarshalByRefObject
{
/// <summary>
/// Constructor
/// </summary>
///
private static SampleObject instance;
private static int numOfReference;
// private string code;
private SampleObject()
{
numOfReference = 20;
// Create WMEncoderApp and WMEncoder objects.
WMEncoderApp EncoderApp = new WMEncoderApp();
IWMEncoder Encoder = EncoderApp.Encoder;
// Display the predefined Encoder UI.
EncoderApp.Visible = true;
}
public static SampleObject GetInstance()
{
if(instance == null)
{
instance = new SampleObject();
}
numOfReference++;
return instance;
}
public static int Reference
{
get { return numOfReference; }
}
public string Reference_String
{
get { return numOfReference.ToString(); }
}
}
}
############################################
Remote Client
############################################
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
namespace CodeGuru.Remoting
{
/// <remarks>
/// Sample client to demonstrate the use of .NET Remoting.
/// </remarks>
public class SampleClient
{
public static int Main(string [] args)
{
// Create a channel for communicating w/ the remote object
// Notice no port is specified on the client
TcpChannel chan = new TcpChannel();
ChannelServices.RegisterChannel(chan);
// Create an instance of the remote object
SampleObject obj = (SampleObject) Activator.GetObject(
typeof(CodeGuru.Remoting.SampleObject),
"tcp://localhost:8080/HelloWorld" );
// Use the object
if( obj.Equals(null) )
{
System.Console.WriteLine("Error: unable to locate server");
}
else
{
Console.WriteLine(obj.Reference_String);
System.Console.WriteLine("Press the enter key to exit...");
System.Console.ReadLine();
}
return 0;
}
}
}
I have a Console app that acts as a server and sets up the remoting channel.
I have a remote object that is called by a client through the channel.
This remote object is a singleton object.
I have a client that uses the channel to call the remote object via the channel.
The remote object runs Windows Media Encoder by instantiating it and then running it.
I run the remote object first from the Console app server using the servers Main method.
I then want any client that uses the channel to use the same singleton remote object.
In this way I can get the client to remotely control an already running app.
However, using the code below, the Console app server starts the remote object that runs the Windows Media Encoder BUT if I then use the remote client the remote object appears to re-start and a new instance of the Windows Media Encoder appears.
My questions are:
Is this the right way to get a remote client to 'talk' to an alrerady running application on a different machine?
Can I do a remote client version of:
SampleObject obj = SampleObject.GetInstance();
so that the Singleton code runs.?
Thanks for any help.
Richard
The code is included below.
Console App Server:
############################################
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
namespace CodeGuru.Remoting
{
/// <remarks>
/// Sample server to demonstrate the use of .NET Remoting.
/// </remarks>
public class SampleServer
{
public static int Main(string [] args)
{
// Create an instance of a channel
TcpChannel channel = new TcpChannel(8080);
ChannelServices.RegisterChannel(channel);
// Register as an available service with the name HelloWorld
RemotingConfiguration.RegisterWellKnownServiceType(
typeof(SampleObject),
"HelloWorld",
WellKnownObjectMode.SingleCall );
//start the encoder
SampleObject obj = SampleObject.GetInstance();
Console.WriteLine(obj.Reference_String);
System.Console.WriteLine("Press the enter key to exit...");
System.Console.ReadLine();
return 0;
}
}
}
##########################################
Remote Object:
############################################
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using WMEncoderLib;
namespace CodeGuru.Remoting
{
/// <remarks>
/// Sample object to demonstrate the use of .NET Remoting.
/// </remarks>
public class SampleObject : MarshalByRefObject
{
/// <summary>
/// Constructor
/// </summary>
///
private static SampleObject instance;
private static int numOfReference;
// private string code;
private SampleObject()
{
numOfReference = 20;
// Create WMEncoderApp and WMEncoder objects.
WMEncoderApp EncoderApp = new WMEncoderApp();
IWMEncoder Encoder = EncoderApp.Encoder;
// Display the predefined Encoder UI.
EncoderApp.Visible = true;
}
public static SampleObject GetInstance()
{
if(instance == null)
{
instance = new SampleObject();
}
numOfReference++;
return instance;
}
public static int Reference
{
get { return numOfReference; }
}
public string Reference_String
{
get { return numOfReference.ToString(); }
}
}
}
############################################
Remote Client
############################################
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
namespace CodeGuru.Remoting
{
/// <remarks>
/// Sample client to demonstrate the use of .NET Remoting.
/// </remarks>
public class SampleClient
{
public static int Main(string [] args)
{
// Create a channel for communicating w/ the remote object
// Notice no port is specified on the client
TcpChannel chan = new TcpChannel();
ChannelServices.RegisterChannel(chan);
// Create an instance of the remote object
SampleObject obj = (SampleObject) Activator.GetObject(
typeof(CodeGuru.Remoting.SampleObject),
"tcp://localhost:8080/HelloWorld" );
// Use the object
if( obj.Equals(null) )
{
System.Console.WriteLine("Error: unable to locate server");
}
else
{
Console.WriteLine(obj.Reference_String);
System.Console.WriteLine("Press the enter key to exit...");
System.Console.ReadLine();
}
return 0;
}
}
}