我有一种情况,服务器从客户端请求信息。
回调声明:
public interface IControlServiceCallback
{
[...]
[OperationContract]
[FaultContract(typeof(GetSystemFilesFault))]
List<SystemFile> GetSystemFiles(string directory);
}
回调实现:
public List<SystemFile> GetSystemFiles(string directory)
{
try
{
var files = Directory.GetDirectories(directory)
.Select(x => new DirectoryInfo(x))
.Select(x => new SystemFile(x)).ToList();
files.AddRange(Directory.GetFiles(directory)
.Select(x => new FileInfo(x))
.Select(x => new SystemFile(x)));
return files;
}
catch (UnauthorizedAccessException e)
{
throw new FaultException<GetSystemFilesFault>(new GetSystemFilesFault(e));
}
catch (Exception e)
{
throw new FaultException<GetSystemFilesFault>(new GetSystemFilesFault(e));
}
}
问题是,当我调用该方法并FaultException<TDetail>
在客户端抛出一个异常时,异常处理不正确。我得到了一个CommunicationException
。如下图所示。
我尝试使用一些 WCF 日志记录功能来查看是否可以隔离问题,但是一旦服务器收到响应就会引发异常。
Server stack trace:
at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)
Exception rethrown at [0]:
at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData&amp; msgData, Int32 type)
at RemoteControl.Service.IControlServiceCallback.GetSystemFiles(String directory)
at RemoteControl.Service.Objects.Handling.ControlServiceCallbackProxy.GetSystemFiles(String directory) in c:\Users\Bruno\Source\Repos\RemoteControl\RemoteControl\RemoteControl.Service\Objects\Handling\ControlServiceCallbackProxy.cs:line 46
at RemoteControl.Host.Controllers.ClientController.SelectedDirectoryChanged(String path) in c:\Users\Bruno\Source\Repos\RemoteControl\RemoteControl\RemoteControl.Host\Controllers\ClientController.cs:line 41
at RemoteControl.Host.Forms.ClientForm.treeList_DoubleClick(Object sender, EventArgs e) in c:\Users\Bruno\Source\Repos\RemoteControl\RemoteControl\RemoteControl.Host\Forms\ClientForm.cs:line 95
什么可能导致这种行为?我的代码不应该进入catch (FaultException<FaultBase> e)
区块吗?
首先,我怎样才能找到问题?
编辑:
我刚刚发现异常被正确捕获,只要回调方法返回 void。这可能与我的回调设置有关。