2010-09-02 153 views
2

您好我有一個處理wcf異常的問題。 我有這樣一個服務:Wcf異常處理拋出錯誤

[ServiceContract] 
public interface IAddressService 
{ 
    [OperationContract] 
    [FaultContract(typeof(ExecuteCommandException))] 
    int SavePerson(string idApp, int idUser, Person person); 
} 

我就在WCFTestClient公用事業服務調用SavePerson()。 的SavePerson()的實現是:

public int SavePerson(string idApp, int idUser, Person person) 
{ 
    try 
    { 
     this._savePersonCommand.Person = person; 

     this.ExecuteCommand(idUser, idApp, this._savePersonCommand); 

     return this._savePersonCommand.Person.Id; 
    } 
    catch (ExecuteCommandException ex) 
    { 
     throw new FaultException<ExecuteCommandException>(ex, new FaultReason("Error in 'SavePerson'")); 
    } 
} 

但我得到這個錯誤:如果我改變SavePerson方法,取而代之的

Failed to invoke the service. Possible causes: The service is offline or inaccessible; the client-side configuration does not match the proxy; the existing proxy is invalid. Refer to the stack trace for more detail. You can try to recover by starting a new proxy, restoring to default configuration, or refreshing the service.

catch (ExecuteCommandException ex) 
{ 
    throw new FaultException<ExecuteCommandException>(ex, new FaultReason("Error in 'SavePerson'")); 
} 

catch(Exception) 
{ 
    throw; 
} 

我沒有得到上述錯誤,但我只收到異常消息,沒有內部異常。 我在做什麼錯?

+0

是ExecuteCommandException序列化? – 2010-09-06 03:52:19

+0

ExecuteCommandException從Exception繼承並標記爲可序列化。我發現如果我發送異常發生上述錯誤。並發現當在服務器端引發異常時,wcf關閉通道並斷開客戶端。 – Luka 2010-09-06 06:46:01

回答

3

當你定義錯誤契約:

[FaultContract(typeof(ExecuteCommandException))] 

你不能指定的異常類型。相反,您可以指定您選擇的數據合約,以傳回您認爲必要的任何值。

例如:

[DataContract] 
public class ExecuteCommandInfo { 
    [DataMember] 
    public string Message; 
} 

[ServiceContract] 
public interface IAddressService { 
    [OperationContract] 
    [FaultContract(typeof(ExecuteCommandInfo))] 
    int SavePerson(string idApp, int idUser, Person person); 
} 

catch (ExecuteCommandException ex) { 
    throw new FaultException<ExecuteCommandInfo>(new ExecuteCommandInfo { Message = ex.Message }, new FaultReason("Error in 'SavePerson'")); 
}