Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MapControllers has no effect? #38672

Closed
mmisztal1980 opened this issue Nov 28, 2021 · 7 comments
Closed

MapControllers has no effect? #38672

mmisztal1980 opened this issue Nov 28, 2021 · 7 comments
Labels
area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions Needs: Attention 👋 This issue needs the attention of a contributor, typically because the OP has provided an update.

Comments

@mmisztal1980
Copy link

mmisztal1980 commented Nov 28, 2021

As a part of a pet-project, I'm working on a mini-framework to rapidly prototype ready to use asp.net core services which:

  • can be natively hosted on kubernetes
  • consist of preconfigured pipelines so that they share behavior and integrate with kubernetes
  • use IHost instead of WebApplication b/c I need more control over my app than the WebApplication provides (such as support for CancellationToken in RunAsync as I like to start/stop my service instances in integration tests

One of the service "modes" is "Api Controller".

In this case the service is expected to be configured in the same way as a minimalistic dotnet template which uses ASP.NET Core Controllers & Open API support. In order for this to happen, the service needs to :

  • register required authorization services via services.AddAuthorization()
  • register required controller services via services.AddControllers()
  • register required middleware via:
        app.UseRouting();
        app.UseAuthorization();
        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
        });

Describe the bug

The service starts properly, however:

  • /swagger endpoint does not indicate any other endpoints are present
  • /weatherforecast endpoint returns a 404

Note

  • I'm not getting this behavior when using minimalistic APIs via .MapGet()
  • I'm not getting this behavior when using HotChocolate nad mimicking the startup sample with GraphQL

To Reproduce

I've managed to create a minimalistic repro project located here which is consistently failing on my Windows and MacOS machines.

  • download dotnet sdk 6.0.100
  • clone the repository
  • build the Repro.ApiControllers.Demo
  • run the Repro.ApiControllers.Demo
  • attempt to access /weatherforecast (404 will be returned)

Exceptions (if any)

None. I suspect it's either an interesting issue or a really stupid bug, however I haven't been able to spot this for the past couple of evenings.

Further technical details

  • ASP.NET Core version: 6 (dotnet sdk 6.0.100)
  • The IDE (VS / VS Code/ VS4Mac) you're running on, and its version: VS 2022 v4.8.04.084
  • Include the output of dotnet --info:
dotnet --info Output
 Maciek@KATANA  ~  dotnet --info
.NET SDK (reflecting any global.json):
 Version:   6.0.100
 Commit:    9e8b04bbff

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.19043
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\6.0.100\

Host (useful for support):
  Version: 6.0.0
  Commit:  4822e3c3aa

.NET SDKs installed:
  3.1.413 [C:\Program Files\dotnet\sdk]
  5.0.400 [C:\Program Files\dotnet\sdk]
  5.0.402 [C:\Program Files\dotnet\sdk]
  6.0.100 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 3.1.19 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.20 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.11 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 6.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 3.1.19 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.20 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.11 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.0-rc.1.21451.13 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.0-rc.2.21480.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 3.1.19 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 3.1.20 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 5.0.9 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 5.0.11 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 6.0.0-rc.1.21451.3 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 6.0.0-rc.2.21501.6 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 6.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
@pranavkm
Copy link
Contributor

Likely a dupe of #7315. #7315 (comment) has a workaround. Could you give it a try and see it addresses your issue?

@pranavkm pranavkm added area-runtime Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. labels Nov 28, 2021
@ghost
Copy link

ghost commented Nov 28, 2021

Hi @mmisztal1980. We have added the "Needs: Author Feedback" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

@mmisztal1980
Copy link
Author

mmisztal1980 commented Nov 28, 2021

@pranavkm I've added a commit to reflect the comment from #7315 and it started working as described.

Given the problem appears to be related to the Startup class's location itself (if my understanding is correct) and the original suggestion was made by @davidfowl I feel maybe it's possible to "pivot" the solution slightly? I fear that the solution might impact running the service from test assemblies negatively.

@davidfowl & @pranavkm is it possible to compose the processing pipeline without the Startup class when using the IHost ? We sure can register the required services via ConfigureServices, but what about the pipeline itself?

  • can it be done?
  • would that solve the problem?
  • if not, do you have any alternative suggestions?

@ghost ghost added Needs: Attention 👋 This issue needs the attention of a contributor, typically because the OP has provided an update. and removed Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. labels Nov 28, 2021
@davidfowl
Copy link
Member

You can use Configure(Action<IApplicationBuilder>) to build the pipeline without a startup

@mmisztal1980
Copy link
Author

mmisztal1980 commented Nov 29, 2021

Hi @davidfowl , thanks for your suggestion. Do you feel this will be a viable workaround?

Edit

I've updated my sample to reflect the latest discussion. It appears that the sample is not working at all at the moment, even when applying the workaround from the original thread.

Any suggestions what can be done here?

private IHost CreateHostBuilder(IConfigurationRoot configuration = null, params string[] args)
    {
        var host = global::Microsoft.Extensions.Hosting.Host.CreateDefaultBuilder(args)
            .UseContentRoot(Directory.GetCurrentDirectory())
            .UseConsoleLifetime()
            .ConfigureAppConfiguration((cfg) =>
            {
                if (configuration != null)
                {
                    cfg.AddConfiguration(configuration);
                }
                else
                {
                    cfg
                        .AddJsonFile("appsettings.json", optional: false)                        
                        .AddEnvironmentVariables()
                        .AddCommandLine(args);
                }
            })
            .ConfigureServices(services => {
                services.AddAuthorization();
                services.AddControllers();
                services.AddSwaggerGen(c =>
                {
                    c.SwaggerDoc("v1", new OpenApiInfo { Title = "net6ihost", Version = "v1" });
                });
                services.AddMiddlewareAnalysis();
            })
            .ConfigureWebHostDefaults(host =>
            {
                host.UseSetting(WebHostDefaults.ApplicationKey, Assembly.GetEntryAssembly().FullName);
                host.Configure(app => 
                {
                    app.UseDeveloperExceptionPage();
                    app.UseSwagger();
                    app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "net6ihost v1"));
                    app.UseRouting();
                    app.UseAuthorization();
                    app.UseEndpoints(endpoints =>
                    {
                        endpoints.MapControllers();
                    });
                });
                //app.UseStartup<Startup>();
                // https://github.com/dotnet/aspnetcore/issues/7315#issuecomment-482458078
                //app.UseSetting(WebHostDefaults.ApplicationKey, Assembly.GetEntryAssembly().FullName);
            })
            .Build();        

        return host;
    }

@davidfowl
Copy link
Member

@mmisztal1980
Copy link
Author

@davidfowl has provided a fix for the Startup-less version of the problem with a brief explanation in the PR above.

Again, thank you for spending time on this.

@ghost ghost locked as resolved and limited conversation to collaborators Dec 30, 2021
@amcasey amcasey added area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions and removed area-runtime labels Aug 24, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions Needs: Attention 👋 This issue needs the attention of a contributor, typically because the OP has provided an update.
Projects
None yet
Development

No branches or pull requests

4 participants