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
amc software
  • 747
  • 1
  • 7
  • 18
  • Can you show the line of C# code that is failing (returning null for the connection string in the JSON file)? – Tawab Wakil Jan 27 '23 at 01: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. – amc software Jan 27 '23 at 03:47
  • 1
    `ConfigurationManager` doesn't work with appsettings.json--you have to use another approach. Does this help?: https://stackoverflow.com/questions/52960856/does-configurationmanager-work-with-asp-net-cores-appsettings-json – Tawab Wakil Jan 27 '23 at 15:11

2 Answers2

1

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

Ruikai Feng
  • 6,823
  • 1
  • 2
  • 11
  • 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. – amc software Jan 27 '23 at 18:26
0

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.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Charles Han
  • 1,920
  • 2
  • 10
  • 19
  • 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. – amc software Jan 27 '23 at 03:48