8

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; }
4
  • 3
    Please DO NOT delete your question because it was closed and recreate a new identical question to get rid of the close votes and the comments. – Also, I was just about to answer your original question actually when you deleted it… Commented Sep 8, 2017 at 23:01
  • What delegate type is OnTicketReceived expecting? Commented Sep 8, 2017 at 23:01
  • @MikeStrobel I updated the OP to explain it Commented Sep 8, 2017 at 23:10
  • 3
    It sounds like OnTicketReceived wants a delegate that returns a Task, i.e., a task with no result. I'd guess when you added async, your lambda's implicit return type became async Task. That means you can't return a value, so treat your lambda as if it's returning void. Just remove your return statement entirely. Commented Sep 8, 2017 at 23:15

1 Answer 1

14

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;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Because of the SO editor the Task<T> text transformed in Task, so my quote were wrong. Fixed. And please don't donvote me and ask for code if you don't need it
So how do you return results in this case>? Such as a http status code and message?
@DanielWilliams The event handlers are supposed to interact with the passed context if they want to control the authentication flow. If you want to terminate the HTTP request, you can write directly to the response of the HttpContext, though I would recommend doing that inside your authentication events since that will very likely interrupt the authentication process in a non-recoverable manner.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.