2011-11-24 106 views
0

我想從WCF服務器拋出自定義異常,但它對我的客戶端不起作用。我做什麼:WCF:無法在客戶端上處理FaultException <T>

我的自定義異常:

[DataContract]  
public class MyCustomException 
{ 
    [DataMember] 
    public string MyField { get; set; } 
} 

我的WCF服務合同

[ServiceContract]  
public interface IMyService 
{ 
     [OperationContract] 
     [FaultContract(typeof(MyCustomException))] 
     bool Foo(); 
} 

爲WCF服務我的全局異常處理程序(此代碼擊中時富稱):

public void ProvideFault(Exception error, System.ServiceModel.Channels.MessageVersion version, ref System.ServiceModel.Channels.Message fault) 
{ 
    var ex = new MyCustomException { MyField = "..." }; 
    var fe = new FaultException<MyCustomException>(
        ex, 
        new FaultReason("reason"), 
        FaultCode.CreateSenderFaultCode(new FaultCode("some-string"))); 

    var flt = fe.CreateMessageFault(); 
    fault = Message.CreateMessage(
    version, 
    flt, 
    string.Empty 
);   
} 

然後...我的客戶:

try 
{ 
    Create channel factory and call Foo 
} 
catch(FaultException<MyCustomException> ex) 
{ 
    // OOOPS! It doesn't work!!! 
} 
catch(FaultException ex) 
{ 
    // This block catches exception 
} 

這裏有什麼問題?先謝謝你!

回答

2

我已經找到了問題ProvideFault方法:

fault = Message.CreateMessage(
    version, 
    flt, 
    string.Empty 
); 

fault = Message.CreateMessage(
    version, 
    flt, 
    fe.Action 
); 

代替現在一切都OK!

相關問題