Created
October 15, 2021 18:01
-
-
Save mipdevbe/aa49a1f3045e3cca31a70f67d1b22191 to your computer and use it in GitHub Desktop.
.NET Remoting example in C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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();
}
This still works fine on my test computer.