static void Main() { // allocate and register channel TcpChannel channel = new TcpChannel(); ChannelServices.RegisterChannel(channel, true); // get reference to remote service MyRemoteInterface obj = (MyRemoteInterface)Activator.GetObject( typeof(MyRemoteInterface), "tcp://localhost:8086/MyRemoteObjectName"); try { // first a simple synchronous call Console.WriteLine(obj.MetodoOla()); // change this to true to use the callback (alt.2) bool useCallback = false; if (!useCallback) { // Alternative 1: asynchronous call without callback // Create delegate to remote method RemoteAsyncDelegate RemoteDel = new RemoteAsyncDelegate(obj.MetodoOla); // Call delegate to remote method IAsyncResult RemAr = RemoteDel.BeginInvoke(null, null); // Wait for the end of the call and then explictly call EndInvoke RemAr.AsyncWaitHandle.WaitOne(); Console.WriteLine(RemoteDel.EndInvoke(RemAr)); } else { // Alternative 2: asynchronous call with callback // Create delegate to remote method RemoteAsyncDelegate RemoteDel = new RemoteAsyncDelegate(obj.MetodoOla); // Create delegate to local callback AsyncCallback RemoteCallback = new AsyncCallback(Client.OurRemoteAsyncCallBack); // Call remote method IAsyncResult RemAr = RemoteDel.BeginInvoke(RemoteCallback, null); } } catch (SocketException) { System.Console.WriteLine("Could not locate server"); } catch (MyException e) { Console.WriteLine("I caught an exception: " + e.campo); Console.WriteLine("I caught an exception: " + e.mo.OlaSemExcepcao()); } Console.ReadLine(); }
static void Main() { TcpChannel channel = new TcpChannel(); ChannelServices.RegisterChannel(channel, false); MyRemoteInterface obj = (MyRemoteInterface)Activator.GetObject( typeof(MyRemoteInterface), "tcp://localhost:8086/MyRemoteObjectName"); if (obj == null) { System.Console.WriteLine("Could not locate server"); } else { try { Console.WriteLine(obj.MetodoOla()); } catch (MyException e) { Console.WriteLine("I caught an exception: " + e.campo); Console.WriteLine("I caught an exception: " + e.mo.OlaSemExcepcao()); } } Console.ReadLine(); }