0
我有一些问题:如何在wcf中做异常处理?
- 它需要使用尝试捕捉在
- 我简单分层WCF服务WCF服务的所有层:
,并定义:
[DataContract]
public class ProductFault
{
public ProductFault(string msg)
{
FaultMessage = msg;
}
[DataMember]
public string FaultMessage;
}
并且具有:
public Product GetProduct(int id)
{
ProductBDO productBDO = null;
try
{
productBDO = productLogic.GetProduct(id);
}
catch (Exception e)
{
string msg = e.Message;
string reason = "GetProduct Exception";
throw new FaultException<ProductFault>
(new ProductFault(msg), reason);
}
if (productBDO == null)
{
string msg =
string.Format("No product found for id {0}",
id);
string reason = "GetProduct Empty Product";
throw new FaultException<ProductFault>(new ProductFault(msg), reason);
}
Product product = new Product();//****
TranslateProductBDOToProductDTO(productBDO,
product);
return product;
}
当我通过无效id
为“GetProduct”的方法我收到此错误信息:
如何避免在我的业务层提高错误信息并发送至客户端?
如何以及在哪一层我们可以使用try catch来处理数据库异常,例如无法连接到数据库? – 2014-12-03 02:28:43