gpt4 book ai didi

Wcf 异常处理抛出错误

转载 作者:行者123 更新时间:2023-12-04 06:46:58 25 4
gpt4 key购买 nike

嗨,我在处理 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'"));
}
}

但我收到此错误:

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.



如果我更改 SavePerson 方法而不是:
catch (ExecuteCommandException ex)
{
throw new FaultException<ExecuteCommandException>(ex, new FaultReason("Error in 'SavePerson'"));
}

我愿意
catch(Exception)
{
throw;
}

我没有收到上述错误,但我只收到异常消息而没有内部异常。
我究竟做错了什么?

最佳答案

当您定义故障契约(Contract)时:

[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'"));
}

关于Wcf 异常处理抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3628056/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com