Home > Software engineering >  How to add a parameter to a service in Blazor in addition to the dbContext?
How to add a parameter to a service in Blazor in addition to the dbContext?

Time:12-06

VS2022 NetCore 6 EF 6

    builder.Services.AddDbContext<MyAppDbContext>(options removed for simplicity)

    //This is my service registered on Program.cs:
    builder.Services.AddScoped<AccountService>();
    
    //This is the existing class that works as expected:
    public class AccountService
    {
     private readonly MyAppDbContext _context;  
     public AccountService(MyAppDbContext context)
       {
          _context = context;
       }
    }
    //So far so good... 
    
    // Now I need to add another parameter to the service:
    public class AccountService
    {
     private readonly MyAppDbContext _context;  
     public AccountService(MyAppDbContext context, string newParameter)
       {
          _context = context;
          string temp = newParameter;
       }
    }
    
    // But I cannot register; I don't know what to put as first value and if I put MyAppDbContext it gives an error saying it is a type.
    
    builder.Services.AddScoped(ServiceProvider => { return new AccountService(??, newParameter);});

// This works (no compile error) for newParameter but ignores DbContext
    builder.Services.AddScoped(ServiceProvider => { return new AccountService( **null**, newParameter);});

CodePudding user response:

If you've defined:

MyAppDbContext.cs

public class MyAppDbContext : DbContext
    {
        public MyAppDbContext(DbContextOptions<MyAppDbContext> options)
            : base(options)
        {
        }
    }

And a service: AccountService.cs

internal class AccountService
    {
        private MyAppDbContext context;
        public string Myparameter;

        public AccountService(MyAppDbContext context, string Myparameter)
        {
            this.context = context;
            this.Myparameter = Myparameter;
        }
    }

Then in your Startup class you can do something like this:

var contextOptions = new DbContextOptionsBuilder<MyAppDbContext>()
             .UseSqlServer(@"Server=(localdb)\\mssqllocaldb;Database=Test;Trusted_Connection=True;MultipleActiveResultSets=true")
             .Options;

            using var context = new MyAppDbContext(contextOptions);

            services.AddScoped(sp => new AccountService(context, "Myparameter value"));

You can then inject the AccountService into your Index component and use it as demonstrated below:

@page "/"
@inject AccountService _AccountService

<h1>@_AccountService.Myparameter</h1>

Note: You should use DbContext factory in Blazor

CodePudding user response:

You registration will become a little uglier:

builder.Services.AddScoped<AccountService>(x => new AccountService( 
     x.GetRequiredService<MyAppDbContext>(),
     newParameter));

It is important to let the ServiceProvider create the (scoped) DbContext each time you need an AccountService.

  • Related