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.

16
  • In your asp.net core project, You should inject IConfiguration and then you can read the connection string. Read this learn.microsoft.com/en-us/aspnet/core/fundamentals/… Commented May 24, 2018 at 2: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. Commented May 24, 2018 at 2:08
  • No. You keep the connection string in the appsetttings json file Commented May 24, 2018 at 2:13
  • Ahh OK - I have already done that too. Can you point me at the specific code to add? Commented May 24, 2018 at 2:14
  • Check that page i linked. Check the sample line Configuration["MyConfig"];. Replace that with connection string key( Ex : Configuration["ConnectionStrings:DefaultConn"];) Commented May 24, 2018 at 2:16

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.