预览加载中,请您耐心等待几秒...
1/2
2/2

在线预览结束,喜欢就下载吧,查找使用更方便

如果您无法下载资料,请参考说明:

1、部分资料下载需要金币,请确保您的账户上有足够的金币

2、已购买过的文档,再次下载不重复扣费

3、资料包下载后请先用软件解压,在使用对应软件打开

为你解决WCF异常问题 WCF还是比较常用的,于是我研究了一下WCF,在这里拿出来和大家分享一下,希望对大家有用。异常消息与特定技术有关,.NET异常同样如此,因而WCF并不支持传统的异常处理方式。如果在WCF服务中采用传统的方式处理异常,由于异常消息不能被序列化,因而客户端无法收到服务抛出的WCF异常,例如这样的服务设计: [ServiceContract(SessionModeSessionMode=SessionMode.Allowed)] publicinterfaceIDocumentsExplorerService { [OperationContract] DocumentListFetchDocuments(stringhomeDir); } [ServiceBehavior(InstanceContextModeInstanceContextMode=InstanceContextMode.Single)] publicclassDocumentsExplorerService:IDocumentsExplorerService,IDisposable { publicDocumentListFetchDocuments(stringhomeDir) { //SomeCodes if(Directory.Exists(homeDir)) { //Fetchdocumentsaccordingtohomedir } else { thrownewDirectoryNotFoundException( string.Format("Directory{0}isnotfound.",homeDir)); } } publicvoidDispose() { Console.WriteLine("Theservicehadbeendisposed."); } } 则客户端在调用如上的服务操作时,如果采用如下的捕获方式是无法获取该WCF异常的: catch(DirectoryNotFoundExceptionex) { //handletheexception; } 为了弥补这一缺陷,WCF会将无法识别的异常均当作为FaultException异常对象,因此,客户端可以捕获FaultException或者Exception异常: catch(FaultExceptionex) { //handletheexception; } catch(Exceptionex) { //handletheexception; } 然而,这样捕获的WCF异常,却无法识别DirectoryNotFoundException所传递的错误信息。尤为严重的是这样的异常处理方式还会导致传递消息的通道出现错误,当客户端继续调用该服务代理对象的服务操作时,会获得一个CommunicationObjectFaultedException异常,无法继续使用服务。如果服务被设置为PerSession模式或者Single模式,异常还会导致服务对象被释放,终止服务。 [ServiceContract(SessionModeSessionMode=SessionMode.Allowed)] publicinterfaceIDocumentsExplorerService { [OperationContract] [FaultContract(typeof(DirectoryNotFoundException))] DocumentListFetchDocuments(stringhomeDir); }