gpt4 book ai didi

c# - 异步 lambda 表达式转换为 'Task' 返回委托(delegate)无法返回值

转载 作者:行者123 更新时间:2023-11-30 14:06:40 26 4
gpt4 key购买 nike

我在 ASP.NET Core 应用程序中有以下内容( Startup.csConfigure 方法):

我刚刚添加了 async关键字,因为我需要 await一...所以现在,我得到以下信息:

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

在那种情况下我应该返回什么?我试过 return 0; , 但错误信息没有改变...

附言。 OnTicketRecieved签名

namespace Microsoft.AspNetCore.Authentication
{
public class RemoteAuthenticationEvents : IRemoteAuthenticationEvents
{
public Func<TicketReceivedContext, Task> OnTicketReceived { get; set; }

最佳答案

错误信息实际上是不言自明的:

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

所以你有一个异步 lambda 表达式,它应该返回一个 Task —不是Task<T>对于任何 T .

但是当你做 return 0你返回一个 int,所以你的异步方法的返回类型是 Task<int> , 不是 Task .编译器希望您做的是返回根本没有值

OnTicketReceived = async context =>
{
await Task.Delay(100);
return;
}

关于c# - 异步 lambda 表达式转换为 'Task' 返回委托(delegate)无法返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46125437/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com