我有一个依赖于 MSSQL的服务(一个 ASP.NET Core Web 应用程序)。这些服务是使用 Docker compose 编排的,我希望 docker compose在运行我的服务之前首先启动数据库并等待它准备就绪。为此,我将其定义docker-compose.yml为:
version: '3.7'
services:
sql.data:
container_name: db_service
image: microsoft/mssql-server-linux:2017-latest
healthcheck:
test: ["CMD", "/opt/mssql-tools/bin/sqlcmd", "-S", "http://localhost:1433", "-U", "sa", "-P", "Pass_word", "-Q", "SELECT 1", "||", "exit 1"]
my_service:
container_name: my_service_container
image: ${DOCKER_REGISTRY-}my_service
build:
context: .
dockerfile: MyService/Dockerfile
depends_on:
- sql.data
Run Code Online (Sandbox Code Playgroud)
通过这种健康检查,Docker compose 不会等待数据库服务准备就绪my_service,而是立即启动,并且如预期的那样,my_service无法连接到数据库。部分日志是:
Recreating db_service ... done
Recreating my_service_container ... done
Attaching to db_service, my_service_container
my_service_container | info: ...Context[0]
my_service_container | Migrating …Run Code Online (Sandbox Code Playgroud) 在 Github 操作中,您可以使用 获取提交 SHA ${GITHUB_SHA},这是默认的环境变量。。然而,这个提交SHA似乎是一个合并提交!?它不等于 PR 的 Github UI 上显示的提交 SHA。关于如何获取 PR 中显示的 SHA(在 Github UI 上)有什么想法吗?
在 C# 中,我对停止循环感兴趣Parallel.ForEachAsync(考虑和之间的差异StopBreak);因为Parallel.ForEach我可以执行以下操作:
Parallel.ForEach(items, (item, state) =>
{
if (cancellationToken.IsCancellationRequested)
{
state.Stop();
return;
}
// some process on the item
Process(item);
});
Run Code Online (Sandbox Code Playgroud)
但是,由于我有一个需要异步执行的进程,所以我切换到了Parallel.ForEachAsync. ForEachAsync没有该方法Stop(),我可以按break如下方式循环,但我想知道这是否是打破循环的最有效方法(换句话说,循环在收到取消时需要尽快停止要求)。
await Parallel.ForEachAsync(items, async (item, state) =>
{
if (cancellationToken.IsCancellationRequested)
{
return;
}
// some async process on the item
await ProcessAsync(item);
});
Run Code Online (Sandbox Code Playgroud) c# asynchronous cancellation parallel.foreach parallel.foreachasync
假设以下代码:
if (myDictionary.ContainsKey(aKey))
myDictionary[aKey] = aValue;
else
myDictionary.Add(aKey, aValue);
Run Code Online (Sandbox Code Playgroud)
此代码访问字典两次,一次用于确定是否aKey存在,另一次用于更新(如果存在)或添加(如果不存在).我想这个代码只执行几次时,这种方法的性能是"可接受的".但是,在我的应用程序中,类似的代码执行大约500K次.我描述了我的代码,它显示了80%的CPU时间花在了这一部分上(见下图),因此这有助于改进.
第一个解决方法很简单:
myDictionary[aKey] = aValue;
Run Code Online (Sandbox Code Playgroud)
如果aKey存在,则将其值替换为aValue; 如果不存在,KeyValuePair与aKey关键和aValue作为价值被添加到myDictionary.但是,这种方法有两个缺点:
首先,您不知道是否aKey存在会阻止您使用其他逻辑.例如,您无法根据此变通方法重写以下代码:
int addCounter = 0, updateCounter = 0;
if (myDictionary.ContainsKey(aKey))
{
myDictionary[aKey] = aValue;
addCounter++;
}
else
{
myDictionary.Add(aKey, aValue);
updateCounter++;
}
Run Code Online (Sandbox Code Playgroud)
其次,更新不能是旧值的函数.例如,你不能做一个类似于以下的逻辑:
if (myDictionary.ContainsKey(aKey))
myDictionary[aKey] = (myDictionary[aKey] * 2) + aValue;
else
myDictionary.Add(aKey, aValue);
Run Code Online (Sandbox Code Playgroud)
第二种解决方法是使用ConcurrentDictionary.很明显,使用 delegates我们可以解决上述第二个 …
我正在尝试为我在 docker 上部署的服务设置数据库连接,其中服务和数据库都是 dockerized 的。
当我将有效负载发布到服务的 API 时,出现以下错误:
SqlException:建立与 SQL Server 的连接时发生与网络相关或特定于实例的错误。服务器未找到或无法访问。验证实例名称是否正确以及 SQL Server 是否配置为允许远程连接。(提供程序:TCP 提供程序,错误:40 - 无法打开与 SQL Server 的连接)
API 定义为:
[HttpPost]
public async Task<IActionResult> Postfoo([FromBody] Foo foo)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
_context.Foos.Add(foo);
await _context.SaveChangesAsync();
return CreatedAtAction("GetFoo", new { id = foo.ID }, foo);
}
Run Code Online (Sandbox Code Playgroud)
数据库连接定义如下appsettings.json:
"ConnectionStrings": {
"DefaultConnection": "Server=tcp:127.0.0.1,1433;Trusted_Connection=True;MultipleActiveResultSets=true;User ID=sa;Password=Pass@word"
},
Run Code Online (Sandbox Code Playgroud)
定义db连接的启动逻辑Startup.cs如下:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddDbContext<WorkflowContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
}
Run Code Online (Sandbox Code Playgroud)
在 中docker-compose.yml,数据库服务定义为:
version: '3.4'
services: …Run Code Online (Sandbox Code Playgroud) sql-server entity-framework database-connection docker asp.net-core
在 Windows 10 上,使用 Docker 桌面版本 2.1.0.5。
我已经成功使用该图像microsoft/mssql-server-linux:2017-latest,但由于它已被弃用,我将其更改为mcr.microsoft.com/mssql/server:2019-latest我遇到问题的地方。
我将数据库服务定义为:
version: '3.4'
services:
sql.data:
container_name: tvq_db
image: mcr.microsoft.com/mssql/server:2019-latest
environment:
- SA_PASSWORD=Pass_word
- ACCEPT_EULA=Y
ports:
- "1433:1433"
volumes:
- ${APPDATA}/MyServiceDB/mssql:/var/opt/mssql
Run Code Online (Sandbox Code Playgroud)
使用此设置,我的 ASP.NET Core 应用程序无法连接到数据库服务,当我检查数据库容器的日志时,我看到许多Permission denied错误。日志如下:
This program has encountered a fatal error and cannot continue running at Thu Jan 23 19:35:01 2020
SQL Server 2019 will run as non-root by default.
The following diagnostic information is available:
Reason: 0x00000006
This container is running as …Run Code Online (Sandbox Code Playgroud) 我需要使我的可变类不可变,现在它看起来如下.但是,我仍然不确定我是否有一个完全"不可变的*类,如果是这样,那么这个叫做什么样的不变性?
public class B<C, M>
where C : IComparable<C>
where M : IMetaData
{
internal B(char tau, M metadata, B<C, M> nextBlock)
{
if (tau == 'R') omega = 1;
_lambda = new List<Lambda<C, M>>();
_lambda.Add(new Lambda<C, M>(tau: tau, atI: metadata));
foreach (var item in nextBlock.lambda)
if (item.tau != 'L')
_lambda.Add(new Lambda<C, M>(tau: 'M', atI: item.atI));
}
internal int omega { private set; get; }
private List<Lambda<C, M>> _lambda { set; get; }
internal …Run Code Online (Sandbox Code Playgroud) 我想使用SonarQube来分析对我的项目进行的任何拉取请求(PR).
几点快点:
dev分支master的分支);鉴于这些要点,这就是我所做的:
Administration -> Settings -> Pull Requests (Alpha),我设置Authentication token机器人的令牌,并Repository identifier以<Organization>/<Repo>格式设置我的回购地址;appveyor.yml我的项目文件,以便对所有PR运行SonarQube分析; 类似如下:build_script:
choco install "msbuild-sonarqube-runner" -y
MSBuild.SonarQube.Runner.exe begin /k:"REPO" /o:"ORGANIZATION" /d:"sonar.host.url=https://sonarcloud.io" /d:"sonar.analysis.mode=preview"
MSBuild.exe /t:Rebuild
MSBuild.SonarQube.Runner.exe end
Run Code Online (Sandbox Code Playgroud)
但是,我仍然没有看到SonarQube使用机器人评论新的PR(类似于他们的演示.
我有一个条件,如下所示:
ok = (not a > 10 and
not b < 10 and
not c > 99 and
d == 99)
Run Code Online (Sandbox Code Playgroud)
flake8 抱怨这一行并显示错误消息:
W504 二元运算符后换行
当我移动操作员时,它会抛出不同的错误:
ok = (not a > 10
and not b < 10
and not c > 99
and d == 99)
Run Code Online (Sandbox Code Playgroud)
W503 二元运算符前换行
我尝试了多个建议(例如,this),但 flake8 仍然抱怨换行。我的代码中的实际情况非常长,因此我无法将其放在一行中,而且我的团队更喜欢将长行括起来()而不是使用\.
我正在使用镜像在 docker 上运行 SQL Server 数据库microsoft/mssql-server-linux:2017-latest。
中的数据库连接字符串appsettings.json定义为:
"ConnectionStrings": {
"DefaultConnection": "Server=sql.data,1433;MultipleActiveResultSets=true;User Id=SA;Password=Pass@word"
},
Run Code Online (Sandbox Code Playgroud)
数据库服务定义如下docker-compose.yml:
sql.data:
image: microsoft/mssql-server-linux:2017-latest
environment:
- ID=SA
- PASSWORD=Pass@word
- ACCEPT_EULA=Y
ports:
- "1433:1433"
Run Code Online (Sandbox Code Playgroud)
当我尝试在应用程序中连接到数据库(使用实体框架)时,出现以下错误:
SqlException:用户“SA”登录失败。
为了调试它,我登录到 docker 映像,并尝试使用以下命令从 docker 访问数据库:
/opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P "Pass@word"
Run Code Online (Sandbox Code Playgroud)
然后我收到以下错误:
Sqlcmd:错误:适用于 SQL Server 的 Microsoft ODBC 驱动程序 17:用户“SA”登录失败。
我尝试过"(双引号),'(单引号),并且不引用用户名和密码,以及两者的任意组合,但仍然得到相同的错误。
c# ×7
asp.net-core ×4
sql-server ×4
docker ×3
github ×2
asynchronous ×1
atomic ×1
cancellation ×1
dictionary ×1
flake8 ×1
git ×1
immutability ×1
mutable ×1
pep8 ×1
pull-request ×1
python ×1
sonar-runner ×1
sonarqube ×1