在我们以前的demo中我们未对异常进行任何的处理,一量异常发生,我们的系统则没有能力自我保护,轻微的错误会导到当前用户的操作失败,严重的是系统崩溃。我们总是习惯对于可以预见的错误以一种友好的方式来提醒用户,而不是把系统抛出的信息暴露给客户,因为这样可能会给我们的系统带来危害,这就是异常处理的原则。
下面我们通过改造以前的demo来看wcf对异常是怎么样处理的。
一、添加异常
string GetEmpNameByID(int id)接口是获取指定id的员工姓名,当id不存在时,我们希望抛出一个异常,客户端通过catch这个异常来告诉客户当前状况。
public string GetEmpNameByID(int id)
{
List<Employee> emps = Database.GetAll();
var names = from p in emps
where p.ID == id
select p.Name;
if (names.Count() > 0)
{
return names.First<string>();
}
throw new Exception("employee no exists");
}
抛出一个Excepion的异常是一种简单的处理办法,实际应用不应当是这样的,此处仅为演示方便。
客户端在调用GetEmpNameByID时添加try/catch(Exception ex)并把异常的类型和异常的message打印出来。运行程序后,实际上我们看到的异常信息是这样的。
异常类型:System.ServiceModel.FaultException
异常信息:由于内部错误,服务器无法处理该请求。有关该错误的详细信息,请打开服务器上的 IncludeExceptionDetailInFaults (从 ServiceBehaviorAttribute 或从 <serviceDebug> 配置行为)以便将异常信息发送回客户端,或在打开每个 Microsoft .NET Framework 3.0 SDK 文档的跟踪的同时检查服务器跟踪日志。
如果把这些信息显示给我们的用户,一点价值都没有。根据提示,我们在服务端的web.config中增加<serviceDebug>配置行为<serviceDebug includeExceptionDetailInFaults="True"/>,再次运行后。
异常类型:System.ServiceModel.FaultException`1[System.ServiceModel.ExceptionDetail]
异常信息:employee no exists
呵呵,似乎很相似,只是类型不一样。可以看来在服务端抛出的异常总会是FaultException异常类,那么我们想得到具体的异常类型如何做呢。
wcf对自定义异常提供了支持,通过定义一个异常描述的契约,如
namespace Shore.cnblogs.com.WCFData
{
[DataContract]
public class EmployeeNotFind
{
[DataMember]
public string Code;
[DataMember]
public string Message;
}
}
我们把它入到WCFData组件中,因为这也是一个数据契约。
修改服务契约
[ServiceContract]
public interface IEmployeeService
{
[OperationContract]
void AddEmployee(Employee em);
[FaultContract(typeof(EmployeeNotFind))]
[OperationContract]
string GetEmpNameByID(int id);
}
注释trow new Exception("employee no exists");
改为
EmployeeNotFind notFind = new EmployeeNotFind();
notFind.Code = id.ToString();
notFind.Message = "employee no exists";
throw new FaultException<EmployeeNotFind>(notFind);
客户端
catch (System.ServiceModel.FaultException<EmployeeNotFind> fe)
{
EmployeeNotFind notfind = fe.Detail;
Console.WriteLine("exception type={0},exception description={1}", notfind.GetType().ToString(), notfind.Message);
}
下载Demo
