美文网首页.NETC#.Netcore
Polly中调用异步方法的方式

Polly中调用异步方法的方式

作者: Weidaicheng | 来源:发表于2018-07-11 17:53 被阅读15次

最近在看Refit,一个还不错的REST API调用类库(我调用的API也无非是简单的增删改查),因为删除的时候会存在ID不存在的情况,所以就在调用的时候加入了Polly来进行删除的异常处理。

先来看一下后台的API代码
[Produces("application/json")]
[Route("api/Author")]
[ApiExplorerSettings(GroupName = "v1")]
public class AuthorController : Controller
{
    private readonly SwaggerDemoContext _swaggerDemoContext;

    public AuthorController(SwaggerDemoContext swaggerDemoContext)
    {
        _swaggerDemoContext = swaggerDemoContext;
    }

    // GET api/values
    [HttpGet]
    public IEnumerable<Author> Get()
    {
        var authors = _swaggerDemoContext.Authors.ToList();
        return authors;
    }

    // GET api/values/5
    [HttpGet("{id}")]
    public Author Get([FromRoute]int id)
    {
        var author = _swaggerDemoContext.Authors.Find(id);
        return author;
    }

    // POST api/values
    [HttpPost]
    public void Post([FromBody] Author author)
    {
        _swaggerDemoContext.Authors.Add(author);
        _swaggerDemoContext.SaveChanges();
    }

    // PUT api/values/5
    [HttpPut("{id}")]
    public void Put([FromRoute]int id, [FromBody] Author author)
    {
        author.Id = id;
        _swaggerDemoContext.Authors.Update(author);
        _swaggerDemoContext.SaveChanges();
    }

    // DELETE api/values/5
    [HttpDelete("{id}")]
    public void Delete([FromRoute]int id)
    {
        var author = _swaggerDemoContext.Authors.Find(id);
        _swaggerDemoContext.Authors.Remove(author);
        _swaggerDemoContext.SaveChanges();
    }
}
Refti的接口调用接口代码
public interface IAuthorApi
{
    [Get("/api/Author")]
    Task<IEnumerable<Author>> GetAuthors();

    [Get("/api/Author/{id}")]
    Task<Author> GetAuthor(int id);

    [Post("/api/Author")]
    Task PostAuthor([Body]Author author);

    [Put("/api/Author/{id}")]
    Task PutAuthor(int id, [Body]Author author);

    [Delete("/api/Author/{id}")]
    Task DeleteAuthor(int id);
}
在调用接口的时候Polly始终不能正确的抓取并处理异常,Polly处理部分代码如下:
private async static void foo()
{
    var authorApi = RestService.For<IAuthorApi>("http://localhost:8794");

    var delId = 2;
    await Policy
        .Handle<ApiException>()
        .RetryForever(ex =>
        {
          delId++;
        })
        .Execute(() => authorApi.DeleteAuthor(delId));
}

其中想要的是Id不存在(删除异常)时继续删除删除下一个Id,运行时Polly并不能正确的进行处理


unhandled exception.png
解决方法

Polly中调用异步方法要使用ExecuteAsync而不是Execute,同时需要修改处理方法为Async,代码修改如下:

await Policy
    .Handle<ApiException>()
    .RetryForeverAsync((ex, count) =>
    {
        delId++;
    })
    .ExecuteAsync(() => authorApi.DeleteAuthor(delId));

可以正常运行


success.png
后记

Polly对异步方法的支持点击查看
WebApi项目地址
调用客户端项目地址

相关文章

  • Polly中调用异步方法的方式

    最近在看Refit,一个还不错的REST API调用类库(我调用的API也无非是简单的增删改查),因为删除的时候会...

  • 实现异步转同步

    极客时间-《Java并发编程实战》学习笔记 异步方法:调用方法,在方法中启动子线程异步调用:启动子线程调用方法异步...

  • 基于 Spring 的异步调用框架

    Spring 异步调用框架提供了一套异步调用简化方式,只需要写几个注解就能完成调用方法从同步到异步的转化。 使用 ...

  • 2018-03-13 Spring中的异步调用

    异步方法调用 异步方法调用或异步方法模式是(多线程)面向对象程序设计中用于异步调用对象的潜在的长期运行方法的一种设...

  • dubbo笔记-remoting(6)收发请求

    1. 三种发送请求 在DubboInvoker的doInvoke中调用 三种调用方式 同步 异步返回 异步不返回 ...

  • 二十五、OkHttp原理分析(一)

    一、使用方式 OkHttp的使用分为了同步请求和异步请求,分别通过调用execute和enqueue方法,在异步的...

  • 并发编程的一些概念

    同步:同步方法调用一旦开始,调用者必须等到方法调用返回,才能继续后续的工作。 异步:异步方法就不需要等到方法调用返...

  • 异步调用如何使用最好?

    一、异步调用方式分析 今天在写代码的时候,想要调用异步的操作,这里我是用的java8的流式异步调用,但是使用过程中...

  • java 异步调用方法

    异步方法 其他方法要使用异步时,直接调用fun()方法即可

  • Netty-基础

    基础概念 同步和异步 描述的方法跟调用者间通信的方式,如果不需要调用者主动等待,调用者调用后立即返回,然后方法本身...

网友评论

    本文标题:Polly中调用异步方法的方式

    本文链接:https://www.haomeiwen.com/subject/jczhpftx.html