请问:Remoting中,订阅事件的时候为啥要出System.Reflection.TargetInvocationException 附代码
例子来源于http://www.cnblogs.com/wayfarer/articles/75213.html文章中第二项客户端订阅服务端事件的实现方法,下面的代码把例子中初始化和客户端订阅事件的代码独立出来,创建两个项目SERVER和CLIENT,各只包含一个初始窗口,SERVER的窗口构造函数中,InitializeComponent()下面添加服务端注册和创建实例的代码:BinaryServerFormatterSinkProvider serverProvider = new BinaryServerFormatterSinkProvider();
BinaryClientFormatterSinkProvider clientProvider = new BinaryClientFormatterSinkProvider();
serverProvider.TypeFilterLevel = TypeFilterLevel.Full;
IDictionary props = new Hashtable();
props["port"] = 8080;
HttpChannel channel = new HttpChannel(props,clientProvider,serverProvider);
ChannelServices.RegisterChannel(channel);
Obj = new BroadCastObj();
ObjRef objRef = RemotingServices.Marshal(Obj,"BroadCastMessage.soap");
CLIENT窗口的构造函数中InitializeComponent()下面添加注册信道和订阅事件的代码:
BinaryServerFormatterSinkProvider serverProvider = new BinaryServerFormatterSinkProvider();
BinaryClientFormatterSinkProvider clientProvider = new BinaryClientFormatterSinkProvider();
serverProvider.TypeFilterLevel = TypeFilterLevel.Full;
IDictionary props = new Hashtable();
props["port"] = 0;
HttpChannel channel = new HttpChannel(props,clientProvider,serverProvider);
ChannelServices.RegisterChannel(channel);
watch = (IBroadCast)Activator.GetObject(
typeof(IBroadCast),"http://localhost:8080/BroadCastMessage.soap");
wrapper = new EventWrapper();
wrapper.LocalBroadCastEvent += new BroadCastEventHandler(BroadCastingMessage);
watch.BroadCastEvent += new BroadCastEventHandler(wrapper.BroadCasting);
CLIENT窗口类中提供BroadCastingMessage的具体实现
public void BroadCastingMessage(string message)
{
}
在两个项目中都添加BroadCastObj.cs,EventWrapper.cs, IBroadCast.cs作为公共接口和类的声明:
EventWrapper.cs:
using System;
using System.Runtime.Remoting.Messaging;
namespace Wayfarer.BroadCast.Common
{
/// <summary>
/// EventClass 的摘要说明。
/// </summary>
public class EventWrapper:MarshalByRefObject
{
public event BroadCastEventHandler LocalBroadCastEvent;
//[OneWay]
public void BroadCasting(string message)
{
LocalBroadCastEvent(message);
}
public override object InitializeLifetimeService()
{
return null;
}
}
}
IBroadCast.cs:
using System;
namespace Wayfarer.BroadCast.Common
{
public delegate void BroadCastEventHandler(string info);
public inte易做图ce IBroadCast
{
event BroadCastEventHandler BroadCastEvent;
void BroadCastingInfo(string info);
}
}
BroadCastObj.cs:
using System;
using System.Windows.Forms;
using System.Runtime.Remoting.Messaging;
using Wayfarer.BroadCast.Common;
namespace Wayfarer.BroadCast.RemoteObject
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
public class BroadCastObj:MarshalByRefObject,IBroadCast
{
public event BroadCastEventHandler BroadCastEvent;
#region IBroadCast 成员
//[OneWay]
public void BroadCastingInfo(string info)
{
if (BroadCastEvent != null)
{
}
}
#endregion
public override object InitializeLifetimeService()
{
return null;
}
}
}
运行时先启动服务端,客户端在运行到窗口构造函数最后一句watch.BroadCastEvent += new BroadCastEventHandler(wrapper.BroadCasting);
时出异常System.Reflection.TargetInvocationException 调用的目标发生了异常 --------------------编程问答-------------------- 哎。。又是你 帮顶。。。。 --------------------编程问答-------------------- 我记得microsoft的webcast教程中有一个remoting的系列教程。 --------------------编程问答-------------------- 你的内容太多了我也不想看了,
你把客户端的监听端口设为 非零值吧 --------------------编程问答-------------------- IDictionary props = new Hashtable();
props[ "port "] = 0;
- --------------------编程问答-------------------- 有点印象
不是很记得了,
要不上WEBCAST看看有没有Remoting相关的教程吧 --------------------编程问答-------------------- 此问题,你已经解决了吗?我也遇到了该问题,不知如何解决,可否告知如何解决的? --------------------编程问答-------------------- 顶一下!! --------------------编程问答-------------------- 我也出现相同的问题,不知是怎么回事
补充:.NET技术 , C#