Access to database context from server code

Hi, I`m using hangfire (hangfire.io) as a middleware to schedule some jobs in Radzen apps. Tasks can be launched (fire and forget) to, for example, process an uploaded file or in a recurrent way in certain time period to check an email inbox or another resource. The tasks are coded in methods of a static class and can be launched from Startup.Custom.cs and/or a Custom Server Method. I'm used both with success. My question is about database context. I want to use entity framework access to read and write from my hangfire static class and I'm trying to instance a db context but it require an HttpAccessor, I don't have it in my static class because hangfire methods will be triggered by a worker process independent of UI. How I could have a working instance of db context without the HttpAccesor?

Thanks in advance for your support.

Regards

How do you use the class in Startup.Custom.cs? Perhaps you can just set either the HttpAccessor or the context itself from there something like:

myCustomClass.HttpAccessor = httpAccessor;

In Startup.Custom.cs:

partial void OnConfigureServices(IServiceCollection services)
{
services.AddHangfire(configuration =>{
configuration.UseSqlServerStorage(Configuration.GetConnectionString("KPIsConnection"));
});
}

partial void OnConfigure(IApplicationBuilder app)
{
app.UseHangfireServer();

        using (var connection = JobStorage.Current.GetConnection())
        {
            foreach (var recurringJob in StorageConnectionExtensions.GetRecurringJobs(connection))
            {
                RecurringJob.RemoveIfExists(recurringJob.Id);
            }
        }
        
        // Fire and Forget
        BackgroundJob.Enqueue(() => JobLeerDatos.MarcarProyecciones(Configuration.GetConnectionString("KPIsConnection")));
        
        // --

        // Recurrent
        
        //RecurringJob.AddOrUpdate(() => JobLeerDatos.Proceso(Configuration.GetConnectionString("KPIsConnection")), "*/5 * * * *");
        // --
    }

If I could get httpAccessor instance I could expose it like a static property or similar in this class?

Well if you already have access to the connection string you can just instantiate the context.

You can get the IHttpAccessor instance from the IServiceCollection itself:

var httpAccessor = services.BuildServiceProvider().GetService(IHttpContextAccessor) as IHttpContextAccessor;

Something missing?

It is IHttpContextAccessor. Sorry.

I'm using:

public static IHttpContextAccessor httpAccessor
{
get; set;

    }
    partial void OnConfigureServices(IServiceCollection services)
    {
        services.AddHangfire(configuration =>{
            configuration.UseSqlServerStorage(Configuration.GetConnectionString("KPIsConnection"));
        });

        httpAccessor = services.BuildServiceProvider().GetService<IHttpContextAccessor>() as IHttpContextAccessor;
    }

however, I get null value in httpAccessor... what's wrong?

According to this you first have to invoke AddHttpContextAccessor:

services.AddHttpContextAccessor();

Trying to add it but is a method not available. Link is about asp.net core 2.2 and when this radzen app was created I used .NET Core 2.0. Must I recreate server side code?

Well that explains it - HttpContextAccessor was added after 2.0. You better recreate the server-side code - try changing the "version" setting in the app.json file to 2.2. You will have to install .NET Core 2.2 as well.