0

I have a ASP.NET Core Web API project added to my solution. It uses a project dependency which connects to SQL Server database, using a connection string like this:

public string connectionString = ConfigurationManager.ConnectionStrings["ConnectionName"].ConnectionString;

This works fine, another Web/WinForm Apps uses this library and connects to SQL Server instance (the connection string is set in the web.config or app.config)

The problem:

The ASP.NET Core Web API project fails to use the connection string from the library, I have set appsetting.json:

 "ConnectionStrings": {
    "ConnectionName": "Data Source=database;Initial Catalog=catalogname;User ID=username;Password=password;"
  },

But always the connection string with the connection name is null, and has:

data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true
3
  • Can you show the line of C# code that is failing (returning null for the connection string in the JSON file)? Commented Jan 27, 2023 at 1:28
  • The c# code that is failing is the first code snippet in the question: "public string connectionString = ConfigurationManager.ConnectionStrings["ConnectionName"].ConnectionString;" This is located in the library. Commented Jan 27, 2023 at 3:47
  • 1
    ConfigurationManager doesn't work with appsettings.json--you have to use another approach. Does this help?: stackoverflow.com/questions/52960856/… Commented Jan 27, 2023 at 15:11

2 Answers 2

2

It seems ConfigurationManager failed to read json file,have you tried add a .config file in your WebApi project? I tried two solutions as below :

public  class Class1
    {
        public  string? con1 { get; set; }

        public   string? con2 { get; set; }
        public readonly IConfiguration Config;

        public Class1()
        {
            //read connectstring from .config file
            con1 = System.Configuration.ConfigurationManager.ConnectionStrings["ReadConnectStringContext"].ConnectionString;
            Config = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("appsettings.json").Build();
            //read connectstring from appsettings.json
            con2 = Config.GetConnectionString("ReadConnectStringContext");
        }

    }

The structure:

enter image description here

The Result:

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, but I had to use the appsettings.json. I was not able to use my dll class to connect to SQL Server, I had to create another classes connected to the WebAPI project.
1

Here is the documentation from MS:

Class libraries can access configuration settings in the same way as executable apps, however, the configuration settings must exist in the client app's App.config file. Even if you distribute an App.config file alongside your library's assembly file, the library code will not read the file.

A quick fix for you is to place the correct DB connection string section in the Web/WinForm Apps settings file.

If you really want to use the app settings from the library project, using a custom setting to reference the app settings from in the library project. Here is the reference.

1 Comment

I have been trying to add the connection string in the ASP.NET Core Web API project. I have added it in appsetting.json file, but it does not work yet.

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.