I have some questions:
我有一些问题:
- does it need to use try catch in all layers of wcf service
- I have simple layered wcf service:
是否需要在wcf服务的所有层中使用try catch
我有简单的分层wcf服务:
and define:
[DataContract]
public class ProductFault
{
public ProductFault(string msg)
{
FaultMessage = msg;
}
[DataMember]
public string FaultMessage;
}
and have:
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;
}
when I pass invalid id
to 'GetProduct' method I receive this error message:
当我将无效的ID传递给'GetProduct'方法时,我收到以下错误消息:
How can I avoid raising error message in my service layer and send to client?
如何避免在服务层中引发错误消息并发送给客户端?
1 个解决方案
#1
0
Do I need to use try catch in all layer of WCF service?
我是否需要在所有WCF服务层使用try catch?
No - you should only handle exceptions when you can do something about it (handle it, log it, etc.)
不 - 你应该只在处理异常时处理异常(处理,记录等)
How can avoid raising error message in my service layer and send to client?
如何避免在我的服务层中引发错误消息并发送给客户端?
What should you return if there is an exception? How else would you let the client know that something unexpected has happened? Returning null
for "product not found" seems appropriate but if there is a deeper problem I would think you'd want that exception to bubble up (or at the least log it) and let the client know that something bad has happened. How much detail you want to allow to bubble up is up to you.
如果有例外,您应该返回什么?你怎么能让客户知道发生了意想不到的事情?对于“未找到产品”返回null似乎是合适的,但如果存在更深层次的问题,我认为您希望该异常冒泡(或至少记录它)并让客户知道发生了一些不好的事情。你希望泡泡多少细节取决于你。
#1
0
Do I need to use try catch in all layer of WCF service?
我是否需要在所有WCF服务层使用try catch?
No - you should only handle exceptions when you can do something about it (handle it, log it, etc.)
不 - 你应该只在处理异常时处理异常(处理,记录等)
How can avoid raising error message in my service layer and send to client?
如何避免在我的服务层中引发错误消息并发送给客户端?
What should you return if there is an exception? How else would you let the client know that something unexpected has happened? Returning null
for "product not found" seems appropriate but if there is a deeper problem I would think you'd want that exception to bubble up (or at the least log it) and let the client know that something bad has happened. How much detail you want to allow to bubble up is up to you.
如果有例外,您应该返回什么?你怎么能让客户知道发生了意想不到的事情?对于“未找到产品”返回null似乎是合适的,但如果存在更深层次的问题,我认为您希望该异常冒泡(或至少记录它)并让客户知道发生了一些不好的事情。你希望泡泡多少细节取决于你。