Wcf异常处理引发错误
嗨,我在处理wcf中的异常时遇到问题。 我有这样的服务:Wcf异常处理引发错误,wcf,exception,faultexception,Wcf,Exception,Faultexception,嗨,我在处理wcf中的异常时遇到问题。 我有这样的服务: [ServiceContract] public interface IAddressService { [OperationContract] [FaultContract(typeof(ExecuteCommandException))] int SavePerson(string idApp, int idUser, Person person); } 我正在WCFTestClient实用程序中调用服务上的
[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'"));
}
}
我没有得到上面的错误,但我只得到异常消息,没有内部异常。
我做错了什么?定义故障合同时:
[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'"));
}
[DataContract]
公共类ExecuteCommandInfo{
[数据成员]
公共字符串消息;
}
[服务合同]
公共接口服务{
[经营合同]
[FaultContract(类型(ExecuteCommandInfo))]
int SavePerson(字符串idApp、int idUser、Person);
}
catch(ExecuteCommandException ex){
抛出新的FaultException(新的ExecuteCommandInfo{Message=ex.Message},新的FaultReason(“SavePerson”中的错误));
}
ExecuteCommandException是否可序列化?ExecuteCommandException继承自Exception并标记为可序列化。我发现,如果我发送异常,上述错误就会发生。发现在服务器端抛出异常时,wcf会关闭通道并断开客户端的连接。
[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'"));
}