Проблема файла конфигурации ASP.NET Core WEB API - PullRequest
0 голосов
/ 03 мая 2018

Я не уверен, связана ли эта проблема с ASP.NET Core или нет.

Я определяю свои строки подключения в файле appsettings.json и публикую проект API.

Когда я выполняю конечную точку, я получаю эту ошибку: «Не удалось найти строку подключения с именем« DbContext »в файле конфигурации приложения.», Хотя я определил DbContext внутри appsettings.json

Единственный способ заставить его работать - это явно указать строки соединения внутри файла projectname.exe.config. Есть идеи, почему это так? Разве я не могу просто указать строки подключения в appsettings.json, и при необходимости их можно будет выбрать там?

Ниже приведен мой файл appsettings.json:

{
 "ConnectionStrings": {
"DbConnectionString": "data source=DEV-DB;initial catalog=AmsSystem;user id=sa;password=111111;multipleactiveresultsets=True;application name=EntityFramework",
"DbContext": "data source=DEV-DB;initial catalog=AmsSystem;user id=sa;password=111111;multipleactiveresultsets=True;application name=EntityFramework"
},
"AppSettings": {
"AESEncryptionKey": "qgJMnfoVGxwZsl8eELea5/1jqyQJg2mgC/Vb4mUetN4=",
"VirtualDirectory": "/demoapi"
},
"Logging": {
"IncludeScopes": false,
"Debug": {
  "LogLevel": {
    "Default": "Warning"
  }
},
"Console": {
  "LogLevel": {
    "Default": "Warning"
  }
}

},
 "JwtSecurityToken": {
"Key": "YouCannotAlterTokenIfYouCannotHoldThisVeryLongKey",
"Issuer": "http://demoserver.com",
"Audience": "http://demoserver.com"
}
 }

Program.cs

 public class Program
{
    public static void Main(string[] args)
    {
        BuildWebHost(args).Run();
    }

    public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>()
            .Build();
}

Startup.cs

 public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();

        services.AddScoped(_ => new DbContext());

        // get the application configuration settings
        var appSettings = config.GetSection("AppSettings");
        services.Configure<AppConfig>(appSettings);

        services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();

        // Add custom repositories
        services.AddTransient<IModelFactory, ModelFactory>();


        // Add custom services
        services.AddTransient<IAppointmentProvider, AppointmentDataProvider>();
        services.AddTransient<ILogProvider, EventLogProvider>();

        // add the database connection
        services.AddDbContext<SecurityContext>(options =>
    options.UseSqlServer(config.GetConnectionString("DbConnectionString")));

        // add ASP.NET identity service
        services.AddIdentity<ApiUser, ApiRole>()
            .AddEntityFrameworkStores<SecurityContext>()
            .AddDefaultTokenProviders();
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...