wcf - WCF : How to handle errors when calling another WCF service?

标签 wcf error-handling

我有一个项目,我应该在其中编写调用另一个WCF服务的WCF服务。它看起来如下:

[ServiceContract]
public interface IAsurService
{
    [OperationContract(ReplyAction = AsurService.ReplyAction_GetCatalogList)]
    Message GetCatalogList();

public Message GetCatalogList()
{
    // The external client service
    GetNsiClient client = new GetNsiClient();

    authContext auth = new authContext
    {
        company = "asur_nsi",
        password = "lapshovva",
        user = "dogm_LapshovVA"
    };

    catalogs catalogs = client.getCatalogList(auth);

在这种情况下如何处理错误?我可以使用这样的标准故障契约(Contract)方法:
[DataContract]
public class AsurDataFaultException
{
    private string reason;

    [DataMember]
    public string Reason
    {
        get { return reason; }
        set { reason = value; }
    }
}

public Message GetCatalogList()
{
    // The external client service
    GetNsiClient client = new GetNsiClient();

    authContext auth = new authContext
    {
        company = "asur_nsi",
        password = "lapshovva",
        user = "dogm_LapshovVA"
    };

    catalogs catalogs = null;
    try
    {
        catalogs = client.getCatalogList(auth);
    }
    catch (Exception exception)
    {
        AsurDataFaultException fault = new AsurDataFaultException();
        fault.Reason = "The error: " + exception.Message.ToString();
        throw new FaultException<AsurDataFaultException>(fault);
    }

或者是其他东西?

先感谢您。

戈兰

最佳答案

如果远程WCF服务的失败导致代码失败,那么显然您应该抛出一个错误,但是该错误不应公开远程WCF服务的内部详细信息或其异常,例如:

catch (Exception exception) 
{
    // Indicate all that you may expose -- that the data is unavailable.
    DataUnavailableFault fault = new DataUnavailableFault();
    throw new FaultException<DataUnavailableFault>(fault); 
} 

当然,您也可以使用重试机制,但是很显然,如果最终导致的是远程服务失败,则应通过抛出适当的错误来向调用方表明这一点。

关于wcf - WCF : How to handle errors when calling another WCF service?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12892648/

相关文章:

asp.net - ASP.NET MVC应用程序中的电子邮件错误

php - 基于异常的表单验证

python - 在Heroku上的python worker进程中使用try语句来处理由于对等错误而导致的连接重置是否理想?

c# - WCF 客户端和服务终结点使用相同的绑定(bind)

c# - 使用 WCF 服务 - 找不到默认端点元素

具有 MSMQIntegrationBinding 的 WCF 不会从队列中获取消息

mysql - 如何参数化Mysql存储过程中的错误

node.js - 何时在NODE中使用错误中间件功能

c# - WCF Windows 身份验证,未通过

javascript - 调用 WCF 方法