I can't manage to serialize remote objects to a file. I either get a
NotSupportedException or a RemotingException. Is this by design or a bug? Or
am I doing something wrong?

-----

'System.Runtime.Remoting.RemotingException' occurred in mscorlib.dll

Additional information: Attempted to call a method declared on type
System.Runtime.Remoting.Messaging.IMethodMessage on an object which exposes
Rem.Remote.

-----

An unhandled exception of type 'System.NotSupportedException' occurred in
mscorlib.dll

Additional information: Method is not supported.

-----

The following is a simple test case.
SaveLocal works fine, SaveRemote throws NotSupportedException and
SerializeRemote throws RemotingException.


Peter

----------- Client console application:

using System;
using System.Runtime.Serialization.Formatters.Soap;
using System.IO;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels.Http;

namespace SeriTest
{
 class Class1
 {
  [STAThread]
  static void Main(string[] args)
  {
   HttpChannel chan = new HttpChannel();
   Rem.Remote remote = (Rem.Remote)
RemotingServices.Connect(typeof(Rem.Remote),
"http://localhost:8080/remote.rem";);
   Class1.SaveLocal();
   Class1.SaveRemote(remote);
   Class1.SerializeRemote(remote);
   Console.WriteLine("Press a key...");
   Console.ReadLine();
  }

  // order the remote object to serialize itself
  static void SaveRemote(Rem.Remote remote)
  {
   Console.WriteLine("Saving Remote...");
   remote.Save();
  }

  // order the local object to serialize itself
  static void SaveLocal()
  {
   Console.WriteLine("Saving Local...");
   Rem.Remote r = new Rem.Remote();
   r.Save();
  }

  // serialize the remote object
  static void SerializeRemote(Rem.Remote remote)
  {
   Console.WriteLine("Serializing Remote...");
   String filename = "test.ser";
   using (FileStream stream = new FileStream(filename, FileMode.Create))
   {
    SoapFormatter formatter = new SoapFormatter();
    formatter.Serialize(stream, remote);
   }
  }
 }
}

----------- Server console app:

using System;
using System.Runtime.Serialization;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels.Http;

namespace S
{
 class Server
 {
  [STAThread]
  static void Main(string[] args)
  {
   HttpChannel chan = new HttpChannel(8080);
   RemotingConfiguration.RegisterWellKnownServiceType(typeof(Rem.Remote),
"remote.rem", WellKnownObjectMode.Singleton);
   Console.WriteLine("Server online. Press a key...");
   Console.ReadLine();
  }
 }
}

----------- Remote object dll:

using System;
using System.Runtime.Remoting;
using System.Runtime.Serialization.Formatters.Soap;
using System.IO;

namespace Rem
{
 [Serializable]
 public class Remote : MarshalByRefObject
 {
  public String s = "test1";

  public void Save()
  {
   String filename = "test.ser";
   using (FileStream stream = new FileStream(filename, FileMode.Create))
   {
    SoapFormatter formatter = new SoapFormatter();
    formatter.Serialize(stream, this);
   }
  }
 }
}

You can read messages from the DOTNET archive, unsubscribe from DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

Reply via email to