1

I have connection string in azure api configuration with the same name, but when i go to api, swagger, it tells me - System.PlatformNotSupportedException: LocalDB is not supported on this platform. returning 500 status. this is how i get connection string from api:

private string _connectionString = string.Empty;

        public string GetConnectionString(string name)
        {
            var configurationBuilder = new ConfigurationBuilder();
            var path = Path.Combine(Directory.GetCurrentDirectory(), "appsettings.json");
            configurationBuilder.AddJsonFile(path, false);

            var root = configurationBuilder.Build();
            _connectionString = root.GetSection("ConnectionStrings").GetSection(name).Value;
            var appSetting = root.GetSection("ApplicationSettings");

            return _connectionString;
        }

API project is on .net 5

1 Answer 1

1

This is because you are building you configuration from file only, while you need to add AddEnvironmentVariables so it will add properties from Azure App Service Configuration

Try this:

  public string GetConnectionString(string name)
        {
            var path = Path.Combine(Directory.GetCurrentDirectory(), "appsettings.json");
            var root = new ConfigurationBuilder()
                .AddJsonFile(path, false)
                .AddEnvironmentVariables()
                .Build();

            _connectionString = root.GetSection("ConnectionStrings").GetSection(name).Value;
            var appSetting = root.GetSection("ApplicationSettings");

            return _connectionString;
        }

Since its in library you need add package Microsoft.Extensions.Configuration.EnvironmentVariables which contains AddEnvironmentVariables extension.

The other way to do it would be to add FrameworkReference in your project but that would bring lots of asp.net core stuff you probably dont need

  <ItemGroup>
    <FrameworkReference Include="Microsoft.AspNetCore.App" />
  </ItemGroup>
Sign up to request clarification or add additional context in comments.

6 Comments

It says that IConfigurationBuilder does not contain a definition for AddEnvironmentVariables
try to add namespace using Microsoft.Extensions.Configuration;
@komnen is that code in librarry or webapi project?
It's in library edit: Oh, could that be a problem? I didnt publish my library yet
@komnen done. Ask if you have any other questions
|

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.