1

I am using hangfire in my ASP.net core application to run some background task. I am using IIS and i have configured Hangfire as follows:

using Hangfire;

namespace Whm.APIPortal
{
    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.AddHangfire(option =>
            {
                option.UseSqlServerStorage(Configuration.GetConnectionString("HangFireConnection"));
            });
            services.AddHangfireServer();
         }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });

            app.UseHangfireDashboard();

            app.UseHangfireServer();
         }
     }
}

Below is my appsettings.json file

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "ConnectionStrings": {
    "DefaultConnection": "Server=.\\SQLEXPRESS;Database=Whm;User Id=sa;Password=***;MultipleActiveResultSets=true",
    "HangFireConnection": "Server=.\\SQLEXPRESS;Database=WhmHangFire;User Id=sa;Password=***;"
  },
  "AllowedHosts": "*"
}

Whenever I try to launch the application I get below exception

Unhandled exception. System.ArgumentNullException: Value cannot be null. (Parameter 'nameOrConnectionString')

I have created the database "WhmHangFire" on my sql server.

Still I get same exception. could somebody explain what is causing this issue?

Thanks

Sheheryar Sajid
  • 395
  • 2
  • 5
  • 15
  • The code is expecting the connection string to be called : nameOrConnectionString – jdweng Apr 27 '21 at 08:30
  • what should I do? what changes do i need to make it work? – Sheheryar Sajid Apr 27 '21 at 08:48
  • Check Hangfire documentation and find out the connection string requirements. Someplace in code the connection string name is wrong. Not sure where the variable 'nameOrConnectionString' is coming from. What is the name of your SQL database? Can you connect using SQL Server Management Studio? On SSMS login window what is the Server Name? Do login show Window Credential or are you using Username and Pasword? – jdweng Apr 27 '21 at 10:38
  • The error message is obvious, but I don’t see nameOrConnectionString in your code. can you show me it? – samwu Apr 28 '21 at 03:24
  • that's the thing hangfire docs does not say anything about nameOrConnectionString. – Sheheryar Sajid Apr 28 '21 at 10:21
  • The ArgumentNullException exception is thrown at run time in the following two major circumstances, both of which reflect developer error: an uninstantiated object is passed to a method. To prevent the error, instantiate the object. and an object returned from a method call is then passed as an argument to a second method, but the value of the original returned object is null. To prevent the error, check for a return value that is null and call the second method only if the return value is not null. so you can check it based on these. – samwu Apr 29 '21 at 09:05

0 Answers0