DynamicInvoke a remote object
-
I cant dynamically invoke a method on a remote object. Code is as follows (tested on a dummy class with same method and works perfectly but wont run on the remote object) public void RunMethod(Type methodType,string methodName,object[] arguments) { object proxy=GetProxy (returns a transparent proxy of a singlecall remote object) Delegate dlg=Delegate.CreateDelegate(methodType,proxy,methodName); throws a System.ArgumentException dlg.DynamicInvoke(arguments); } Is it possible to invoke a remote object through it's proxy this way? If not, how can it be done?
-
I cant dynamically invoke a method on a remote object. Code is as follows (tested on a dummy class with same method and works perfectly but wont run on the remote object) public void RunMethod(Type methodType,string methodName,object[] arguments) { object proxy=GetProxy (returns a transparent proxy of a singlecall remote object) Delegate dlg=Delegate.CreateDelegate(methodType,proxy,methodName); throws a System.ArgumentException dlg.DynamicInvoke(arguments); } Is it possible to invoke a remote object through it's proxy this way? If not, how can it be done?
Please be specific and verbose. What is the message for the
ArgumentException
? What is the stack trace? Please be sure to also describe the method signature that you're trying to invoke as well. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles] [My Blog] -
I cant dynamically invoke a method on a remote object. Code is as follows (tested on a dummy class with same method and works perfectly but wont run on the remote object) public void RunMethod(Type methodType,string methodName,object[] arguments) { object proxy=GetProxy (returns a transparent proxy of a singlecall remote object) Delegate dlg=Delegate.CreateDelegate(methodType,proxy,methodName); throws a System.ArgumentException dlg.DynamicInvoke(arguments); } Is it possible to invoke a remote object through it's proxy this way? If not, how can it be done?
The MSDN documentation says the following about the function Delegate.CreateDelegate(Type objType,Object target,string method) method. A System.ArgumentException is thrown when objType does not inherit from either Delegate or MulticastDelegate OR method is not an instance method. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdelegateclasscreatedelegatetopic.asp[^] I'm a little confused about your question: Is it possible to invoke a remote object through it's proxy this way? When you have a remote objet you use its proxy to call its method exactly the same way you would call other objects. For example, say I have an object called myRemoteObject
using System; using System.Runtime.Remoting; namespace myRemoteService { // Well Known Web Service object public class myRemoteObject : MarshalByRefObject { // Method myRemoteMethod public String myRemoteMethod() { return "Hello World"; } } }
You can then call the remote object via it's proxy using the following code:using System; using System.Runtime.Remoting; using myRemoteService; public class Client { static void Main(string[] args) { //Using a configuration file to have less code... String filename = "client.exe.config"; RemotingConfiguration.Configure(filename); myRemoteObject obj = new myRemoteObject(); Console.WriteLine(obj.myRemoteMethod()); obj = null; Console.WriteLine("Done..."); }
-
Please be specific and verbose. What is the message for the
ArgumentException
? What is the stack trace? Please be sure to also describe the method signature that you're trying to invoke as well. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles] [My Blog]Hi Heath, Sorry, i'm running VS in spanish so I'm not sure how to exactly translate any exception messages without confusing things any more :p The calling code is as follows: public delegate string TestMethod(); ... SingleCallManager manager=new SingleCallManager(typeof(IDataLayerTool),"tcp://localhost:8086/DataLayerTool.rem"); manager.RunMethod(typeof(TestMethod),"GetAssetsMenuLayout",null); ... Complete info on exception is the following: The message would translate to "Error linking to target method." more or less. [System.ArgumentException]: {"Error al enlazar con el método de destino." } System.Object: {System.ArgumentException} _className: null _COMPlusExceptionCode: -532459699 _exceptionMethod: _exceptionMethodString: null _helpURL: null _HResult: -2147024809 _innerException: { } _message: "Error al enlazar con el método de destino." _remoteStackIndex: 0 _remoteStackTraceString: null _source: null _stackTrace: {System.Array} _stackTraceString: " at System.Delegate.InternalCreate(Object target, String method, Boolean ignoreCase)\r\n at System.Delegate.CreateDelegate(Type type, Object target, String method)\r\n at Vigila.Objects.SingleCallManager.RunMethod(Type methodType, String methodName, Object[] arguments) in d:\\proyectos .net\\código aplicaciones\\vigila 2\\vigila objects\\singlecallmanager.cs:line 128\r\n at Vigila.Client.VigilaStartUp.loadUpIniData() in d:\\proyectos .net\\código aplicaciones\\vigila 2\\vigila client\\vigilastartup.cs:line 63\r\n at Vigila.Objects.ThreadWorker.workingEnvelope() in D:\\Proyectos .NET\\Código Aplicaciones\\Vigila 2\\Vigila objects\\ThreadWorker.cs:line 153" _xcode: -532459699 _xptrs: 0 HelpLink: null HResult: -2147024809 InnerException: { } Message: "Error al enlazar con el método de destino." Source: "mscorlib" StackTrace: " at System.Delegate.InternalCreate(Object target, String method, Boolean ignoreCase)\r\n at System.Delegate.CreateDelegate(Type type, Object target, String method)\r\n at Vigila.Objects.SingleCallManager.RunMethod(Type methodType, String methodName, Object[] arguments) in d:\\proyectos .net\\código aplicaciones\\vigila 2\\vigila objects\\singlecallmanager.cs:line 128\r\n at Vigila.Client.VigilaStartUp.loadUpIniData() in d:\\proyectos .net\\código aplicaciones\\vigila 2\\vigila client\\vigilastartup.cs:line 63\r\n at Vigila.Objects.ThreadWorker.workingEnvelope() in D:\\Proyectos .NET\\Código Aplicaciones\\Vigila 2\\Vigila objects\\ThreadWorker.cs:line 153" TargetSite: {System.Reflection.RuntimeMethodInfo}
-
The MSDN documentation says the following about the function Delegate.CreateDelegate(Type objType,Object target,string method) method. A System.ArgumentException is thrown when objType does not inherit from either Delegate or MulticastDelegate OR method is not an instance method. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdelegateclasscreatedelegatetopic.asp[^] I'm a little confused about your question: Is it possible to invoke a remote object through it's proxy this way? When you have a remote objet you use its proxy to call its method exactly the same way you would call other objects. For example, say I have an object called myRemoteObject
using System; using System.Runtime.Remoting; namespace myRemoteService { // Well Known Web Service object public class myRemoteObject : MarshalByRefObject { // Method myRemoteMethod public String myRemoteMethod() { return "Hello World"; } } }
You can then call the remote object via it's proxy using the following code:using System; using System.Runtime.Remoting; using myRemoteService; public class Client { static void Main(string[] args) { //Using a configuration file to have less code... String filename = "client.exe.config"; RemotingConfiguration.Configure(filename); myRemoteObject obj = new myRemoteObject(); Console.WriteLine(obj.myRemoteMethod()); obj = null; Console.WriteLine("Done..."); }
Hi javier, thanks for the info. I can call any method I want of my remote object through its transparent proxy without any problems. The problem is that I want to localize in one piece of code all the exception handling of the SocketException that gets thrown when the server where the remote object lives is not accessible to the client app that calls one if its methods. There r two ways around it that I could find right now: 1) Implement the exception handling on all remote calls throughout my code. 2) Create a SingleCallManager object with a generic RunMethod method that will dinamycally link any method calls to the remoteobject (loss of performance is not an issue) dynamically invoking the call to its proxy like you would with any other object. The problem is, that the same piece of code will link perfectly to a dummy non remote class but it will not link to the remote object throwing the exception i've included in the reply to Heath's post. Through this RunMethod method I would be able to localize all excpetion handling of any method call due to a connection failure to the server in one place.
-
Hi javier, thanks for the info. I can call any method I want of my remote object through its transparent proxy without any problems. The problem is that I want to localize in one piece of code all the exception handling of the SocketException that gets thrown when the server where the remote object lives is not accessible to the client app that calls one if its methods. There r two ways around it that I could find right now: 1) Implement the exception handling on all remote calls throughout my code. 2) Create a SingleCallManager object with a generic RunMethod method that will dinamycally link any method calls to the remoteobject (loss of performance is not an issue) dynamically invoking the call to its proxy like you would with any other object. The problem is, that the same piece of code will link perfectly to a dummy non remote class but it will not link to the remote object throwing the exception i've included in the reply to Heath's post. Through this RunMethod method I would be able to localize all excpetion handling of any method call due to a connection failure to the server in one place.
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 { /// /// The main entry point for the application. /// \[STAThread\] static void Main(string\[\] args) { RemotingConfiguration.Configure("RemoteServer.exe.config"); Console.WriteLine("Hit 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 { /// /// The main entry point for the application. /// \[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> -
Hi javier, thanks for the info. I can call any method I want of my remote object through its transparent proxy without any problems. The problem is that I want to localize in one piece of code all the exception handling of the SocketException that gets thrown when the server where the remote object lives is not accessible to the client app that calls one if its methods. There r two ways around it that I could find right now: 1) Implement the exception handling on all remote calls throughout my code. 2) Create a SingleCallManager object with a generic RunMethod method that will dinamycally link any method calls to the remoteobject (loss of performance is not an issue) dynamically invoking the call to its proxy like you would with any other object. The problem is, that the same piece of code will link perfectly to a dummy non remote class but it will not link to the remote object throwing the exception i've included in the reply to Heath's post. Through this RunMethod method I would be able to localize all excpetion handling of any method call due to a connection failure to the server in one place.
Skynyrd wrote: The problem is that I want to localize in one piece of code all the exception handling of the SocketException that gets thrown when the server where the remote object lives is not accessible to the client app that calls one if its methods. Could you elaborate a little more? No offense, but the grammar is getting in the way of the problem, specifically "thrown when the server where the remote object lives". If I'm correct, you're trying to say that you want to localize the exception message that the server throws, and you want to localize it on the client? If so, just catch that exception type and display a localized error message to the user, or if this is for a library (never lets your users see exceptions - there's simply no excuse for that in an application, but in a library it's common since your library gets used by applications) localize the text and throw a new exception with the original exception as the
InnerException
. Sorry if I'm wrong, but like I said I have problems parsing and understanding your sentance. Again, no offense meant. It's usually not a problem but in this case - in order to hopefully help you with the true problem - I need to be sure I understand you correctly. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles] [My Blog] -
I cant dynamically invoke a method on a remote object. Code is as follows (tested on a dummy class with same method and works perfectly but wont run on the remote object) public void RunMethod(Type methodType,string methodName,object[] arguments) { object proxy=GetProxy (returns a transparent proxy of a singlecall remote object) Delegate dlg=Delegate.CreateDelegate(methodType,proxy,methodName); throws a System.ArgumentException dlg.DynamicInvoke(arguments); } Is it possible to invoke a remote object through it's proxy this way? If not, how can it be done?
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 { /// /// The main entry point for the application. /// \[STAThread\] static void Main(string\[\] args) { RemotingConfiguration.Configure("RemoteServer.exe.config"); Console.WriteLine("Hit 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 { /// /// The main entry point for the application. /// \[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> -
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 { /// /// The main entry point for the application. /// \[STAThread\] static void Main(string\[\] args) { RemotingConfiguration.Configure("RemoteServer.exe.config"); Console.WriteLine("Hit 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 { /// /// The main entry point for the application. /// \[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>Javier, thanks a lot for your help. I'm gonna give it a try right now and will let you know how it turns out. Funny that u can succesfully link if using the mehtodinfo but not through the method's name. Trying it right away. Heath, I'm sorry if I'm not explaining myself too well, english isnt my native language :p. What I'm trying to do is localize all the exception handling of a failed connection to the remote server in one place inside my client app (the singlecallmanager class), and not in all the calls to remote object's different methods throughout my client app code. Still Javier seems to have solved the problem. Thanks for your time.
-
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 { /// /// The main entry point for the application. /// \[STAThread\] static void Main(string\[\] args) { RemotingConfiguration.Configure("RemoteServer.exe.config"); Console.WriteLine("Hit 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 { /// /// The main entry point for the application. /// \[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>Sorry to say but ur code did not work Javier :( Still, I've figured out what the problem is. Maybe it was obvious and I've been wasting ur time: I can't create the proxy using the public interface of my remote object: object proxy=RemotingServices.Connect(typeof(IDataToolLayer),myUrl.AbsoluteUri); DynamicInvoke fails. However, if I create the proxy using the remote object class, everything works ok: object proxy=RemotingServices.Connect(typeof(DataToolLayer),myUrl.AbsoluteUri); The question is, is there a way around this? I dont want to have to include the remote library in my client app, it shouldn't be necessary. Another interesting thing, is that if I try to get the MethodInfo as Javier does in his example, I get a null return if I'm using the IDataLayerTool interface.
-
Sorry to say but ur code did not work Javier :( Still, I've figured out what the problem is. Maybe it was obvious and I've been wasting ur time: I can't create the proxy using the public interface of my remote object: object proxy=RemotingServices.Connect(typeof(IDataToolLayer),myUrl.AbsoluteUri); DynamicInvoke fails. However, if I create the proxy using the remote object class, everything works ok: object proxy=RemotingServices.Connect(typeof(DataToolLayer),myUrl.AbsoluteUri); The question is, is there a way around this? I dont want to have to include the remote library in my client app, it shouldn't be necessary. Another interesting thing, is that if I try to get the MethodInfo as Javier does in his example, I get a null return if I'm using the IDataLayerTool interface.
That stinks! Have you seen this article,http://www.thinktecture.com/Resources/RemotingFAQ/USEINTERFACESWITHCONFIGFILES.html[^], from Ingo Rammer? I've used the code before on a project and it worked great, however I did not call any methods dynamically. I can change my code and see if it will work. ~javier lozano (blog)
-
That stinks! Have you seen this article,http://www.thinktecture.com/Resources/RemotingFAQ/USEINTERFACESWITHCONFIGFILES.html[^], from Ingo Rammer? I've used the code before on a project and it worked great, however I did not call any methods dynamically. I can change my code and see if it will work. ~javier lozano (blog)
Ok Javier, got it! :D I've discoverd one hell of a class in the Reflexion namespace that has solved all my problems: TypeDelegator public object RunMethod(string methodName, object[] arguments) { object proxy=GetProxy(); TypeDelegator delegator=new TypeDelegator(typeof(IDataLayerTool)); return delegator.GetMethod(methodName).Invoke(proxy,arguments); } It seems that the TypeDelegator can map correctly through interfaces and even makes the job a lot easier as u dont have to create delegates as u would with a DynamicInvoke. Nice little class, yessir :D Again, thanks for your time Javier.