简体   繁体   中英

.NET Core API call to a method in a .NET Project is saying it can't find Connection strings in the app.config file

I have a Core API project that was originally accessing the database from the controller (bad practice) so I moved the data access methods into an existing .NET class library where the connection strings are stored in the app.config file. The same connection strings are also located in the Core API project's appsettings.json/appsettings.development.json folders.

I then tried to access this new .NET class library method (containing the call to the database) from my API, but I'm getting the "can't find 'connectionstringname' in app.config" error. I'm new to .NET but a colleague was saying perhaps the app.config isn't getting pulled into the API's bin folder. Has anyone had problems with .NET Core projects struggling to communicate with .NET project's connection strings and how could I go about troubleshooting this?

1.) I tried pasting the app.config file into the API's bin folder, to no avail (same error.)

I've seen a few answers on SO but haven't been able to implement them successfully, so I apologize if this has been answered before.

You need to completely throw out your dot-net-framework understanding of config settings.

https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/options?view=aspnetcore-5.0

Note the value of the url.

aspnet core fundamentals configuration

In DotNet-Core: Highly recommend creating a first class object. (config-value-holder-poco)

And in your Ioc Registrations, inject the config-value-holder-poco into the Ioc (aka, "servicecollection" in dn-core).

And then constructor inject your settings poco holder.

As per the article.

Json "section"

  "Position": {
    "Title": "Editor",
    "Name": "Joe Smith"
  }

First class config-value-holder-poco.

public class PositionOptions
{
    public const string Position = "Position";

    public string Title { get; set; }
    public string Name { get; set; }
}

IoC registration.

    public void ConfigureServices(IServiceCollection services)
{
    services.Configure<PositionOptions>(Configuration.GetSection(
                                        PositionOptions.Position));
  
}

Constructor Injection:

public class Test2Model : PageModel
{
    private readonly PositionOptions _options;

    public Test2Model(IOptions<PositionOptions> options)
    {
        _options = options.Value;
    }

    public ContentResult OnGet()
    {
        return Content($"Title: {_options.Title} \n" +
                       $"Name: {_options.Name}");
    }
}

and now you can take advantage of the "refresh-rate" BUILT IN options for how these are treated.

https://medium.com/@kmar.ayush/eli5-ioptions-vs-ioptionssnaphot-vs-ioptionsmonitor-fab1d7e26a75

IOptions vs IOptionsSnaphot vs IOptionsMonitor

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM