0

How to force App Service to read connection string from settings, and not from the appsettings.json file?

We’ve tried Settings/Application Settings/Connection Strings and Settings/Configuration (preview)/Connection strings, but it keeps reading from uploaded appsettings.json.

We’ve using Azure DevOps and tried Web One Click Publish. Probably it would with work with manual publish and connection string replacement directly from publish settings (in VS).

We used this approach for other (non-asp.net core) WebSites, and it works as it should.

I’ve checked tutorials for Asp.Net Core and Azure Hosting, and I believe I did everything right?

Where do I even start to debug this thing?

EntityFramework is in a separate project, we’re using dependency injection with a custom constructor for the connection string, and it works just fine if I setup appsettings.json file manually, but I have to deploy it on multiple servers, and it would not make much sense to this stuff from code or manually each time. And I wouldn’t like to exclude auto deployment of appsettings.json, I just want to transform it on the server.

ConfigureServices

IConfigurationRoot configuration = new ConfigurationBuilder().SetBasePath(AppDomain.CurrentDomain.BaseDirectory).AddJsonFile("appsettings.json", optional: false, reloadOnChange: true).Build();
            string connectionString = configuration.GetConnectionString("DefaultConnection");

appsettings.json

{
  "ConnectionStrings": {
    "DefaultConnection": "xxx"
  },
...

Perhaps I’m reading appsetting.json wrong, and Azure cannot inject new connection string properly?

Bzzz
  • 53
  • 1
  • 6
  • You need to add environment variables to the config builder. If you are using ASP.NET Core 2.x then you don't need to build the config yourself by the way, you can get it in Startup constructor as IConfiguration. – juunas Mar 10 '19 at 09:41
  • Thank you! _>> If you are using ASP.NET Core 2.x then you don't need to build the config yourself by the way, you can get it in Startup constructor as IConfiguration. <<_ This was the problem :| If I build config manually, Azure cannot inject connection string. But if I use IConfiguration from public Startup(IConfiguration configuration) everything works. Please move the comment to answer. – Bzzz Mar 10 '19 at 10:02

1 Answers1

1

Based on the discussion in the comments, the solution was to use the IConfiguration object provided via the Startup class constructor (this only works in Core 2.x+).

So for example:

private readonly IConfiguration _config;

public Startup(IConfiguration config)
{
    _config = config;
}

You can then use _config to access settings within Startup.

juunas
  • 54,244
  • 13
  • 113
  • 149
  • 1
    Could you add some details? :) Are you trying to inject IConfiguration to your Startup class constructor and getting an error? – juunas Aug 21 '19 at 19:37
  • I have this code >> https://github.com/medhatelmasry/JwtAuthentication/blob/master/JwtAuthentication/Startup.cs. Copied almost exactly, only difference being I've added an #if block to check for the local VS configuration (debug/release) and it will load a hard-coded local db string if it's debug. I also have an appsettings.json file in the solution which contains data needed for the JWT authentication. I'm happy to get rid of it if I can put it somewhere else. – ataraxia Aug 21 '19 at 19:51
  • I'm getting to the point of considering paying for help because this is seriously holding me up now. This is my API that everything needs to be requesting data from, nothing will work without it. That, and I've been up until almost 2am every morning whilst working a full time job trying to fix it and I'm getting really f***ing tired now. – ataraxia Aug 21 '19 at 22:51
  • Could you create a new question with all the detail you can put there and send the link to it here? – juunas Aug 22 '19 at 05:01
  • I've created a question here >> https://stackoverflow.com/questions/57606636/azure-cannot-access-connection-string-stored-in-app-service-configuration – ataraxia Aug 22 '19 at 09:54