如果服务器端应用程序逻辑抛出异常,它应该能够编组到客户端,让它知道发生了什么。您可以通过故意在远程对象的方法之一中抛出异常来测试这一点。然后从客户端调用该特定方法,期待异常:
HttpChannel channel = new HttpChannel();
ChannelServices.RegisterChannel(channel);
IMyRemoteObject obj = (IMyRemoteObject) Activator.GetObject(
typeof(IMyRemoteObject),
"http://localhost:1234/MyRemoteObject.soap");
Console.WriteLine("Client.Main(): Reference to rem.obj. acquired");
int tmp = obj.GetValue();
Console.WriteLine("Client.Main(): Original server side value: {0}",tmp);
Console.WriteLine("Client.Main(): Will set value to 42");
try
{
// This method will throw an ApplicationException in the server-side code.
obj.SetValue(42);
}
catch (Exception ex)
{
Console.WriteLine("=====");
Console.WriteLine("Exception type: " + ex.GetType().ToString());
Console.WriteLine("Message: " + ex.Message);
Console.WriteLine("Source: " + ex.Source);
Console.WriteLine("Stack trace: " + ex.StackTrace);
Console.WriteLine("=====");
}
您可以期望收到这样的异常
=====
Exception type: System.ApplicationException
Message: testing
Source: Server
Stack trace:
Server stack trace:
at Server.MyRemoteObject.SetValue(Int32 newval) in i:\projects\remoting.net\ch03\01_singlecallobjects\server\server.cs:line 27
at System.Runtime.Remoting.Messaging.StackBuilderSink.PrivateProcessMessage(MethodBase mb, Object[] args, Object server, Int32 methodPtr, Boolean fExecuteInContext, Object[]& outArgs)
at System.Runtime.Remoting.Messaging.StackBuilderSink.SyncProcessMessage(IMessage msg, Int32 methodPtr, Boolean fExecuteInContext)
Exception rethrown at [0]:
at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
at General.IMyRemoteObject.SetValue(Int32 newval)
at Client.Client.Main(String[] args) in i:\projects\remoting.net\ch03\01_singlecallobjects\client\client.cs:line 29
=====
它应该告诉您源在服务器上,并带有服务器端堆栈跟踪。