Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IServiceCollection.AddHostedService<>(); does not resolve

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?

like image 389
Nomenator Avatar asked Jul 09 '18 15:07

Nomenator


People also ask

What is IServiceCollection in .NET core?

AddScoped(IServiceCollection, Type, Type) Adds a scoped service of the type specified in serviceType with an implementation of the type specified in implementationType to the specified IServiceCollection.

Is IHostedService a singleton?

When you register implementations of IHostedService using any of the AddHostedService extension methods - the service is registered as a singleton.


1 Answers

AddHostedService is part of Microsoft.Extensions.Hosting.Abstractions.

While it is defined in the Microsoft.Extensions.DependencyInjection namespace, it belongs to Microsoft.Extensions.Hosting.Abstractions in the ServiceCollectionHostedServiceExtensions class

using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting;

namespace Microsoft.Extensions.DependencyInjection
{
    public static class ServiceCollectionHostedServiceExtensions
    {
        /// <summary>
        /// Add an <see cref="IHostedService"/> registration for the given type.
        /// </summary>
        /// <typeparam name="THostedService">An <see cref="IHostedService"/> to register.</typeparam>
        /// <param name="services">The <see cref="IServiceCollection"/> to register with.</param>
        /// <returns>The original <see cref="IServiceCollection"/>.</returns>
        public static IServiceCollection AddHostedService<THostedService>(this IServiceCollection services)
            where THostedService : class, IHostedService
        {
            services.TryAddEnumerable(ServiceDescriptor.Singleton<IHostedService, THostedService>());

            return services;
        }
    }
}

And ensure that the relevant packages are installed and referenced to have access to the extension method

like image 171
Nkosi Avatar answered Oct 29 '22 09:10

Nkosi