【发布时间】:2017-06-13 01:08:57
【问题描述】:
我的ServiceBase 抽象类中有以下代码:
internal ServiceResponse ExecuteNonQuery(Action action)
{
try
{
action();
return new ServiceResponse() { IsFaulty = false };
}
catch (DbEntityValidationException e)
{
OnDbEntityValidationException(e);
return new ServiceResponse() { IsFaulty = true, Exception = e};
}
catch (Exception e)
{
_logger.Log(e);
return new ServiceResponse() {IsFaulty = true, Exception = e};
}
}
我的所有服务都派生自此类,我的 WCF 服务端点如下所示:
public ServiceResponse Add(Client client)
{
_logger.LogInformation($"ClientService.Add()");
return ExecuteNonQuery(() =>
{
using (var context = _contextFactory.Build())
{
context.Clients.Add(_serviceToDalMapper.Map(client));
context.SaveChanges();
}
});
}
在客户端调用我的服务时,我有类似的 Try/Catch 方法。
我不明白的是,当服务端抛出异常时,异常被捕获,但我仍然在客户端得到一个System.ServiceModel.CommunicationException。为什么 ?我发现了我的异常,我的服务不应该只返回ServiceResponse吗?
【问题讨论】:
-
可能在catch块内抛出异常