在使用ClientSponsor的Register函数的时候,出现
未处理的“System.Runtime.Serialization.SerializationException”类型的异常出现在
mscorlib.dll 中。
其他信息: 由于安全限制,无法访问类型 System.Runtime.Remoting.ObjRef。是什么意思,ths
未处理的“System.Runtime.Serialization.SerializationException”类型的异常出现在
mscorlib.dll 中。
其他信息: 由于安全限制,无法访问类型 System.Runtime.Remoting.ObjRef。是什么意思,ths
解决方案 »
- 一个关于.net2.0下程序运行出现的异常问题。
- 如何将datatable中某一列数绑定到GridView表头上?
- 流媒体问题
- 在access中的”OLE对象”字段的存取?
- format不会用啊
- 有人知道向QQ那样的非活动窗口来消息时,任务栏里一闪一闪的是如何做到的?
- 谁知道在那里可以下载要关于图书管理系统的源码~~~~
- AspNetForums是微软的官方.Net技术的论坛,本机安装遇困难,朋友们来看看....
- checkbox的size属性怎么无法改变
- 关于System.Data的问题
- C#中将记录集写入XML然后从XML读入记录集的问题(100分)
- 用.net remoting访问驻留在iis中的程序时的问题
在使用remoting技术时,对象需要能够序列化,如果对象不能序列化就会报错
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>