-1

I am new to C# and asp.net core and I am trying to create a simple repository pattern and pass a string value but I'm getting the error

An unhandled exception occurred while processing the request. InvalidOperationException: Unable to resolve service for type 'HomepageRepository' while attempting to activate 'WebApplication.Controllers.HomeController'. GetService

This is my code

public static class DBConnection 
{

    public static string  connectDB(this string connectionString) 
    {
        return connectionString = "pass value of this string";
    }
 }

HomePageRepository: I would like to pass the value of connectionString here

public class HomepageRepository : IHomepageRepository
{
    private string connectionString;

    public HomepageRepository()
    {
        connectionString= connectionString.connectDB();
    }

    public string Streams()
    {
        return connectionString;
    }
}

Then pass this to my controller like this

namespace WebApplication.Controllers
{
    public class HomeController : Controller
    {
        public HomepageRepository _repository;

        public HomeController(HomepageRepository repository)
        {
            _repository = repository;
        }

        public string Streams()
        {
            _repository.Streams();

            return "hello";
        }
    }
}

Again I am very new to C# and this code above gives only a runtime error . All I am trying to do is pass the connectionString value in DBConnection to HomePageRepository then pass it into a controller in HomeController. Any suggestions would be great.

Luiso
  • 3,658
  • 2
  • 32
  • 57
Rome Torres
  • 933
  • 2
  • 10
  • 21
  • 1
    Have you registered your service in the Startup class (ConfigureServices()) ? – Kalten Feb 05 '17 at 23:32
  • 1
    I'm afraid your code is a bit of a mess, there's far too much wrong with it for anyone to really help you here. I suggest you should run through some basic tutorials on how to use various technologies you are trying to get to grips with before posting questions here. – DavidG Feb 06 '17 at 00:57

2 Answers2

1

I if you want pass connection string to connectDB method please refere below code sample. but better option is to wrapper DB with Interface and initialized with DI

You don't need DB extension method modify Home Repository as below

public HomepageRepository(string dbconnectionString)
    {
        connectionString = dbconnectionString;
    }

Then add Below entry to Startup Class's ConfigureServices method

 services.AddSingleton<IHomepageRepository>(new HomepageRepository ("dbstring"));
Damith Asanka
  • 861
  • 9
  • 12
0

You have to register the service in the startup class. Here's an example from the MusicStore sample app:

namespace MusicStore
{
    public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            ...
            services.AddSingleton<ISystemClock, SystemClock>();
            ...
        }
    }
}

Full example, here: https://github.com/aspnet/MusicStore/blob/dev/samples/MusicStore/Startup.cs#L80


Edit:

As DavidG pointed out, I missed part of the question.

You can't register plain strings with the out-of-the-box DI container. However, you can register a type with a single method that provides that string. Something like a ConnectionStringProvider

Victor Hurdugaci
  • 27,343
  • 5
  • 83
  • 103
  • 1
    This looks like it's only the first part of the answer (hard to tell when the question is so confusing), but how does this answer the point about passing in the connection string? – DavidG Feb 06 '17 at 00:58