I am getting, Cannot convert async lambda expression to delegate type 'Func<string, bool>'. An async lambda expression may return void, Task or Task, none of which are convertible to 'Func<string, bool>'. with below code,
var requestHandler = new SignInRequestHandler(async stringUrl =>
{
if (stringUrl.StartsWith(options.EndUrl))
{
var cookieManager = Cef.GetGlobalCookieManager();
var cookies = await cookieManager.VisitAllCookiesAsync();
if (cookies != null)
{
foreach (var cookie in cookies)
{
if (cookie.Name.ToLower() == options.CookieName.ToLower())
{
var sessionCookie = new System.Net.Cookie(cookie.Name, cookie.Value, "/", cookie.Domain)
{
HttpOnly = true,
Secure = true
};
browserResult = new BrowserResult()
{
SessionCookie = sessionCookie
};
await signWindow.Dispatcher.BeginInvoke(new Action(signWindow.Close));
return Task.FromResult(true);
}
}
}
}
return Task.FromResult(false);
});
private class SignInRequestHandler : RequestHandler, IDisposable
{
private readonly LoginResourceRequestHandler _resourceRequestHandler;
public SignInRequestHandler(Func<string, bool> urlHandler)
: base()
{
_resourceRequestHandler = new LoginResourceRequestHandler(urlHandler);
}
protected override IResourceRequestHandler GetResourceRequestHandler(IWebBrowser chromiumWebBrowser, CefSharp.IBrowser browser, IFrame frame, IRequest request, bool isNavigation, bool isDownload, string requestInitiator, ref bool disableDefaultHandling)
{
return _resourceRequestHandler;
}
}
private class SignInResourceRequestHandler : ResourceRequestHandler
{
private readonly Func<string, bool> _urlHandler;
public SignInResourceRequestHandler(Func<string, bool> urlHandler)
: base()
{
_urlHandler = urlHandler;
}
protected override CefReturnValue OnBeforeResourceLoad(IWebBrowser chromiumWebBrowser, CefSharp.IBrowser browser, IFrame frame, IRequest request, IRequestCallback callback)
{
if (request != null && request.Url != null && _urlHandler(request.Url))
{
return CefReturnValue.Cancel;
}
return base.OnBeforeResourceLoad(chromiumWebBrowser, browser, frame, request, callback);
}
}
I should await this method cookieManager.VisitAllCookiesAsync() and so added async to lambda expression causing this error.
How to solve this please?
This was resolved by using ContinueWith method,
var _ = Task.Run(async () => await _urlHandler(request.Url)).ContinueWith(ContinuationAction);
private async Task<CefReturnValue> ContinuationAction(Task<bool> obj)
{
CefReturnValue? result = null;
if (await obj)
{
result = CefReturnValue.Cancel;
}
return result.Value;
}