[Mono-devel-list] Remoting problem with cross-domain objects

Lluis Sanchez lluis at ideary.com
Sun Apr 27 16:13:39 EDT 2003


Hi,

Fixed in CVS. However, notice that the client will fail when getting the "Second" object since it was created in an app domain that has not registered the tcp channel.

- Lluis.
  ----- Original Message ----- 
  From: Urs Muff 
  To: 'mono-devel-list at lists.ximian.com' 
  Sent: Friday, April 25, 2003 10:40 PM
  Subject: [Mono-devel-list] Remoting problem with cross-domain objects


  <server.cs>

  using System;

  using System.Collections;

  using System.Runtime.Remoting;

  using System.Runtime.Remoting.Channels.Tcp;



  public class ServerObjImpl : MarshalByRefObject, ServerObj

  {

     string mName;

     public ServerObjImpl(string name) { mName = name; }



     #region ServerObj Members



     public string Name { get { return mName; } }



     public string DoIt(string arg)

     {

         string ret = string.Format("{0} @ {1}: {2}",Name,AppDomain.CurrentDomain.FriendlyName,arg);

         Console.WriteLine(ret);

         return ret;

     }



     #endregion

  }



  class ServerBrokerImpl : MarshalByRefObject, ServerBroker

  {

     IDictionary mMap = new Hashtable();



     #region ServerBroker Members



     public void RegisterServer(string which, ServerObj obj)

     {

         Console.WriteLine(string.Format("[{0}]: Register ({1},{2})",AppDomain.CurrentDomain.FriendlyName,which,obj.Name));

         mMap[which] = obj;

     }



     public ServerObj GetServerObj(string which)

     {

         return mMap[which] as ServerObj;

     }



     #endregion

  }



  class Server

  {

     [STAThread]

     static void Main(string[] args)

     {

         RemotingHelper.RegisterBinaryChannel(8123);

        RemotingConfiguration.RegisterWellKnownServiceType(typeof(ServerBrokerImpl),"Broker",WellKnownObjectMode.Singleton);



         ServerBroker broker = Activator.GetObject(typeof(ServerBroker),"tcp://localhost:8123/Broker") as ServerBroker;



         broker.RegisterServer("First",new ServerObjImpl("Primary Server Object"));

         ServerObj obj = AppDomain.CreateDomain("Second Server Domain").CreateInstanceAndUnwrap(

            typeof(ServerObjImpl).Assembly.FullName,typeof(ServerObjImpl).FullName,false,

            System.Reflection.BindingFlags.Default,null,new object[]{"Separated Server Object"},null,null,null

            ) as ServerObj;

         broker.RegisterServer("Second",obj);



         Console.WriteLine("Server up and running [hit enter to exit]");

         Console.ReadLine();

     }

  }

  </server.cs>



  <common.cs>

  using System;



  public interface ServerObj

  {

     string Name { get; }

     string DoIt(string arg);

  }



  public interface ServerBroker

  {

     ServerObj GetServerObj(string which);

     void RegisterServer(string which,ServerObj obj);

  }



  public class RemotingHelper

  {

     public static System.Runtime.Remoting.Channels.IChannel RegisterBinaryChannel(int port)

     {

         return RegisterChannel(port,new System.Runtime.Remoting.Channels.BinaryServerFormatterSinkProvider(),true);

     }

     public static System.Runtime.Remoting.Channels.IChannel RegisterSoapChannel(int port)

     {

         return RegisterChannel(port,new System.Runtime.Remoting.Channels.SoapServerFormatterSinkProvider(),false);

     }

     public static System.Runtime.Remoting.Channels.IChannel RegisterChannel(int port,System.Runtime.Remoting.Channels.IServerChannelSinkProvider provider,bool tcp)

     {

         System.Collections.IDictionary props = new System.Collections.Hashtable();

         System.Runtime.Remoting.Channels.IChannel channel = null;

            

         System.Reflection.MemberInfo[] info = provider.GetType().GetMember("TypeFilterLevel");

         if (info.Length > 0) 

         {

            ((info[0]) as System.Reflection.PropertyInfo).SetValue(provider,

               Type.GetType("System.Runtime.Serialization.Formatters.TypeFilterLevel").GetField("Full").GetValue(null),null);

         }

         props["port"] = port;



         if (tcp) channel = new System.Runtime.Remoting.Channels.Tcp.TcpChannel(props, null, provider);

         //else channel = new System.Runtime.Remoting.Channels.Http.HttpChannel(props, null, provider);

         System.Runtime.Remoting.Channels.ChannelServices.RegisterChannel(channel);



         return channel;

     }

  }

  </common.cs>



  <client.cs>

  using System;



  class Client

  {

     [STAThread]

     static void Main(string[] args)

     {

         RemotingHelper.RegisterBinaryChannel(0);

         Console.ReadLine();

         ServerBroker broker = Activator.GetObject(typeof(ServerBroker),"tcp://localhost:8123/Broker") as ServerBroker;



         ServerObj obj1 = broker.GetServerObj("First");

         ServerObj obj2 = broker.GetServerObj("Second");

         

         try

         {

            if (obj1 != null) Console.WriteLine(obj1.DoIt("First call"));

            if (obj2 != null) Console.WriteLine(obj2.DoIt("Second call"));

         }

         catch (Exception ex)

         {

            Console.WriteLine(ex);

         }



         Console.WriteLine("Client done [hit enter to exit]");

         Console.ReadLine();

     }

  }

  </client.cs>



  mcs -g -r System.Runtime.Remoting Common.cs Server.cs -o Server.exe

  mcs -g -r System.Runtime.Remoting Common.cs Client.cs -o Client.exe



  Error with latest CVS version [from yesterday]:

  mono --debug Server.exe

  Unhandled Exception: System.Runtime.Remoting.RemotingException: Some sort of w32 error occurred: 0



  Server stack trace:

  in <0x000da> 00 System.Runtime.Remoting.Channels.Tcp.HostConnectionPool:CreateConnection ()

  in <0x00108> 00 System.Runtime.Remoting.Channels.Tcp.HostConnectionPool:GetConnection ()

  in <0x00121> 00 System.Runtime.Remoting.Channels.Tcp.TcpConnectionPool:GetConnection (string,int)

  in <0x00026> 00 System.Runtime.Remoting.Channels.Tcp.TcpClientTransportSink:ProcessMessage (System.Runtime.Remoting.Mess

  aging.IMessage,System.Runtime.Remoting.Channels.ITransportHeaders,System.IO.Stream,System.Runtime.Remoting.Channels.ITra

  nsportHeaders&,System.IO.Stream&)

  in <0x001e7> 00 System.Runtime.Remoting.Channels.BinaryClientFormatterSink:SyncProcessMessage (System.Runtime.Remoting.M

  essaging.IMessage)





  Exception rethrown at [0]:



  in <0x000c3> 00 System.Runtime.Remoting.Proxies.RealProxy:PrivateInvoke (System.Runtime.Remoting.Proxies.RealProxy,Syste

  m.Runtime.Remoting.Messaging.IMessage,System.Exception&,object[]&)

  in (unmanaged) 07 .ServerBroker:RegisterServer (string,ServerObj)

  in <0x00004> 07 .ServerBroker:RegisterServer (string,ServerObj)

  in [0x0004a] (at c:\data\Projects\Test\ErrorIn1.1Remoting\Server.cs:59) 00 .Server:Main (string[])



  - Urs C. Muff

  Software Architect      - Research Lab


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/pipermail/mono-devel-list/attachments/20030427/01caa191/attachment.html 


More information about the Mono-devel-list mailing list