roslyn icon indicating copy to clipboard operation
roslyn copied to clipboard

IntelliSense confused by Action<> overloads

Open datvm opened this issue 3 years ago • 7 comments

Description

I was trying to create a Windows Service in .NET 6 following this documentation.

I want to read the settings from appsettings.json so I added the following code:

IHost host = Host.CreateDefaultBuilder(args)
    .UseWindowsService(options =>
    {
        options.ServiceName = "My Service";
    })
    .ConfigureServices(services =>
    {
        var settings = new ScriptOptions(); // ScriptOptions is just a POCO class
        services.Configuration.Bind(settings);

        services.AddHostedService<WindowsBackgroundService>();
    })
    .Build();

As you can see, IntelliSense seems to recognize there is Configuration property in services (instance of IServiceCollection instead of HostBuilderContext).

image

However the code wouldn't compile because IServiceCollection does NOT have Configuration property, but HostBuilderContext does.

I realized ConfigureServices method has 2 overload (actually there is one, the other is extension method):

// Default Method
IHostBuilder ConfigureServices(Action<HostBuilderContext, IServiceCollection> configureDelegate);

// Extension Method
public static IHostBuilder ConfigureServices(this IHostBuilder hostBuilder, Action<IServiceCollection> configureDelegate)

So in the case of using Extension Method, somehow IntelliSense thought services is HostBuilderContext.

Note: I already fixed my problem, just want to report strange behavior of IntelliSense that made me spend a lot of time figuring out why my code couldn't compile. Here's the fix if anyone is interested:

    // Add ctx parameter
    .ConfigureServices((ctx, services) =>
    {
        var settings = new ScriptOptions();
        ctx.Configuration.Bind(settings);

        services.AddHostedService<WindowsBackgroundService>();
    })

Configuration

  • Which version of .NET is the code running on? 6.0
  • What OS and version, and for Linux, what distro? Windows 11 Pro
  • What is the architecture (x64, x86, ARM, ARM64)? x64
  • Do you know whether it is specific to that configuration? I am not sure, probably not
  • If you're using Blazor, which web browser(s) do you see this issue in? None

Regression?

I don't know

Other information

Microsoft Visual Studio Community 2022 Version 17.0.0 VisualStudio.17.Release/17.0.0+31903.59 Microsoft .NET Framework Version 4.8.04161

Installed Version: Community

datvm avatar Nov 15 '21 08:11 datvm

cc @mairaw

eiriktsarpalis avatar Nov 15 '21 11:11 eiriktsarpalis

This is not related to the tutorials but instead it's an IntelliSense issue. @jaredpar @terrajobst would you know who can help with this?

mairaw avatar Nov 16 '21 05:11 mairaw

@datvm would it be possible to share a repo with instructions on how to reproduce the issue? We can try to take a look.

eiriktsarpalis avatar Nov 16 '21 12:11 eiriktsarpalis

@eiriktsarpalis Hi, just create a new project with Worker template and you have the problem immediately in Program.cs file:

image

Github Repo if you still need it.

It seems to list all members of HostBuilderContext, for example, HostingEnvironment is listed as well though it should not:

image

datvm avatar Nov 17 '21 03:11 datvm

Hi is there any update on this issue? It's still happening today (VS 2022 17.2.3)

image

datvm avatar Jun 08 '22 20:06 datvm

Since this is happening in VS, you might be able to get better support if you file the issue through Visual Studio Help menu.

mairaw avatar Jun 09 '22 23:06 mairaw

Related VS feedback ticket https://devdiv.visualstudio.com/DevDiv/_workitems/edit/1590331

genlu avatar Aug 11 '22 19:08 genlu

I can confirm this is no longer an issue testing at v17.9.2. Do you want to close this?

datvm avatar Mar 06 '24 16:03 datvm

@datvm In general, the behavior described above is by design. In the face of incomplete code, the compiler may or may not be able to complete its overload resolution process down to a single set of types. When this step fails to produce a unique result, completion lists will attempt to include the union of possible results to best allow the user to bring the code from an incomplete state to a valid complete state.

sharwell avatar Mar 06 '24 21:03 sharwell