4

Why am I getting this error?

InvalidOperationException: Unable to resolve service for type 'System.Configuration.Configuration' while attempting to activate 'HANACONNECT.Controllers.ServisController'.

My code in Program.cs

builder.Services.AddControllersWithViews();
builder.Services.AddDbContext<HANADbContext>();
var app = builder.Build();
var configuration = app.Configuration;

and code in ServisController.cs

namespace HANACONNECT.Controllers
{
    public class ServisController : Controller
    {
        
        private readonly Configuration _configuration;

        private readonly HANADbContext _context;
       
        public ServisController(Configuration configuration)
        {
            this._configuration = configuration;
        }
    }
}
Guru Stron
  • 102,774
  • 10
  • 95
  • 132
wittream
  • 185
  • 11
  • did you try IConfiguration as parameter? – jeb Feb 10 '23 at 11:35
  • @jeb yes but now I am getting another error for this line `System.Configuration.ConfigurationManager.ConnectionStrings["Hana"].ConnectionString` and the error is `System.Configuration.ConnectionStringSettingsCollection.this[string].get, returned null. ` – wittream Feb 10 '23 at 11:55
  • 1
    @wittream this seems a totally different problem. Do not start a debug session on line with Stack Overflow community. If the original problem is solved then mark the answer that solved the problem and ask a new question for a different problem – Steve Feb 10 '23 at 11:59
  • @Steve okay I thought they are related because of `configuration` thanks – wittream Feb 10 '23 at 12:00
  • Please check [the docs](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-7.0) for recommended patterns for working with configuration in ASP.NET Core. – Guru Stron Feb 10 '23 at 12:35

2 Answers2

2

When using DI you're generally always going to be injecting an Interface into your constructor. The DI container will then handle assigning the correct implementation according to your setup in Program.cs

Your code should look like this:

namespace HANACONNECT.Controllers
{
    public class ServisController : Controller
    {
        
        private readonly IConfiguration _configuration;

        private readonly HANADbContext _context;
       
        public ServisController(IConfiguration configuration)
        {
            _configuration = configuration;
        }
    }
}
akseli
  • 1,416
  • 14
  • 22
  • Thank you so much, it worked, but now I am getting another error for this line `System.Configuration.ConfigurationManager.ConnectionStrings["Hana"].ConnectionString` and the error is `System.Configuration.ConnectionStringSettingsCollection.this[string].get, returned null.` are they related? – wittream Feb 10 '23 at 11:54
  • Do you have a `ConnectionString` named `Hana` in your appsettings.json in the `ConnectionStrings` section? – akseli Feb 10 '23 at 11:57
  • yes `"ConnectionStrings": { "Hana": "Server=''; UserName=''; Password='' "` } – wittream Feb 10 '23 at 11:59
  • Can you show the line that you're using to actually get the connection string? In any case, if your original question has been answered, you might want to consider accepting the correct answer and opening a new question with any further issues you run into as these are really different questions altogether. – akseli Feb 10 '23 at 12:00
0

While the other answer covers the problem with injection of configuration into the controller, in the case of connection string and the database context usually you should not do it at all. Usual approach is to set up the connection string during the service registration. Something along this lines:

// add ctor accepting options to the context
public HANADbContext(DbContextOptions<HANADbContext> options) : base(options)
// register context with config
builder.Services.AddDbContext<HANADbContext>(opts => 
   opts.UseSqlServer(builder.Configuration.GetConnectionString("Hana"))); // Or UseNpgsql or any other provider

And then you just inject the context into controller and use it.

Read more:

Guru Stron
  • 102,774
  • 10
  • 95
  • 132