按你的说法,是在客户端定制服务器端对象的事件, 这样可以吗?
如果你的eventhandler类也被指定为远程的(我是通过在xml设置的方式来指定某个类是在远程实例化还是还是在本地的), 有可能还可以不报错. 如果没有指定在远程实例化,肯定是不行的.这个我也没试过.
在使用远程对象时有时还是有些限的.比喻我想在客户端引用远程的静态类就无法实现.
如果你的eventhandler类也被指定为远程的(我是通过在xml设置的方式来指定某个类是在远程实例化还是还是在本地的), 有可能还可以不报错. 如果没有指定在远程实例化,肯定是不行的.这个我也没试过.
在使用远程对象时有时还是有些限的.比喻我想在客户端引用远程的静态类就无法实现.
/// <summary>
/// 事件包装器,通过它来订阅远程的事件
/// </summary>
public class BroadcastEventWrapper: MarshalByRefObject
{
/// <summary>
/// 广播事件
/// </summary>
public event BroadcastEventHandler MessageArrivedLocally; // don't use OneWay here!
/// <summary>
/// 订阅事件
/// </summary>
/// <param name="Sender"></param>
/// <param name="e"></param>
public void LocallyHandleMessageArrived (object Sender, BroadcastEventArgs e)
{
// forward the message to the client
//BroadcastEventArgs e = new BroadcastEventArgs(gid, optype);
MessageArrivedLocally(this, e);
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public override object InitializeLifetimeService()
{
// this object has to live "forever"
return null;
}
}
使用它:
BroadcastEventWrapper eventWrapper = new BroadcastEventWrapper();
eventWrapper.MessageArrivedLocally +=new BroadcastEventHandler(this.gateSrv_GateBroadcaster);
gateSrv.GateBroadcaster +=new BroadcastEventHandler(eventWrapper.LocallyHandleMessageArrived);
private void btnStart_Click(object sender, System.EventArgs e)
{
//Connecting to Server
if(_channel == null)
{
// 关键在后面这14行
// 定义provider,如果需要加入callback,TypeFilterLevel就一定要是Full
BinaryServerFormatterSinkProvider provider = new BinaryServerFormatterSinkProvider();
provider.TypeFilterLevel = System.Runtime.Serialization.Formatters.TypeFilterLevel.Full;
// creating the IDictionary to set the port on the channel instance.
IDictionary props = new Hashtable();
// Create a channel at port number
// 当server调用client的时候一定要为client定义channel
props["port"] = 0;
// pass the props for the port setting and the server provider in the
// server chain argument. (Client remains null here.)
this._channel = new TcpChannel(props, null, provider);
}
try
{
// Register the channel
ChannelServices.RegisterChannel(_channel);
}
catch
{
_channel = null;
WriteLine("RegisterChannel Failed!!!");
return;
}try
{
_RO = (RemoteService)Activator.GetObject(typeof(RemoteService), textUrl.Text);
_RO.myEvent+= new MyDelegate(myFunc);
//_RO.join(_observerRO);
WriteLine("Start ok!");
//throw new NullReferenceException();
}
catch(System.Exception me)
{
if(_channel != null)
{
ChannelServices.UnregisterChannel(_channel);
}
_RO = null;
_channel = null;
string msg = "Server not available";
WriteLine(msg);
return;
}
}
说明: 应用程序试图执行安全策略不允许的操作。要授予此应用程序所需的权限,请与系统管理员联系,或在配置文件中更改该应用程序的信任级别。 异常详细信息: System.Security.SecurityException: 请求失败。源错误: 执行当前 Web 请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪信息确定有关异常原因和发生位置的信息。 堆栈跟踪:
[SecurityException: 请求失败。]