【IT168 专稿】
异常消息与特定技术有关,.NET异常同样如此,因而WCF并不支持传统的异常处理方式。如果在WCF服务中采用传统的方式处理异常,由于异常消息不能被序列化,因而客户端无法收到服务抛出的异常,例如这样的服务设计:
[ServiceContract(SessionMode = SessionMode.Allowed)]
public interface IDocumentsExplorerService
{
[OperationContract]
DocumentList FetchDocuments(string homeDir);
}
[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]
public class DocumentsExplorerService : IDocumentsExplorerService,IDisposable
{
public DocumentList FetchDocuments(string homeDir)
{
//Some Codes
if (Directory.Exists(homeDir))
{
//Fetch documents according to homedir
}
else
{
throw new DirectoryNotFoundException(
string.Format("Directory {0} is not found.",homeDir));
}
}
public void Dispose()
{
Console.WriteLine("The service had been disposed.");
}
}
catch (DirectoryNotFoundException ex)
{
//handle the exception;
}
| 第1页: 传统方法处理异常 | 第2页: FaultContract特性 |
| 第3页: System.ServiceModel.Dispatcher.IErro... | 第4页: 安装错误处理扩展 |