【问题标题】:Azure Service Fabric remoting framework exception handlingAzure Service Fabric 远程处理框架异常处理
【发布时间】:2017-09-18 07:23:12
【问题描述】:

我有一个 Azure Service Fabric 无状态服务,客户端使用远程服务通过 ServiceProxy 连接到该服务,我正在尝试捕获客户端内的服务中引发的异常。在线文档说:

远程处理框架将服务抛出的异常传播到 客户端。所以客户端的异常处理逻辑通过使用 ServiceProxy 可以直接处理服务抛出的异常。

但是,我似乎得到的只是一个 System.Exception,其中包含一些其他错误消息,例如:

类型异常 'ExceptionServiceCommon.Exceptions.MyCustomException' 被抛出。

为了解决更复杂情况下的问题,我正在使用一个非常简单的实现。

服务代码是:

internal sealed class ExceptionService : StatelessService, IException
{
    public ExceptionService(StatelessServiceContext context)
        : base(context)
    { }

    public Task ThrowException()
    {
        throw new MyCustomException();
    }

    protected override IEnumerable<ServiceInstanceListener> CreateServiceInstanceListeners()
    {
        return new[]
        {
            new ServiceInstanceListener(initParams => this.CreateServiceRemotingListener(initParams))
        };
    }
}

通用程序集中定义的接口和异常有:

public interface IException : IService
{
    Task ThrowException();
}

[Serializable]
public class MyCustomException : Exception
{
    public MyCustomException()
    { }

    public MyCustomException(string message)
        : base(message) { }

    public MyCustomException(string message, Exception innerException)
        : base(message, innerException)
    { }

    protected MyCustomException(SerializationInfo info, StreamingContext context) 
        : base(info, context)
    {
    }
}

客户端(控制台应用程序)中的代码是:

class Program
{
    static void Main(string[] args)
    {
        var client = ServiceProxy.Create<IException>(new Uri("fabric:/ExceptionApplication/ExceptionService"));

        try
        {
            client.ThrowException().GetAwaiter().GetResult();

        }
        catch (MyCustomException)
        {
            Console.WriteLine("My Custom Exception");
        }
        catch (Exception)
        {
            Console.WriteLine("Plain old Exception");
        }

        Console.Read();
    }
}

MyCustomException 永远不会被捕获,只有 Exception。

【问题讨论】:

  • 我还没有玩过这个...但是我不认为它可以。您的,呃,调用程序集如何引用远程中抛出的异常类型(类)?我很想被证明是错的!

标签: exception-handling azure-service-fabric remoting


【解决方案1】:

您没有在客户端获取异常的原因是它包含在AggregateException 中。只需确保您在 try/catch 中捕获它,然后 handle 您的自定义异常类型。

    public void Fail()
    {
        var faultyService = ServiceProxy.Create<IException>(
            new Uri("fabric:/ExceptionApplication/ExceptionService"));

        try
        {
            faultyService.ThrowException().GetAwaiter().GetResult();
        }
        catch (CustomException cex)
        {
            // Will never be hit                
        }
        catch (AggregateException aex)
        {
            aex.Handle(ex =>
            {
                var cex = ex as CustomException;
                if (cex != null)
                {
                    // Will be hit by your code
                    return true;
                }
                return false;
            });
        }
        catch (Exception ex)
        {
            // Will never be hit
        }
    }

【讨论】:

    猜你喜欢
    • 2016-09-08
    • 2021-02-15
    • 2018-08-17
    • 2017-07-26
    • 2018-09-24
    • 2017-10-18
    • 1970-01-01
    • 2019-04-05
    • 2017-07-10
    相关资源
    最近更新 更多