2010-09-02 177 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'")); 
}