我在C#中有以下异步函数:
private async Task<T> CallDatabaseAsync<T>(Func<SqlConnection, Task<T>> execAsync)
{
using (var connection = new SqlConnection(_connectionString))
{
connection.Open();
return await execAsync(connection);
}
}
Run Code Online (Sandbox Code Playgroud)
它允许执行任何异步函数execAsync,它将SQL连接作为参数并通过提供连接对象并确保它将被正确关闭来使用它来进行数据库调用.
然后从WebApi控制器中的操作调用此函数,如下所示:
public async Task<HttpResponseMessage> MyAction()
{
Func<SqlConnection, Task<SomeType>> execAsync = (function definition here);
await CallDatabaseAsync(execAsync);
return Request.CreateResponse(HttpStatusCode.OK);
}
Run Code Online (Sandbox Code Playgroud)
这一切都很有效,直到我对WebApi操作进行了一次更改:我从中删除了异步/等待.我不想等待数据库调用,因为我不关心结果,我只是想解雇并忘记.
这仍然似乎工作正常 - 即如果我在浏览器中导航到操作的URL我没有得到任何错误.但实际上有一个问题 - 数据库连接没有关闭.在100次调用操作后,连接池达到其默认限制100,并且应用程序停止工作.
我究竟做错了什么?我需要在CallDatabaseAsync()中进行哪些更改,以便绝对确保连接将被关闭,无论如何?
我只是尝试将 Swagger 与 ASP.Net Core Web API 项目一起使用,但它没有找到我的任何控制器/操作。结果 Swagger 不支持传统路由(如此处所述https://github.com/domaindrivendev/Swashbuckle.AspNetCore)。我所有的路线都是在一个函数中使用 routeBuilder.MapRoute() 在代码中生成的。我有 150 个控制器和大约 500 个动作——将属性路由应用到这个项目将是一场噩梦!
似乎没有办法让 Swagger 与传统路由一起工作。有没有 Swagger 的替代品?
我在优化 SQL Server 上的查询时遇到了我意想不到的问题。tblEvent数据库中有一个表,其中包含IntegrationEventStateId和ModifiedDateUtc。这些列有一个索引:
create index IX_tblEvent_IntegrationEventStateId_ModifiedDateUtc
on dbo.tblEvent (
IntegrationEventStateId,
ModifiedDateUtc
)
Run Code Online (Sandbox Code Playgroud)
当我执行以下语句时:
select *
from dbo.tblEvent e
where
e.IntegrationEventStateId = 1
or e.IntegrationEventStateId = 2
or e.IntegrationEventStateId = 5
or (e.IntegrationEventStateId = 4 and e.ModifiedDateUtc >= dateadd(minute, -5, getutcdate()))
Run Code Online (Sandbox Code Playgroud)
我得到了这个执行计划(注意索引没有被使用):
但是当我执行这个语句时:
select *
from dbo.tblEvent e
where
1 = e.IntegrationEventStateId
or 2 = e.IntegrationEventStateId
or 5 = e.IntegrationEventStateId
or (4 = e.IntegrationEventStateId and e.ModifiedDateUtc >= dateadd(minute, -5, getutcdate()))
Run Code Online (Sandbox Code Playgroud)
我得到了这个执行计划(注意索引确实被使用了):
两个语句之间的唯一区别是where子句中的比较顺序。谁能解释为什么我得到不同的执行计划? …
在 ASP.Net Core 中,我可以通过提供多个 appsettings.<EnvironmentName>.json 文件来为不同的环境设置不同的应用程序设置。但是如何为不同的环境提供不同的 web.config 文件呢?
将缓存条目添加到 System.Runtime.Caching.MemoryCache 时,可以使用 AbsoluteExpiration 指定 CacheItemPolicy。根据文档(https://msdn.microsoft.com/en-us/library/Dd780607(v=VS.110,d=hv.2).aspx),AbsoluteExpiration是“在缓存条目被逐出”。
是的,这就是我想要的 - 指定我的缓存条目将过期的“时间段”。但 AbsoluteExpiration 的类型为 DateTimeOffset 而不是 TimeSpan,因此它是“时间点”而不是“时间段”。那我该怎么设置呢?例如,如果我希望条目在 60 秒内过期,我应该将 AbsoluteExpiration 设置为 吗DateTimeOffset.UtcNow.AddSeconds(60)?如果我设置为 ,我会得到相同的结果吗DateTimeOffset.Now.AddSeconds(60)?有什么理由使用其中之一?
以下是重现的步骤。下面的程序使用 .Net Core 控制台应用程序和 EF Core 将 10,000 行从一个 SQL 表复制到另一个。该程序分 100 批插入记录,并且(这很重要!)它为每个插入创建一个新的 DbContext 实例。
1) 创建 SQL Server 数据库,以及“Froms”和“Tos”表:
create table Froms (
Id int identity(1, 1) not null,
Guid [uniqueidentifier] not null,
constraint [PK_Froms] primary key clustered (Id asc)
)
go
create table Tos (
Id int not null,
Guid [uniqueidentifier] not null,
constraint [PK_Tos] primary key clustered (Id asc)
)
go
Run Code Online (Sandbox Code Playgroud)
2) 填充“Froms”表:
set nocount on
declare @i int = 0
while @i < 10000
begin
insert …Run Code Online (Sandbox Code Playgroud) 我正在使用Serilog和ASP.Net Core 2.0,使用JsonFormatter写入RollingFile.按照此处的说明进行配置:https://github.com/serilog/serilog-aspnetcore.一切都很好,但在每个日志条目中我得到以下没有记录的属性:
我认为它们是由ASP.Net Core日志框架添加的.我怎么能摆脱他们?
如果我有这样的 ExpandoObject:
dynamic d = new ExpandoObject();
d.x = "a";
d.y = "b";
Run Code Online (Sandbox Code Playgroud)
并使用 JsonFormatter 将 Serilog 记录到 RollingFile,如下所示:
_logger.Debug("{@d}", d);
Run Code Online (Sandbox Code Playgroud)
它将被序列化为 json,如下所示:
[{"_typeTag":"KeyValuePair`2","Key":"x","Value":"a"},{"_typeTag":"KeyValuePair`2","Key":"y","Value":"b"}]
Run Code Online (Sandbox Code Playgroud)
如果我使用 Newtonsoft.Json 序列化相同的 ExpandoObject,如下所示:
JsonConvert.SerializeObject(d)
Run Code Online (Sandbox Code Playgroud)
我会得到这个:
{"x":"a","y":"b"}
Run Code Online (Sandbox Code Playgroud)
如何使 Serilog 生成与 Newtonsoft.Json 相同的 json?
我有两台显示器,Windows 10 将它们识别为 1 和 2。我想更改哪一台。是否可以?
请不要回复说我应该进入显示设置并拖动显示器来代表我的物理布局,或者我应该将所需的显示器标记为主要显示器。我已经完成了所有这些。它修复了几乎所有问题,但例如每次我重新启动笔记本电脑并打开 Visual Studio 时,它都会在监视器#1 上打开,尽管我将#2 标记为主要监视器。
我正在将 Swagger 与 ASP.Net Core 2.1 Web API 项目结合使用。下面是一个控制器操作方法示例:
[HttpGet]
public string GetString([Required, MaxLength(20)] string name) =>
$"Hi there, {name}.";
Run Code Online (Sandbox Code Playgroud)
这是我在 Swagger 文档中得到的内容。正如您所看到的,Swagger 显示了Required属性,但没有显示MaxLength属性:
如果我在作为 POST 操作方法参数的 DTO 类上使用Required和属性,则 Swagger 会同时显示它们:MaxLength
如何让 Swagger 显示MaxLength查询参数的(和其他)验证属性?
注意:我尝试string name用一个类替换该参数,该类具有一个名为name- Swagger 的字符串属性,生成完全相同的文档。
我运行 Windows 10 并有两个显示器,如下面的屏幕截图所示:
显示器 2 设置为我的主监视器,但是当我启动 Visual Studio 时,它总是在显示器 1 上打开。 SQL Server Management Studio 也是如此,因为它基于 VS。
有没有办法解决这个问题 - 无论是在 VS 设置中,还是通过更改 Windows 中的显示数字?
asp.net-core ×3
c# ×3
serilog ×2
swagger ×2
async-await ×1
caching ×1
display ×1
ef-core-2.0 ×1
ef-core-2.1 ×1
json ×1
memorycache ×1
sql-server ×1
swashbuckle ×1
using ×1
web-config ×1
windows ×1