Skip to content

Instantly share code, notes, and snippets.

@mipdevbe
Created October 15, 2021 18:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mipdevbe/aa49a1f3045e3cca31a70f67d1b22191 to your computer and use it in GitHub Desktop.
Save mipdevbe/aa49a1f3045e3cca31a70f67d1b22191 to your computer and use it in GitHub Desktop.
.NET Remoting example in C#
/* NET Remoting Client */
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
using System.Runtime.Remoting.Channels.Ipc;
using System.Runtime.Remoting.Channels.Tcp;
using System.Security.Permissions;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace mpRemotingClient
{
[SecurityPermission(SecurityAction.Demand)]
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
[Obsolete]
private void cmdSum_Click(object sender, EventArgs e)
{
try
{
var obj = (mpRemotingClass.RemoteOperations)Activator.GetObject(typeof(mpRemotingClass.RemoteOperations),
"tcp://localhost:8085/RemoteOperations");
int x = Int32.Parse(txtValueOne.Text);
int y = Int32.Parse(txtValueTwo.Text);
txtResult.Text = (obj.sum(x, y)).ToString();
}
catch(Exception ex)
{
File.WriteAllText("mpRemotingClient.txt", ex.Message);
MessageBox.Show(ex.Message);
}
}
}
}
/* NET Remoting Server */
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
using System.Runtime.Remoting.Channels.Tcp;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace mpRemotingServer
{
class Program
{
[STAThread]
static void Main(string[] args)
{
try
{
TcpServerChannel serverChannel = new TcpServerChannel(8085);
ChannelServices.RegisterChannel(serverChannel, false);
RemotingConfiguration.RegisterWellKnownServiceType(typeof
(mpRemotingClass.RemoteOperations), "RemoteOperations", WellKnownObjectMode.Singleton);
Console.WriteLine("Listening on {0}",
serverChannel.GetChannelUri());
Console.WriteLine("Press the enter key to exit...");
Console.ReadLine();
}
catch(Exception ex)
{
File.WriteAllText("mpRemotingServer.txt", ex.Message);
MessageBox.Show(ex.Message);
}
}
}
}
/* NET Remoting Object Definition */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace mpRemotingClass
{
public class RemoteOperations : MarshalByRefObject
{
public int sum(int a, int b)
{
return a + b;
}
}
}
@mipdevbe
Copy link
Author

On the server code, I have changed:

RemotingConfiguration.RegisterWellKnownServiceType(typeof (mpRemotingClass.RemoteOperations), "RemoteOperations", WellKnownObjectMode.Singleton);

// Changed to...
var remoteOperations = new mpRemotingClass.RemoteOperations(1);
RemotingServices.Marshal(remoteOperations, "RemoteOperations");

and the object definition is now:
public interface ICountingService
{
int Increment();
}

[Serializable]
public class RemoteOperations : MarshalByRefObject, ICountingService
{
    private static int _value = 0;

    public RemoteOperations(int startValue)
    {
        _value = startValue;
    }

    public int Increment()
    {
        // not threadsafe!
        _value++;
        return _value;
    }

    [System.Security.SecurityCritical]  // auto-generated
    public int sum(int a, int b)
    {
        return a + b;
    }
}

This still works fine on my test computer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment