I have a strange occurrence happening in a brand new .net core 2 web app. This is the standard web api from the template built into Visual Studio. VS 2017, all the jazz. Here's the the entire startup.cs:
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using VerySimpleAPI.Helpers;
namespace VerySimpleAPI
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddHostedService<MainLoop>();
}
// 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.UseMvc(routes => {
routes.MapRoute(
name: "Default",
template: "{controller}/{action}/{id?}",
defaults: new { controller = "Home", action = "Index"}
);
});
}
}
}
MainLoop
implements IHostedService
and IDisposable
.
The services.AddHostedService<MainLoop>();
does not resolve, producing the error "'IServiceCollection' does not contain a definition for 'AddHostedService' and no extension method..." all that jazz. I checked out the Microsoft.Extensions.DependencyInjection
source, and I clearly see the definition for public static IServiceCollection AddHostedService<THostedService>(this IServiceCollection services)
Without the hosted service reference, the project compiles just fine. Is there something I'm missing?
AddHostedService
is part ofMicrosoft.Extensions.Hosting.Abstractions
. While it is defined in theMicrosoft.Extensions.DependencyInjection
namespace, it belongs toMicrosoft.Extensions.Hosting.Abstractions
in theServiceCollectionHostedServiceExtensions
classusing Microsoft.Extensions.Hosting;
andusing Microsoft.Extensions.Hosting.Abstractions;
does nothing. I can clearly see the the definition for AddHostedService here: github.com/aspnet/Hosting/blob/master/src/…