解决“不允许类型 System.DelegateSerializationHolder 和从中派生的类型(例如 System.DelegateSerializationHolder)在此安全级别上被反序列化”错误的办法。
在.NET Framework 1.1中安全级别默认是Low的,所以不能被反序列化。以下代码是是一个聊天程序,在修改了配置文件后就可以访问了。
远程处理程序:ChatCoordinator.cs
using System;
using System.Runtime.Remoting;
using System.Collections;
[Serializable]
public class SubmitEventArgs : EventArgs{
private string _string = null;
private string _alias = null;
public SubmitEventArgs(string contribution, string contributor){
this._string = contribution;
this._alias = contributor;
}
public string Contribution{
get{
return _string;
}
}
public string Contributor{
get {
return _alias;
}
}
}
public delegate void SubmissionEventHandler(object sender, SubmitEventArgs submitArgs);
public class ChatCoordinator : MarshalByRefObject{
public ChatCoordinator(){
Console.WriteLine("聊天控制对象已经建立。 实例: " + this.GetHashCode().ToString());
}
public override object InitializeLifetimeService(){
return null;
}
public event SubmissionEventHandler Submission;
public void Submit(string contribution, string contributor){
Console.WriteLine("{0} 发送: {1}.", contributor, contribution);
SubmitEventArgs e = new SubmitEventArgs(contribution, contributor);
if (Submission != null){
Console.WriteLine("广播……");
Submission(this, e);
}
}
}
宿主应用程序:Server.cs
using System;
using System.Runtime.Remoting;
public class Server
{
public static void Main(string[] Args)
{
RemotingConfiguration.Configure("Central.config");
Console.WriteLine("服务已经启动。 按回车键退出。");
Console.ReadLine();
}
}
客户端应用程序:client.cs
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
public class ChatClient : MarshalByRefObject {
private string username = null;
public override object InitializeLifetimeService() {
return null;
}
public ChatClient(string alias){
this.username = alias;
}
public void Run(){
RemotingConfiguration.Configure("Client.config");
ChatCoordinator chatcenter = new ChatCoordinator();
chatcenter.Submission += new SubmissionEventHandler(this.SubmissionReceiver);
String keyState = "";
while (true){
Console.WriteLine("按0和回车键退出:\r\n");
keyState = Console.ReadLine();
if (String.Compare(keyState,"0", true) == 0)
break;
chatcenter.Submit(keyState, username);
}
chatcenter.Submission -= new SubmissionEventHandler(this.SubmissionReceiver);
}
public void SubmissionReceiver(object sender, SubmitEventArgs args){
if (String.Compare(args.Contributor, username, true) == 0){
Console.WriteLine("你的消息经被广播。。");
}
else
Console.WriteLine(args.Contributor + " 说: " + args.Contribution);
}
public static void Main(string[] Args){
if (Args.Length != 1){
Console.WriteLine("You need to type an alias.");
return;
}
ChatClient client = new ChatClient(Args[0]);
client.Run();
}
}
服务器端配置文件:Central.config
<configuration>
<system.runtime.remoting>
<application>
<service>
<wellknown mode="Singleton" type="ChatCoordinator, ChatCoordinator" objectUri="Chat" />
</service>
<channels>
<channel ref="http" port="8080">
<!--
以下红颜色的部分是控制按级别的
Full: .NET 远程处理的完全反序列化级别。它在所有情况下都支持远程处理所支持的全部类型。
Low: .NET 远程处理的低反序列化级别。它支持与基本的远程处理功能相关联的类型。
-->
<serverProviders>
<provider ref="wsdl" />
<formatter ref="soap" typeFilterLevel="Full" />
<formatter ref="binary" typeFilterLevel="Full" />
</serverProviders>
</channel>
</channels>
</application>
</system.runtime.remoting>
</configuration>
客户端配置文件:Client.config
<configuration>
<system.runtime.remoting>
<application>
<client>
<wellknown type="ChatCoordinator, ChatCoordinator" url="http://localhost:8080/Chat" />
</client>
<channels>
<channel ref="http" port="0">
<!--以下红颜色的部分是控制按级别的-->
<serverProviders>
<provider ref="wsdl" />
<formatter ref="soap" typeFilterLevel="Full" />
<formatter ref="binary" typeFilterLevel="Full" />
</serverProviders>
</channel>
</channels>
</application>
</system.runtime.remoting>
</configuration>
在该目录中的命令提示处,键入以下命令就可以运行了:
csc /t:library ChatCoordinator.cs
csc /r:ChatCoordinator.dll server.cs
csc /r:ChatCoordinator.dll client.cs