Exception handling Azure服务结构远程处理框架异常处理
我有一个Azure Service Fabric无状态服务,客户端通过ServiceProxy使用远程处理连接到该服务,我正在尝试捕获客户端内服务中引发的异常。在线文档显示: 远程处理框架将服务抛出的异常传播到 客户。因此,通过使用 ServiceProxy可以直接处理服务引发的异常 然而,我似乎得到的只是一个System.Exception,其中包含一些其他错误消息,例如: 类型异常 引发了“ExceptionServiceCommon.Exceptions.MyCustomException” 为了解决更复杂情况下的问题,我正在使用一个认真的回骨骼实现 服务代码为:Exception handling Azure服务结构远程处理框架异常处理,exception-handling,azure-service-fabric,remoting,Exception Handling,Azure Service Fabric,Remoting,我有一个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))
};
}
}
客户端(控制台应用程序)中的代码是:
类程序
{
静态void Main(字符串[]参数)
{
var client=ServiceProxy.Create(新Uri(“结构:/ExceptionApplication/ExceptionService”);
尝试
{
client.ThrowException().GetAwaiter().GetResult();
}
捕获(MyCustomException)
{
Console.WriteLine(“我的自定义异常”);
}
捕获(例外)
{
Console.WriteLine(“普通旧异常”);
}
Console.Read();
}
}
MyCustomException永远不会被捕获,只有异常。您没有在客户端获取异常的原因是它被包装在一个
聚合异常中。只需确保在try/catch中捕获该异常,然后在自定义异常类型中捕获该异常
public void Fail()
{
var faultyService=ServiceProxy.Create(
新Uri(“结构:/ExceptionApplications/ExceptionService”);
尝试
{
faultyService.ThroweException().GetAwaiter().GetResult();
}
捕获(自定义异常cex)
{
//永远不会被击中
}
捕获(聚合异常aex)
{
aex.Handle(ex=>
{
var cex=ex作为客户例外;
如果(cex!=null)
{
//将被您的代码击中
返回true;
}
返回false;
});
}
捕获(例外情况除外)
{
//永远不会被击中
}
}
我还没有玩过这个游戏。。。然而,我认为这是不可能的。调用程序集如何引用远程抛出的异常类型(类)?我希望被证明是错的!
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();
}
}