UseStartup on GenericWebHostBuilder should fail when Startup.ConfigureServices returns an IServiceProvider
See original GitHub issueRight 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.
Issue Analytics
- State:
- Created 5 years ago
- Comments:8 (6 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@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.
Comments on closed issues are not tracked, please open a new issue with the details for your scenario.