在使用ClientSponsor的Register函数的时候,出现
未处理的“System.Runtime.Serialization.SerializationException”类型的异常出现在 
mscorlib.dll 中。
其他信息: 由于安全限制,无法访问类型 System.Runtime.Remoting.ObjRef。是什么意思,ths

解决方案 »

  1.   

    是序列化时出现的错误。
    在使用remoting技术时,对象需要能够序列化,如果对象不能序列化就会报错
      

  2.   

    贴出程序
    MyRemoteObject.csusing System;
    using System.Runtime.Remoting.Lifetime;namespace Wrox.Samples
    {
    public class MyRemoteObject : System.MarshalByRefObject
    {
            private int state; public MyRemoteObject(int state)
    {
    Console.WriteLine("Constructor called");
    this.state = state;
    }        public int State
            {
                get
                {
                    return state;
                }
                set
                {
                    state = value;
                }
            }        public override object InitializeLifetimeService()
            {
                ILease lease = (ILease)base.InitializeLifetimeService();
                if (lease.CurrentState == LeaseState.Initial)
                {
                    lease.InitialLeaseTime = TimeSpan.FromMinutes(1);
                    lease.RenewOnCallTime = TimeSpan.FromSeconds(20);
                }
                return lease;        }        public string Hello()
            {
                Console.WriteLine("Hello called");
                return "Hello, .NET Client!";
            }
    }
    }SimpleServer.csusing System;
    using System.Runtime.Remoting;namespace Wrox.Samples
    {
    class SimpleServer
    {
    static void Main(string[] args)
    {
             RemotingConfiguration.Configure("SimpleServer.exe.config");         Console.WriteLine("Press return to exit");
             Console.ReadLine();
    }
    }
    }SimpleServer.exe.config<configuration>
    <system.runtime.remoting>
    <application name="SimpleServer">
    <service>
    <activated type="Wrox.Samples.MyRemoteObject, MyRemoteObject" />
    </service>
    <channels>
    <channel ref="tcp" port="9000" />
    </channels>
    </application>
    </system.runtime.remoting>
    </configuration>
    SimpleClient.csusing System;
    using System.Runtime.Remoting;
    using System.Runtime.Remoting.Lifetime;
    using System.Threading;namespace Wrox.Samples
    {
        public class MySponsor: ClientSponsor, ISponsor
        {
            TimeSpan ISponsor.Renewal(ILease lease)
            {
                Console.WriteLine("Renewal called");
                return this.RenewalTime;
            }
        } class SimpleClient
    {
    static void Main(string[] args)
    {
                try
                {
                    RemotingConfiguration.Configure("SimpleClient.exe.config");                MyRemoteObject obj = new MyRemoteObject(33);
                    MySponsor sponsor = new MySponsor();
                    sponsor.RenewalTime = TimeSpan.FromMinutes(2);
                    sponsor.Register(obj);                for (int i=0; i < 20; i++)
                    {
                        DisplayLease("In loop: " + i, obj);
                        obj.Hello();
                        Thread.Sleep(25000);
                    }
                }
                catch (RemotingException ex)
                {
                    Console.WriteLine(ex.Message);
                }
    }        public static void DisplayLease(string s, MarshalByRefObject o)
            {
                ILease lease = o.GetLifetimeService() as ILease;
                if (lease != null)
                {
                    Console.WriteLine(s + " - remaining lease time: " + lease.CurrentLeaseTime);
                }
            }
    }
    }
    SimpleClient.exe.config
    <configuration>
    <system.runtime.remoting>
    <application name="SimpleClient">
    <client url="tcp://localhost:9000/SimpleServer">
    <activated type="Wrox.Samples.MyRemoteObject, MyRemoteObject" />
    </client>
    <channels>
    <channel ref="tcp" port="9002" />
    </channels>
    </application>
    </system.runtime.remoting>
    </configuration>
      

  3.   

    ClientSponsor的Register函数实现是如何的?