0

I have a solution containing many projects.

One of the projects is a data access layer Class Library. It uses this line for the connection string which it gets from the web.config of any other project that references it:

private string Constr = ConfigurationManager.ConnectionStrings["DefaultConn"].ConnectionString;

I've added a web.config to a .net core 2 MVC app, but I get this error:

FileNotFoundException: Could not load file or assembly 'System.Configuration.ConfigurationManager, Version=0.0.0.0, Culture=neutral, PublicKeyToken=123456'. The system cannot find the file specified.

I've also tried adding the connection string to the appsettings.json file - but it also doesn't help; the full contents of appsettings.json:

{
  "ConnectionStrings": {
    "DefaultConn": "Server=.;Database=MyDatabase;Trusted_Connection=true;"
  },

  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Warning"
    }
  }
}

I've found a few articles online dancing around it - but none that can fix this.

How to read web.config file in .Net Core app This one mentions a similar situation and a nuget package - but it doesn't solve the issue for the user.

How to include reference to assembly in ASP.NET Core project This one touches on it but applies to Hangfire only, not a Class Library using ConfigurationManager

How can I do this? Thanks.

niico
  • 11,206
  • 23
  • 78
  • 161
  • In your asp.net core project, You should inject `IConfiguration` and then you can read the connection string. Read this https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-2.0&tabs=basicconfiguration – Shyju May 24 '18 at 02:00
  • Thanks - I'm not quite clear. So in the core project I still need the web.config with the connectionstring section? The link is long and mentions IConfiguration a lot - an answer with the specific code required would be appreciated - thanks. – niico May 24 '18 at 02:08
  • No. You keep the connection string in the appsetttings json file – Shyju May 24 '18 at 02:13
  • Ahh OK - I have already done that too. Can you point me at the specific code to add? – niico May 24 '18 at 02:14
  • Check that page i linked. Check the sample line `Configuration["MyConfig"];`. Replace that with connection string key( Ex : `Configuration["ConnectionStrings:DefaultConn"];`) – Shyju May 24 '18 at 02:16
  • sorry I'm still not sure exactly what you're telling me to do. var myConfig = Configuration["ConnectionStrings:DefaultConn"];? – niico May 24 '18 at 02:23
  • unexplained driveby downvote - nice. – niico May 24 '18 at 02:33
  • Yes. Why don't you try it ? – Shyju May 24 '18 at 04:16
  • Doesn't work - is 'var myConfig' correct? Where did that come from? Feel like I'm missing something here. – niico May 24 '18 at 08:13
  • If you do not know what `var myConfig` means, i strongly recommend you to learn basics of C# and asp.net mvc before trying to build anything. – Shyju May 24 '18 at 16:23
  • lol you misunderstand me. I know what it means - I don't know why declaring a MyConfig variable helps here - perhaps I haven't explained my problem well? – niico May 24 '18 at 16:27
  • I have a Class Library with data access inside that needs a connection string. If I reference it from an MVC 5 app - then add the connection string to the web.config of the MVC 5 app - it gets it fine. How can I do the same from a .net core 2 MVC app? How does declaring var MyConfig help pass that connection string into the Class Library? – niico May 24 '18 at 16:29
  • I've been developing in MVC 5 for a few years - I understand it. – niico May 24 '18 at 16:29
  • dot net core uses a different mechanism for reading the config values. The injectable `IConfiguration` provides access to the config files(like appsettings.json). I used the same page i liked above to learn about this. Here is a sample project where i am using it. Hopefully that helps https://github.com/kshyju/ProjectPlanningTool/blob/master/src/TeamBins.DataAccess/BaseRepo.cs#L25 – Shyju May 24 '18 at 16:52
  • Thanks - coming back to my question - how can I pass the connection string from the core app to the non core class library, without changing the class library? – niico May 24 '18 at 17:00
  • I get this error when I try and build your solution: Detected package downgrade: NETStandard.Library from 1.6.1 to 1.6.0. Reference the package directly from the project to select a different version. – niico May 24 '18 at 20:46

1 Answers1

0

I realise that ideally you'd use DI and appsettings.json to store a connection string in .net core MVC 2.

However, regarding my specific question - passing a connectionstring to a Class Library that's already expecting something from the web.config - and to aid in transitioning to .net core - this other question was useful: How to read web.config file in .Net Core app

But no answer answered it - a comment did from Zuhair. Basically just rename web.config app.config and it works. It works for the linked class library too.

Here is my app.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <connectionStrings>
    <add name="DefaultConnection" connectionString="Server=.;Database=mydb;Trusted_Connection=true;" providerName="System.Data.SqlClient" />
  </connectionStrings>
</configuration>

You also need to install this NuGet package:

System.Configuration.ConfigurationManager
niico
  • 11,206
  • 23
  • 78
  • 161