Async crashes when asking entity framework query

3

I have an aspx project in the use of the repository, generic, and I am starting to use the async methods that the entity framework offers to interact with the database, when trying to save a record, it always hangs up and does not advance the code.

From this method that is in the codebehind of the aspx I make the request to save the database to my business layer.

    [WebMethod]
    public async static Task<string> Guardar(TallerEntity item)
    {
        var classInstance = new Talleres();

        var message = string.Empty;

        try
        {
            await classInstance.GuardarAsync(item);
        }
        catch (TallerException ex)
        {
            message = ex.Message;
        }
        catch (Exception ex)
        {
            message = "Ocurrió un error, intente de nuevo";
        }

        return JsonConvert.SerializeObject(new { Message = message });
    }

This method is that it is called and it tries to do the saving of the record, first it inquires if the record exists, if it does not exist, I believe it if not, it updates it.

   public async Task<TallerEntity> GuardarAsync(TallerEntity item)
   {
        if (item.Nombre.Length == 0)
        {
            throw (new TallerException("Falta capturar el nombre"));
        }

        using (var r = new Repository<Taller>())
        {
            var saved = await r.RetrieveAsync(x => x.Codigo == item.Codigo);

            var isNew = saved == null;

            if (isNew)
            {
                saved = new Taller();
            }

            saved.Nombre = item.Nombre;

            if (isNew)
            {
                saved = await r.CreateAsync(saved);
                item.Codigo = saved.Codigo;
            }
            else
            {
                saved = await r.UpdateAsync(saved, x => x.First(y => y.Codigo == saved.Codigo));
            }
        }

        return item;
    }

And in the RetrieveAsync method it crashes when it reaches the FirstOrDefaultAsync line, I've already tried setting the ConfigureAwait (false) method and it's still hanging.

    public async Task<TEntity> RetrieveAsync(Expression<Func<TEntity, bool>> where)
    {
        TEntity result;

        try
        {
            result = await EntitySet.Where(where).FirstOrDefaultAsync();
        }
        catch
        {
            throw;
        }

        return result;
    }

If in the Save method I remove the async from Retrieve Async, it goes to Create or Update and if it affects the database, but when it arrives at the return it hangs up as well.

    public async Task<TEntity> CreateAsync(TEntity toCreate)
    {
        TEntity result = null;
        try
        {
            EntitySet.Add(toCreate);
            await context.SaveChangesAsync().ConfigureAwait(false);
            result = toCreate;
        }
        catch
        {
            throw;
        }

        return result;
    }

Anyone who has any idea why he hangs up?

Greetings

    
asked by elchente23 18.05.2016 в 02:27
source

1 answer

1

You are putting an async on the server side, it is not necessary since you can receive several simultaneous requests in the same way, that is, it does not affect anything

And to implement it, you automatically create the async function, when you instantiate it, first add the reference, then from code instantiate

WService.InstantLinkSoapClient ws = new WService.InstantLinkSoapClient();

Later when you want to use some method you are going to have already created you will have

ws.MiMetodo
ws.MiMetodoAsync
    
answered by 16.09.2016 в 17:55