【发布时间】: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