Actionresult and async method Using Fastmapper, Typeadpter Error

Asked

Viewed 115 times

0

I have the following problem with Fastmapper. When implementing an Actionresult async when using Typeadapt it cannot perform the asynchronous conversion, would anyone know how to do the conversion with Fastmapper? Remembering, when I do the conversion by hand and or without async the same works normally. Code presenting the problem:

// GET: Credito/Details/5
public async Task<ActionResult> Details(int? id)
{
    if (id == null)
    {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }
    var creditoTabela = db.Creditos.FindAsync(id);

    CreditoViewModel creditoTela = await TypeAdapter.Adapt<CreditoModel, CreditoViewModel> (creditoTabela);


    if (creditoTela == null)
    {
        return HttpNotFound();
    }
    return View(creditoTela);
}

Validation and error image:

inserir a descrição da imagem aqui

Error message:

inserir a descrição da imagem aqui

Without using Fastmapper works normally, my problem is how to use Fastmapper with Async? I have to implement something in the Model part to make it work?

    // GET: Credito/Details/5
    public async Task<ActionResult> Details(int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        var creditoTabela = db.Creditos.Find(id);

        CreditoViewModel creditoTela = new CreditoViewModel() {
            Codigo = creditoTabela.Codigo,
            Descricao = creditoTabela.Descricao,
            Nome = creditoTabela.Nome,
            Imagem = creditoTabela.Imagem
    };


        if (creditoTela == null)
        {
            return HttpNotFound();
        }
        return View(creditoTela);
    }

1 answer

1


For the home page documentation, Adapt is not asynchronous.

Switch to:

// GET: Credito/Details/5
public async Task<ActionResult> Details(int? id)
{
    if (id == null)
    {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }
    var creditoTabela = await db.Creditos.FindAsync(id);

    CreditoViewModel creditoTela = TypeAdapter.Adapt<CreditoModel, CreditoViewModel> (creditoTabela);

    if (creditoTela == null)
    {
        return HttpNotFound();
    }
    return View(creditoTela);
}

You already use an asynchronous method within this Action. The Fastmapper method need not be either.

  • Had thought the same at first but persists and still presents a warning informing that the method will be synchronous even the so-called async.

  • That’s right. I forgot await in var creditoTabela = await db.Creditos.FindAsync(id);. Look now.

  • 1

    Boy was obvious and I didn’t notice, the consultation the base was asyn and had not set the await in it, thanks and thanks.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.