2

Cannot convert lambda to intended delegate because some of the return types in the block are not implicitly converted to the delegate return type.

without DI

var chromeDriverService = ChromeDriverService.CreateDefaultService();
var chromeOptions = new ChromeOptions();
chromeOptions.AddArguments(new List<string>() { "headless" });

ChromeDriver driver = new ChromeDriver(chromeDriverService, chromeOptions);

with DI in Startup.cs

 services.AddScoped<ChromeDriverService>((serviceProvider =>
 {
   return  ChromeDriverService.CreateDefaultService();
 }));
 //**** errors here*****
 services.AddScoped<ChromeOptions>((serviceProvider =>
 { return new ChromeOptions().AddArguments(new List<string>() { "headless" }); }));
 // errors here******
 // how would i pass the driver service & options 
 services.AddScoped<ChromeDriver>(
 (serviceProvider =>
 {
   return new ChromeDriver(chromeDriverService,chromeOptions);
 }));

how do i make it so it's convertible and pass the correct options to the chromeDriver ?

3
  • What does .AddArguments(...); return? Is it an instance of ChromeOptions? Commented Jul 25, 2018 at 4:52
  • Do you have any services that depends on Chrome Options and ChromeDiverService apart from ChromeDriver? if so, you may want to consider registering only ChromeDriver Commented Jul 25, 2018 at 4:57
  • yeah see it news up a chromeoptions as well Commented Jul 25, 2018 at 5:17

1 Answer 1

4

You have an issue within this line:

services.AddScoped<ChromeOptions>((serviceProvider =>
{ return new ChromeOptions().AddArguments(new List<string>() { "headless" }); }));

AddScoped input delegate is excepted to return ChromeOptions while .AddArguments returns void

How about:

services.AddScoped<ChromeOptions>((serviceProvider =>
{
    var chromeOptions = new ChromeOptions();
    chromeOptions.AddArguments(new List<string>() {"headless"});
    return chromeOptions; // Return expected type
});

services.AddScoped<ChromeDriver>((s => 
{
     return new ChromeDriver(s.GetService<ChromeDriverService>(), 
                             s.GetService<ChromeOptions>());
}));
Sign up to request clarification or add additional context in comments.

2 Comments

That does work :) thx, but I how to pass them to services.AddScoped<ChromeDriver> ?
I got it services.AddScoped<ChromeDriver>( (s => { return new ChromeDriver(s.GetService<ChromeDriverService>(), s.GetService<ChromeOptions>()); })); , please update your answer, your also missing a ) at the end.

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.