using System;
using System.Collections;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Security.Permissions;
using System.Runtime.Serialization.Formatters;
using Remote;namespace YiLongServer
{
[Serializable]
class Server
{
static void Main(string[] args)
{
BinaryServerFormatterSinkProvider provider = new BinaryServerFormatterSinkProvider();
provider.TypeFilterLevel = TypeFilterLevel.Full;
IDictionary props = new Hashtable();
props["port"] = 9090;
TcpChannel serverChannel = new TcpChannel(props, null, provider);
ChannelServices.RegisterChannel(serverChannel, false);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemoteObject), "Update", WellKnownObjectMode.SingleCall);
Console.WriteLine("Press return to exit");
Console.ReadLine();
}
}
}服务器
远程对象using System;
using System.Data;
using System.Threading;
using System.Data.SqlClient;namespace Remote
{
public class RemoteObject : MarshalByRefObject
{
public delegate void StatusEvent(object sender, StatusEventArgs e); public event StatusEvent Status; public RemoteObject()
{
Console.WriteLine("One Object has created");
} ~RemoteObject()
{
Console.WriteLine("One Object has destroy");
}
public void LongWorking(int ms)
{
Console.WriteLine("RemoteObject:LongWorking() Started");
StatusEventArgs e = new StatusEventArgs("Message for Client:LongWork() Started");
if(Status!=null)
{
Console.WriteLine("RemoteObject:Firing Starting Event");
Status(this,e);
}
System.Threading.Thread.Sleep(ms);
e.Message = "Message for Client LongWorking Ending";
if(Status!=null)
{
Console.WriteLine("RemoteObject:Firing Ending Event");
Status(this,e);
}
Console.WriteLine("RemoteObject LongWorking() Ending");
}
} [Serializable]
public class StatusEventArgs
{
private string message;
public StatusEventArgs(string m)
{
message = m;
} public string Message
{
get{
return message;
}
set{
message = value;
}
}
}
}
Clientusing System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Remoting.Messaging;
using Remote;namespace Client
{
class Program
{
static void Main(string[] args)
{
Remote.RemoteObject Shareobject = (Remote.RemoteObject)Activator.GetObject(typeof(Remote.RemoteObject), "tcp://localhost:9090/Update"); //125.45.61.205:9090
Console.WriteLine(Shareobject.GetHashCode().ToString());
EventSink sink = new EventSink(); Shareobject.Status += new RemoteObject.StatusEvent(sink.StatusHandler);
Shareobject.LongWorking(5000);
Console.WriteLine(Shareobject.GetHashCode().ToString());
Console.ReadLine();
}
} public class EventSink : MarshalByRefObject
{
public EventSink()
{
} public void StatusHandler(object sender, StatusEventArgs e)
{
Console.WriteLine("EventSink:Event occurred:"+e.Message);
}
}
}
为什么每次这个地方都运行到这句就报错
Shareobject.Status += new RemoteObject.StatusEvent(sink.StatusHandler);Exception has been thrown by the target of an invocation
请问怎么销毁远程对象谢谢
using System.Collections;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Security.Permissions;
using System.Runtime.Serialization.Formatters;
using Remote;namespace YiLongServer
{
[Serializable]
class Server
{
static void Main(string[] args)
{
BinaryServerFormatterSinkProvider provider = new BinaryServerFormatterSinkProvider();
provider.TypeFilterLevel = TypeFilterLevel.Full;
IDictionary props = new Hashtable();
props["port"] = 9090;
TcpChannel serverChannel = new TcpChannel(props, null, provider);
ChannelServices.RegisterChannel(serverChannel, false);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemoteObject), "Update", WellKnownObjectMode.SingleCall);
Console.WriteLine("Press return to exit");
Console.ReadLine();
}
}
}服务器
远程对象using System;
using System.Data;
using System.Threading;
using System.Data.SqlClient;namespace Remote
{
public class RemoteObject : MarshalByRefObject
{
public delegate void StatusEvent(object sender, StatusEventArgs e); public event StatusEvent Status; public RemoteObject()
{
Console.WriteLine("One Object has created");
} ~RemoteObject()
{
Console.WriteLine("One Object has destroy");
}
public void LongWorking(int ms)
{
Console.WriteLine("RemoteObject:LongWorking() Started");
StatusEventArgs e = new StatusEventArgs("Message for Client:LongWork() Started");
if(Status!=null)
{
Console.WriteLine("RemoteObject:Firing Starting Event");
Status(this,e);
}
System.Threading.Thread.Sleep(ms);
e.Message = "Message for Client LongWorking Ending";
if(Status!=null)
{
Console.WriteLine("RemoteObject:Firing Ending Event");
Status(this,e);
}
Console.WriteLine("RemoteObject LongWorking() Ending");
}
} [Serializable]
public class StatusEventArgs
{
private string message;
public StatusEventArgs(string m)
{
message = m;
} public string Message
{
get{
return message;
}
set{
message = value;
}
}
}
}
Clientusing System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Remoting.Messaging;
using Remote;namespace Client
{
class Program
{
static void Main(string[] args)
{
Remote.RemoteObject Shareobject = (Remote.RemoteObject)Activator.GetObject(typeof(Remote.RemoteObject), "tcp://localhost:9090/Update"); //125.45.61.205:9090
Console.WriteLine(Shareobject.GetHashCode().ToString());
EventSink sink = new EventSink(); Shareobject.Status += new RemoteObject.StatusEvent(sink.StatusHandler);
Shareobject.LongWorking(5000);
Console.WriteLine(Shareobject.GetHashCode().ToString());
Console.ReadLine();
}
} public class EventSink : MarshalByRefObject
{
public EventSink()
{
} public void StatusHandler(object sender, StatusEventArgs e)
{
Console.WriteLine("EventSink:Event occurred:"+e.Message);
}
}
}
为什么每次这个地方都运行到这句就报错
Shareobject.Status += new RemoteObject.StatusEvent(sink.StatusHandler);Exception has been thrown by the target of an invocation
请问怎么销毁远程对象谢谢
Shareobject.Status += new RemoteObject.StatusEvent(sink.StatusHandler);Exception has been thrown by the target of an invocation