我需要集成一个第 3 方服务。它有 3 个端点用于 3 个呼叫。就是这样,每个 API 调用都有自己的端点和自己的 wsdl。因此,不同调用的 POCO 之间没有任何联系(如继承)。但是这些调用的结果非常相似。特别是每个结果都有 'Errors' 属性,其中包含管道分隔字符串中的错误。一些错误在调用之间共享(具有相同的名称)并且必须以类似的方式处理。
我的目标是在引发异常或错误中出现名为“EXCEPTION”的错误时重试调用。我正在尝试使用 Polly 来实现这个目标。我现在看到的唯一方法是为每个调用创建单独的策略。有没有办法为所有呼叫创建一个单一的策略?
示例代码如下(在实际项目中 *Result 类和 *Service 接口由 VisualStudio 在导入 wsdl 时自动生成):
public partial class CreateResult
{
public string Errors {get;set;}
}
public interface ICreateService
{
Task<CreateResult> CreateAsync();
}
public partial class UpdateResult
{
public string Errors {get;set;}
}
public interface IUpdateService
{
Task<UpdateResult> UpdateAsync();
}
public partial class DeleteResult
{
public string Errors {get;set;}
}
public interface IDeleteService
{
Task<DeleteResult> DeleteAsync();
}
public class Connector
{
private readonly ICreateService _createService;
private readonly IUpdateService _updateService;
private readonly IDeleteService _deleteService;
private readonly Policy _policy = ???;
public Connector(ICreateService createService, IUpdateService updateService, IDeleteService deleteService)
{
_createService = createService;
_updateService = updateService;
_deleteService = deleteService;
}
public async Task<CreateResult> CreateAsync()
{
// sample policy: var policy = Policy.Handle<Exception>()
// .OrResult<CreateResult>(r => r.Errors.Contains("EXCEPTION"))
// .Retry();
// Now I need to create such a policy for every call. How can I create a single policy or a factory method to enforce DRY principle?
return _policy.ExecuteAsync(() => _createService.CreateAsync());
}
public async Task<UpdateAsync> UpdateAsync()
{
return _policy.ExecuteAsync(() => _updateService.UpdateAsync());
}
public async Task<DeleteResult> DeleteAsync()
{
return _policy.ExecuteAsync(() => _deleteService.DeleteAsync());
}
}