0

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?

7
  • How about using Func<string, Task<bool>> instead of Func<string, bool>? Commented Dec 16, 2022 at 8:13
  • @Kisar getting this error "Cannot implicitly convert type 'System.Threading.Tasks.Task<System.Threading.Tasks.Task<bool>>' to 'bool'" from OnBeforeResourceLoad method. Commented Dec 16, 2022 at 8:27
  • You have to await _urlHandler(request.Url) there. Commented Dec 16, 2022 at 8:37
  • Problem is I can't make OnBeforeResourceLoad as async method as it is overridden method of Cef base method Commented Dec 16, 2022 at 8:40
  • In that case you can use _urlHandler(request.Url).Result to block at that point until the result comes back. In that case you'll lose the benefit of asynchronous computing for this instance of the method call, however. Commented Dec 16, 2022 at 8:43

1 Answer 1

-2

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

Comments

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.