Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Async lambda expression converted to a 'Task' returning delegate cannot return a value

I have the following in an ASP.NET Core application (Startup.cs, Configure method):

I just added the async keyword, because I needed the await one... So now, I am getting the following:

Error CS8031 Async lambda expression converted to a 'Task' returning delegate cannot return a value. Did you intend to return 'Task<T>'?

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, ITableRepositories repository)
{
    // ...  
    app.UseStaticFiles();    
    app.UseCookieAuthentication();

    app.UseOpenIdConnectAuthentication(new OpenIdConnectOptions
    {
        ClientId = Configuration["..."],
        Authority = Configuration["..."],
        CallbackPath = Configuration["..."],
        Events = new OpenIdConnectEvents
        {
            OnAuthenticationFailed = context => { return Task.FromResult(0); },
            OnRemoteSignOut = context => { return Task.FromResult(0); },
            OnTicketReceived = async context =>
            {
                var user = (ClaimsIdentity)context.Ticket.Principal.Identity;
                if (user.IsAuthenticated)
                {
                    var firstName = user.FindFirst(ClaimTypes.GivenName).Value;
                    // ...
                    List<Connection> myList = new List<Connection>() { c };
                    var results = await repository.InsertOrMergeAsync(myList);
                    var myConnection = (results.First().Result as Connection);                         
                }
                return Task.FromResult(0); // <<< ERROR HERE ....... !!!
            },
            OnTokenValidated = context => { return Task.FromResult(0); },
            OnUserInformationReceived = context => { return Task.FromResult(0); },
        }
    });

    app.UseMvc(routes =>        { ...        });
}

enter image description here

What should I return in that case? I tried to return 0;, but the error message doesn't change...

PS. The OnTicketRecieved signature

namespace Microsoft.AspNetCore.Authentication
{
    public class RemoteAuthenticationEvents : IRemoteAuthenticationEvents
    {
        public Func<TicketReceivedContext, Task> OnTicketReceived { get; set; }
like image 623
serge Avatar asked Sep 08 '17 22:09

serge


1 Answers

The error message is actually self-explanatory:

Async lambda expression converted to a 'Task' returning delegate cannot return a value. Did you intend to return 'Task<T>'?

So you have an async lambda expression which is supposed to return a Task—not a Task<T> for any T.

But when you do return 0 you are returning an int, so the return type of your async method is Task<int>, not Task. What the compiler wants you to do there is to return no value at all.

OnTicketReceived = async context =>
{
    await Task.Delay(100);
    return;
}
like image 158
poke Avatar answered Sep 18 '22 16:09

poke