I am not sure if there are still people using .NET Remoting. In the current projects I am doing, it has been replaced by WCF for WCF on .NET Framework 3.5 SP1 has much better performance on computers with many core CPUs.
However, I still put some old bits here demonstrating the use of some helper classes I wrote for .NET remoting. They have been used in several mission critical applications which have real-time data transmissions. If you still want to use .NET Remoting somewhere, they will greatly simplify the job by hiding many complicated details for maintaining the remoting channels in their full life cycles.
Basically there can splitted into 3 parts,
1. Remoting server for hosting a singleton object
RemotingServer server = new RemotingServer(); // instantiates a remoting server
server.StartServer(30005); // starts listening on TCP port 30005
new Remoting.RemotingServerObject<YourCutomClass1>();
new RemotingServerObject<YourCutomClass2>();
//...
// do something here to block the procedure to keep the server running in memory
//...
server.StopServer();
2. RemotingClient for communications to the server
RemotingClient remotingClient = new RemotingClient(); // creates instance of the client
remotingClient.CreateConnection();
YourCutomClass1 yourCustomObject = (new RemotingClientObject<YourCutomClass1>()).GetRemoteObject("10.0.0.31", 30005);
// pulls data from server side on IP address 10.0.0.31 and TCP port 30005
// ...
remotingClient.CloseConnection(); // closes client side connection
3. Client side helper class for remoting object
RemotingClientObject<T>
It includes an important method called
public T GetRemoteObject(string server, int portNumber)
Please check the source code to see how actually it works
The following is the full source code,
using System;
using System.Collections;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Serialization.Formatters;
namespace SingletonRemotingHelpers
{
public class RemotingServer
{
private TcpChannel _tcpChannel;
/// <summary>
/// start listening
/// </summary>
/// <param name="portNumber">TCP port number</param>
public void StartServer(int portNumber)
{
BinaryServerFormatterSinkProvider provider = new BinaryServerFormatterSinkProvider();
provider.TypeFilterLevel = TypeFilterLevel.Full;
IDictionary props = new Hashtable();
props["port"] = portNumber;
_tcpChannel = new TcpChannel(props, null, provider);
ChannelServices.RegisterChannel(_tcpChannel, false);
}
/// <summary>
/// stop listening
/// </summary>
public void StopServer()
{
_tcpChannel.StopListening(null);
ChannelServices.UnregisterChannel(_tcpChannel);
}
}
public class RemotingServerObject<T>
{
public RemotingServerObject()
{
Register();
}
/// <summary>
/// register type T on server side
/// </summary>
public void Register()
{
RemotingConfiguration.RegisterWellKnownServiceType(typeof(T), typeof(T).ToString(), WellKnownObjectMode.Singleton);
}
}
public class RemotingClient
{
private TcpChannel _tcpChannel = new TcpChannel();
public void CreateConnection()
{
BinaryClientFormatterSinkProvider clientProvider = new BinaryClientFormatterSinkProvider();
BinaryServerFormatterSinkProvider serverProvider = new BinaryServerFormatterSinkProvider();
serverProvider.TypeFilterLevel = TypeFilterLevel.Full;
int subscriberPort = 0;
IDictionary props = new Hashtable();
props["name"] = "tcp" + subscriberPort.ToString();
props["port"] = subscriberPort;
props["typeFilterLevel"] = TypeFilterLevel.Full;
tcpChannel = new TcpChannel(props, clientProvider, serverProvider);
ChannelServices.RegisterChannel(_tcpChannel, false);
}
public void CloseConnection()
{
ChannelServices.UnregisterChannel(_tcpChannel);
}
}
/// <summary>
/// typed remoting object
/// </summary>
/// <typeparam name="T">type of the object</typeparam>
public class RemotingClientObject<T>
{
/// <summary>
/// get remote object
/// </summary>
/// <param name="server">the server's IP address or domain name (if DNS is available)</param>
/// <param name="portNumber">the server's listening TCP port number</param>
/// <returns></returns>
public T GetRemoteObject(string server, int portNumber)
{
return (T)Activator.GetObject(typeof(T), "tcp://" + server + ":" + portNumber.ToString() + "/" + typeof(T).ToString());
}
}
}
Hope this helps.