4

I want to read a connectionstring property from an appsettings.json file added to a .NET CORE class library.

How can I do this?

There exist many solution concerning an asp.net core project.

But this is not what I want!

I need to read a connectionstring from my integrationTests project for the creation of the entity framework context to insert test data.

Pascal
  • 12,265
  • 25
  • 103
  • 195
  • 2
    Could you elaborate on why the solutions you found aren't what you want? – Josh E Feb 26 '18 at 21:22
  • Sure. Those solutions require to use the IConfiguration Type which I can not create manually because its instance is of type IConfiguration and it is injected in each Startup.cs. – Pascal Feb 26 '18 at 22:30
  • And you can't read the configuration in the startup and pass it to the classes in your library? If you use a class for these options, your lib classes can add a `IOptions` parameter to their constructor and get it via DI. – Martin Ullrich Feb 26 '18 at 22:51
  • Well lets just assume I do not use asp.net core at all. Its a .NET Core library with some integration tests where I do not want to mock db stuff. – Pascal Feb 26 '18 at 22:53
  • @Sucker Why minus one downvote, for a very good question! – Pascal Feb 27 '18 at 19:47
  • 1
    Interesting that a similar question has 14 upvotes...: https://stackoverflow.com/questions/45796776/get-connectionstring-from-appsettings-json-instead-of-being-hardcoded-in-net-co Its just about asp.net core... Interesting to see that when you ask questions about mainstream problems you get upvoted. But asking really clever questions then you get slapped. Its just like saying: Do not ask, just work you get paid for! :P – Pascal Feb 27 '18 at 20:31
  • Did you solve it? – Leandro Bardelli Jul 05 '22 at 23:42

0 Answers0