我遇到服务器从客户端请求信息的情况。
回调声明:
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,就会正确捕获异常。它可能与我的回调设置有关。