0

I am trying to start Hangfire server in .Net Worker Service using example: Configure Hangfire dashboard in a worker service in .Net 5? answer by Ruslan Gilmutdinov https://stackoverflow.com/users/1734655/ruslan-gilmutdinov but following code

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

namespace WorkerServiceHangfire
{
    public class Program
        {
            public static void Main(string[] args)
            {
                CreateHostBuilder(args).Build().Run();
            }
    
            public static IHostBuilder CreateHostBuilder(string[] args) =>
                Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(builder =>
                {
                    builder.Configure(app =>
                    {
                        app.UseRouting();
    
                        app.UseHangfireDashboard();
                        app.UseEndpoints(endpoints =>
                        {
                            endpoints.MapHangfireDashboard();
                        });
                    });
                })
                    .ConfigureServices((hostContext, services) =>
                    {
                        services.AddHangfire(conf => conf.UseSqlServerStorage("connection string"));
                        services.AddHangfireServer();
    
                        services.AddHostedService<Worker>();
                    });
        }
}

gives error at builder.Configure(app => line:

Error CS0411 The type arguments for method 'OptionsConfigurationServiceCollectionExtensions.Configure(IServiceCollection, IConfiguration)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

Any suggestions?

1 Answers1

3

You have to include the namespace Microsoft.AspNetCore.Hosting wich contains WebHostBuilderExtensions an extension class that among other extension methods contains also the one you are trying to use:
public static IWebHostBuilder Configure(this IWebHostBuilder hostBuilder, Action<IApplicationBuilder> configureApp)

using Microsoft.AspNetCore.Hosting

LMio
  • 126
  • 1
  • 9
  • using Microsoft.AspNetCore.Hosting is included. My usings: using Hangfire; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; – Vasil Ivanishvili Jul 01 '22 at 11:51
  • Just for test can you try to edit the .csproj and add: – LMio Jul 01 '22 at 11:58
  • Done, but same error :( – Vasil Ivanishvili Jul 01 '22 at 12:45
  • 1
    I have seen that you have updated the question with all the includes but `using Microsoft.AspNetCore.Hosting` is missing, is possible that you have confused it with `using Microsoft.Extensions.Hosting;`? you need them both – LMio Jul 01 '22 at 14:03
  • Now it works !!! Thank you so much!!! Mixed up Microsoft.AspNetCore.Hosting and Microsoft.Extensions.Hosting – Vasil Ivanishvili Jul 01 '22 at 14:48