question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

UseStartup on GenericWebHostBuilder should fail when Startup.ConfigureServices returns an IServiceProvider

See original GitHub issue

Right now it’s being ignored and it should throw instead.

Program.cs

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace WebApplication44
{
    public class Program
    {
        public static void Main(string[] args)
        {
            CreateWebHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateWebHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(builder =>
                {
                    builder.UseStartup<Startup>();
                });
    }
}

Startup.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;

namespace WebApplication44
{
    public class Startup
    {
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            return services.BuildServiceProvider();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.Run(async (context) =>
            {
                await context.Response.WriteAsync("Hello World!");
            });
        }
    }
}

Here’s the code that supposed to do that check.

https://github.com/aspnet/AspNetCore/blob/cc899e2be729364589ba7f4392bce9942e4faca4/src/Hosting/Hosting/src/GenericHost/GenericWebHostBuilder.cs#L245-L249

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
davidfowlcommented, Dec 24, 2018

@HassanHashemi Returning an IServiceProvider from Startup.ConfigureServices does not compose with other calls to ConfigureServices. That pattern is the reason it was so difficult to implement allowing test services to override services added in Startup. It’s also the reason why ConfigureContainer only works if you don’t directly return the IServiceProvider from the ConfigureSevices.

0reactions
Tratchercommented, Mar 20, 2019

Comments on closed issues are not tracked, please open a new issue with the details for your scenario.

Read more comments on GitHub >

github_iconTop Results From Across the Web

'ConfigureServices returning a System.IServiceProvider isn ...
It show me this error: 'ConfigureServices returning an System.IServiceProvider isn't supported.' ... But it not solved. ... How can I solve this ...
Read more >
Avoiding Startup service injection in ASP.NET Core 3
In this post I describe the changes to dependency injection in your Startup class when upgrading an ASP.NET Core 2.x app to .NET...
Read more >
ASP.NET Core Introduction
The startup class can be configured using WebHostBuilderExtensions.UseStartup<TStartup> method on the host builder as shown below. public class ...
Read more >
ASP.NET Core Anatomy - How does UseStartup work?
If our Startup. ConfigureServices method returned an IServiceProvider directly, this then gets returned immediately.
Read more >
Code samples migrated to the new minimal hosting model ...
Learn how to migrate ASP.NET Core samples to the new minimal hosting model in 6.0.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found