Wcf异常处理抛出错误

发布于 2024-09-18 03:48:47 字数 1275 浏览 5 评论 0原文

您好,我在 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 方法而不是:

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

我这样做

catch(Exception)
{
    throw;
}

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

Hi I have a problem handling exceptions in wcf.
I have a service like this one:

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

I am calling the SavePerson() on the service in the WCFTestClient utility.
The SavePerson() implementation is:

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

But I get this error:

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.

if I change the SavePerson method and instead of:

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

I do

catch(Exception)
{
    throw;
}

I don't get the above error, but I only get the exception message and no inner exception.
What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伤痕我心 2024-09-25 03:48:48

当您定义故障契约时:

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

When you define the fault contract:

[FaultContract(typeof(ExecuteCommandException))] 

you must not specify an exception type. Instead, you specify a data contract of your choice to pass back any values that you deem necessary.

For example:

[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'")); 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文