【发布时间】:2020-01-15 08:09:07
【问题描述】:
我有一个使用 SQL 数据库运行的 Net.Core 应用程序。连接字符串在环境变量中。
在本地 IIS 上,应用程序运行良好。
与 Docker-Container 相同的应用程序出现以下错误
失败:Microsoft.AspNetCore.Server.Kestrel[13] Connection id "0HLPO85V83VNO", Request id "0HLPO85V83VNO:00000001": 一个未处理的异常被抛出 应用。 System.PlatformNotSupportedException:LocalDB 不是 在此平台上受支持。
在 System.Data.SqlClient.SNI.LocalDB.GetLocalDBConnectionString(字符串 localDbInstance)
在 System.Data.SqlClient.SNI.SNIProxy.GetLocalDBDataSource(字符串 fullServerName,布尔值&错误)
Docker 的环境变量: “DB_CONNECTION”:“服务器=hc-XXX;数据库=IB33DB-Core_XXX;用户ID=sa;密码=XXX”
这是网络中的设置
Startup.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using ib.api_core.Data;
using Microsoft.EntityFrameworkCore;
using ib.api_core.Models;
namespace ib.api_core
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
.AddJsonOptions(x => x.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore);
string dbConnection = Environment.GetEnvironmentVariable("DB_CONNECTION");
Console.WriteLine("Env var DB Connection: "+dbConnection);
//Verweis auf den Datenbank Kontext
services.AddDbContext<ibContext>(options =>
options.UseSqlServer(dbConnection));
//Verweis auf den Datenbank Kontext
// services.AddDbContext<ibContext>(options =>
// options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
//1. Alle Anfragen in der Konsole loggen
app.UseMiddleware<RequestResponseLoggingMiddleware>();
//2. Prüfe Login und Berechtigung der Anfrage
app.UseMiddleware<AuthenticationMiddleware>();
app.UseHttpsRedirection();
app.UseMvc();
}
}
}
ibContext.cs
using System;
using Microsoft.EntityFrameworkCore;
namespace ib.api_core.Models
{
public partial class ibContext : DbContext
{
public ibContext()
{
}
public ibContext(DbContextOptions<ibContext> options)
: base(options)
{
}
public static ibContext GetContext()
{
var optionsBuilder = new DbContextOptionsBuilder<ibContext>();
optionsBuilder.UseSqlServer(Environment.GetEnvironmentVariable("DB_CONNECTION"));
return new ibContext(optionsBuilder.Options);
}
[...]
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseSqlServer(Environment.GetEnvironmentVariable("DB_CONNECTION"));
}
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
....
【问题讨论】:
-
你可以在容器中发布连接字符串的主要部分而不包含敏感信息(例如帐户,密码)吗?它与部署到 IIS 时使用的连接字符串相同吗?
-
当然是一样的,连接字符串在问题里
标签: c# docker asp.net-core .net-core