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?

Florian
  • 1,019
  • 6
  • 22
Sara
  • 55
  • 5
  • How about using `Func>` instead of `Func`? – Kisar Dec 16 '22 at 08:13
  • @Kisar getting this error "Cannot implicitly convert type 'System.Threading.Tasks.Task>' to 'bool'" from OnBeforeResourceLoad method. – Sara Dec 16 '22 at 08:27
  • You have to `await _urlHandler(request.Url)` there. – Kisar Dec 16 '22 at 08:37
  • Problem is I can't make OnBeforeResourceLoad as async method as it is overridden method of Cef base method – Sara Dec 16 '22 at 08: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. – Kisar Dec 16 '22 at 08:43
  • _"In that case you can use _urlHandler(request.Url).Result to block at that point until the result comes back."_ - Don't. That's a bad idea. _At the very least_ use `GetAwaiter().GetResult()`. – Fildor Dec 16 '22 at 08:48
  • _"Problem is I can't make OnBeforeResourceLoad as async method as it is overridden method of Cef base method"_ - Then maybe your inheritance tree needs a redesign. – Fildor Dec 16 '22 at 08:50

1 Answers1

-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;
        }
Sara
  • 55
  • 5