我试图了解 .NET 远程处理,试图在 .NET 中复制 VB6 ActiveX EXE。
到目前为止,我在所有客户端都可以共享的服务器上实例化了一个单例。
单例接受来自客户端的请求并验证数据,以事件的形式返回验证数据。这很好用——请求引用单例的类会触发它们的事件——即它们发送数据,接收经过验证的数据。
但是,我需要一个接口。客户端托管在 WPF 应用程序中(服务器也是如此),当它们接收数据时,我需要更新显示(文本框、列表框等)以反映客户端和单例之间的通信。
但是,一旦我添加了一个在主窗体中实现的事件供客户端在收到单例的回复后调用,我就会遇到运行时错误,抱怨主窗体没有序列化属性......
为了保持简洁,我将描述该过程如下
服务器运行代码:
BinaryClientFormatterSinkProvider clientProvider = new BinaryClientFormatterSinkProvider();
BinaryServerFormatterSinkProvider serverProvider = new BinaryServerFormatterSinkProvider();
//
IDictionary myDictionary = new Hashtable();
myDictionary["name"] = String.Format("PracticonChannel_{0}", Port);
myDictionary["typeFilterLevel"] = TypeFilterLevel.Full;
myDictionary["port"] = Port.ToString();
serverProvider.TypeFilterLevel = TypeFilterLevel.Full;
http = new HttpChannel(myDictionary, clientProvider, serverProvider);
// Register RemotingShared.SingletonObject as a
// Singleton Server-Activated type.
RemotingConfiguration.RegisterWellKnownServiceType(
typeof(Practicon.RemotingShared.UploadObjectSingleton), // Server-activated type
"SingletonService", // objectUri
WellKnownObjectMode.Singleton // Singleton instancing mode
);
RemotingConfiguration.ApplicationName = " Upload Server";
RemotingConfiguration.RegisterActivatedServiceType(
typeof(Practicon.RemotingShared.UploadObjectSingleton));
客户端通过以下方式获取服务器激活的单例:
HttpChannel http1;
// Set the formatters of the messages for delivery.
BinaryClientFormatterSinkProvider clientProvider = new BinaryClientFormatterSinkProvider();
BinaryServerFormatterSinkProvider serverProvider = new BinaryServerFormatterSinkProvider();
//
IDictionary myDictionary = new Hashtable();
myDictionary["name"] = String.Format("PracticonChannel_{0}", Port);
myDictionary["typeFilterLevel"] = TypeFilterLevel.Full;
myDictionary["port"] = port.ToString();
serverProvider.TypeFilterLevel = TypeFilterLevel.Full;
http1 = new HttpChannel(myDictionary, clientProvider, serverProvider);
ChannelServices.RegisterChannel(http1, false);
uploadObj= (UploadObjectSingleton)Activator.GetObject(
typeof(UploadObjectSingleton),
fullAddress);
//---------- Here's the problem...
uploadObj.ReplyEvent += new UploadObjectReplyEventHandler(OnUploadReply);
OnUploadReply 是一个表单实现的事件,用于更新各种控件。当它在运行时分配时,由于主窗体缺少序列化属性而发生序列化异常。
这让我发疯了。有人可以向我展示/解释/告诉/讲道/讲授我如何更新用户界面以响应单例中触发的事件吗?