Click here to Skip to main content
16,005,437 members
Home / Discussions / C#
   

C#

 
GeneralDynamicInvoke a remote object Pin
Skynyrd7-Dec-04 12:21
Skynyrd7-Dec-04 12:21 
GeneralRe: DynamicInvoke a remote object Pin
Heath Stewart7-Dec-04 14:48
protectorHeath Stewart7-Dec-04 14:48 
GeneralRe: DynamicInvoke a remote object Pin
Skynyrd8-Dec-04 0:57
Skynyrd8-Dec-04 0:57 
GeneralRe: DynamicInvoke a remote object Pin
Javier Lozano7-Dec-04 15:02
Javier Lozano7-Dec-04 15:02 
GeneralRe: DynamicInvoke a remote object Pin
Skynyrd8-Dec-04 1:08
Skynyrd8-Dec-04 1:08 
GeneralRe: DynamicInvoke a remote object Pin
Javier Lozano8-Dec-04 5:07
Javier Lozano8-Dec-04 5:07 
GeneralRe: DynamicInvoke a remote object Pin
Heath Stewart8-Dec-04 5:58
protectorHeath Stewart8-Dec-04 5:58 
GeneralRe: DynamicInvoke a remote object Pin
Javier Lozano8-Dec-04 6:03
Javier Lozano8-Dec-04 6:03 
Hi Skynyrd,

I think I solved your problem or at least came up with an example for you to see. I created a simple remote object with a client and server to host it. Then, I created a delegate so I could call the remote object's method via that delegate. This is what I came up with:

WARNING THIS IS A LONG POST

RemoteObject.cs

using System;

namespace RemoteNamespace
{
	public class RemoteObject : MarshalByRefObject
	{
		public string MyTypeString()
		{
			return this.GetType().ToString();
		}
	}
}


Server.cs

using System;
using System.Runtime.Remoting;


namespace RemoteServer
{

	class Server
	{
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main(string[] args)
		{
			RemotingConfiguration.Configure("RemoteServer.exe.config");
			Console.WriteLine("Hit <ENTER> to exit.");

			Console.ReadLine();
		}
	}
}


Client.cs
using System;
using System.Runtime.Remoting;
using System.Reflection;
using RemoteNamespace;

namespace RemoteTest
{
	public delegate string del_void();

	class Client
	{
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main(string[] args)
		{
			RemotingConfiguration.Configure("RemoteTest.exe.config");
			RemoteObject ro = new RemoteObject();

			// To get the list of all methods
			// MethodInfo[] methods = ro.GetType().GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.InvokeMethod);

			MethodInfo m = ro.GetType().GetMethod("MyTypeString",BindingFlags.Instance | BindingFlags.Public | BindingFlags.InvokeMethod);

			try
			{
				Delegate d = Delegate.CreateDelegate(typeof(del_void),ro,m.Name);
				object o = d.DynamicInvoke(null);
				Console.WriteLine(o);
			}
			catch(TargetInvocationException tie)
			{
				Console.WriteLine(tie.StackTrace);
			}
			catch(Exception e)
			{
				Console.WriteLine(e.StackTrace);
			}
						
			Console.ReadLine();
		}
	}
}


Server.exe.config -- For remoting object config
<configuration>
   <system.runtime.remoting>
      <application>
         <service>
            <wellknown 
               mode="SingleCall" objectUri="RemoteObject.rem"
               type="RemoteNamespace.RemoteObject, RemoteLibrary"/>
         </service>
         <channels>
            <channel ref="tcp" port="8080"/>
         </channels>
      </application>
   </system.runtime.remoting>
</configuration>


Client.exe.config -- For remote object config
<configuration>
   <system.runtime.remoting>
      <application>
         <channels>
            <channel ref="tcp" port="0">
               <clientProviders>
                  <formatter 
                     ref="binary"
                  />
               </clientProviders>
            </channel>
         </channels>
         <client>
            <wellknown 
               url="tcp://localhost:8080/RemoteObject.rem"
               type="RemoteNamespace.RemoteObject, RemoteLibrary"
            />
         </client>
      </application>
   </system.runtime.remoting>
</configuration>


The key for this to work was to use the following line:

MethodInfo m = ro.GetType().GetMethod("MyTypeString",BindingFlags.Instance | BindingFlags.Public | BindingFlags.InvokeMethod);


For some reason when you passed the full name of the method, the object couldn't bind. However, I just tried this, if you pass the name of the method MyTypeString instead of m.Name into the CreateDelegate method, it also works.

Take a look at this code and tell me what you think.

~javier lozano
(blog)
GeneralRe: DynamicInvoke a remote object Pin
Skynyrd8-Dec-04 11:26
Skynyrd8-Dec-04 11:26 
GeneralRe: DynamicInvoke a remote object Pin
Skynyrd8-Dec-04 11:43
Skynyrd8-Dec-04 11:43 
GeneralRe: DynamicInvoke a remote object Pin
Javier Lozano8-Dec-04 12:06
Javier Lozano8-Dec-04 12:06 
GeneralRe: DynamicInvoke a remote object Pin
Skynyrd8-Dec-04 13:18
Skynyrd8-Dec-04 13:18 
GeneralHTML style RichTextBox control Pin
Beringer7-Dec-04 12:06
Beringer7-Dec-04 12:06 
GeneralRe: HTML style RichTextBox control Pin
Beringer7-Dec-04 12:08
Beringer7-Dec-04 12:08 
GeneralRe: HTML style RichTextBox control Pin
Heath Stewart7-Dec-04 14:44
protectorHeath Stewart7-Dec-04 14:44 
GeneralSockets and Win2k Issues Pin
Tristan Rhodes7-Dec-04 11:34
Tristan Rhodes7-Dec-04 11:34 
GeneralRe: Sockets and Win2k Issues Pin
Heath Stewart7-Dec-04 14:27
protectorHeath Stewart7-Dec-04 14:27 
GeneralRe: Sockets and Win2k Issues Pin
Tristan Rhodes8-Dec-04 0:25
Tristan Rhodes8-Dec-04 0:25 
GeneralRe: Sockets and Win2k Issues Pin
Daniel Turini7-Dec-04 20:12
Daniel Turini7-Dec-04 20:12 
GeneralRe: Sockets and Win2k Issues Pin
Tristan Rhodes8-Dec-04 0:26
Tristan Rhodes8-Dec-04 0:26 
GeneralRe: Sockets and Win2k Issues Pin
Heath Stewart8-Dec-04 6:01
protectorHeath Stewart8-Dec-04 6:01 
GeneralFree Lightweight C# SDE Pin
Tristan Rhodes7-Dec-04 8:30
Tristan Rhodes7-Dec-04 8:30 
GeneralRe: Free Lightweight C# SDE Pin
Stanciu Vlad7-Dec-04 9:48
Stanciu Vlad7-Dec-04 9:48 
GeneralRe: Free Lightweight C# SDE Pin
Heath Stewart7-Dec-04 14:21
protectorHeath Stewart7-Dec-04 14:21 
GeneralRe: Free Lightweight C# SDE Pin
leppie8-Dec-04 2:42
leppie8-Dec-04 2:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.