Error doing counter via c#

Asked

Viewed 89 times

1

I’m trying to do a pendant counter to my system, only it’s giving error, at the time of calculating, someone could help me?

Error in . Count as you can see in the picture below.

inserir a descrição da imagem aqui

My Controller

public async Task<ActionResult> Index()
        {
            if (!Cookies.Exists("hid"))
                return RedirectToAction("Index", "Login", new { area = "Entrar" });

            var hospitalId = int.Parse(Cookies.GetCookie("hid"));
            var listaDia1 = await Dia1Service.GetPendenciasByUser(hospitalId);
            var listaDia2 = await Dia2Service.GetPendenciasByUser(hospitalId);
            var listaDia3 = await Dia3Service.GetPendenciasByUser(hospitalId);
            var listaDia7 = await Dia7Service.GetPendenciasByUser(hospitalId);
            var lista90Dias = await Dados90DiasService.GetPendenciasByUser(hospitalId);
            var listaAlta = await AltaUTIService.GetPendenciasByUser(hospitalId);
            var listaDemografia = await DemografiaContatosService.GetPendenciasByUser(hospitalId);
            var listaDadosBasais = await DadosBasaisService.GetPendenciasByUser(hospitalId);

            ViewBag.listaD1 = listaDia1;
            ViewBag.listaD2 = listaDia2;
            ViewBag.listaD3 = listaDia3;
            ViewBag.listaD7 = listaDia7;
            ViewBag.lista90d = lista90Dias;
            ViewBag.listaAlta = listaAlta;
            ViewBag.listaDeC = listaDemografia;
            ViewBag.listaDb = listaDadosBasais;
            return View();
        }

My Contact at Pendenciafilter.Cs

void IActionFilter.OnActionExecuted(ActionExecutedContext filterContext)
        {
            AtualizarNotificacoes(filterContext);
        }

        private void AtualizarNotificacoes(ActionExecutedContext filterContext)
        {
            if (string.IsNullOrEmpty(Cookies.GetCookie("uid"))) return;

            var hospitalId = int.Parse(Cookies.GetCookie("hid"));

            // Pegar todas as notificações deste usuario
            var altaUtiPendencias = new AltaUtiService().GetPendenciasByUser(hospitalId).Count();
            var dia1Pendencias = new Dia1Service().GetPendenciasByUser(hospitalId).ToString();
            filterContext.Controller.ViewBag.QtdPendencias = altaUtiPendencias + dia1Pendencias;
        }

1 answer

3

Your repository returns a async Task<IEnumerable<object>>, then you need a await to access the collection (which has the method Count)

`(await new AltaUtiService().GetPendenciasByUser(hospitalId)).Count()`

I really hope this yours AltaUtiService.GetPendenciasByUser is not calling a ToListAsync before returning the collection.

Quando usar Entity Framework com Repository Pattern?

EDIT

you must modify your filter in order for it to run the AtualizarNotificacoes synchronously.:

public override void OnActionExecuted (ActionExecutedContext filterContext)
{
    AtualizarNotificacoes(filterContext).GetAwaiter().GetResult();
}

private async Task AtualizarNotificacoes(ActionExecutedContext filterContext)
{
    if (string.IsNullOrEmpty(Cookies.GetCookie("uid"))) return;

    var hospitalId = int.Parse(Cookies.GetCookie("hid"));

    // Pegar todas as notificações deste usuario
    var altaUtiPendencias = (await new AltaUtiService().GetPendenciasByUser(hospitalId)).Count();
    var dia1Pendencias = (await new Dia1Service().GetPendenciasByUser(hospitalId)).ToString();
    filterContext.Controller.ViewBag.QtdPendencias = altaUtiPendencias + dia1Pendencias;
}
  • And in the void method Iactionfilter.Onactionexecuted(Actionexecutedcontext filterContext) { Updatedscripts(filterContext); } I need to move something?

  • @Leonardomacedo ready

  • Tobias can you take a look at this picture? http://prntscr.com/ix2hgk

  • @Leonardomacedo was my mistake, Action Filters asynchronous are present only in Core. Then you’ll have to make a synchronous call to the asynchronous method.

  • Instead of (Actionexecutedcontext contex) it would be (Actionexecutedcontext filterContext) correct?

  • I made the fix on my code, apparently it worked, only at the time I run my system. It keeps loading... and doesn’t open the system, but there’s no error. I have in my system a Filterconfig.Cs would I have to touch it too in relation to this? public Static void Registerglobalfilters(Globalfiltercollection Filters) { Filters.Add(new Pendenciafilter(); }

  • 1

    It can be a deadlock in the asynchronous call, the ideal would be to call a synchronous version of the query. Not to mention that these services of yours smell like trouble to me.

  • you have some example of how I perform this modification?

Show 3 more comments

Browser other questions tagged

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