商讯信箱
用户名: @
密  码:   注册|忘记密码
登录
个人用户经销商
您的位置:首页 > 技术频道 > 正文


【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 3 4
©版权所有。未经许可,不得转载。
[责任编辑:胡铭娅]
[an error occurred while processing this directive]
[an error occurred while processing this directive]
[an error occurred while processing this directive]