这个问题源于我问过的关于太多接口、QCRS 和 Mediatr 库(请求/响应)的另一个问题
我创建了一堆命令和查询,我有一堆行为,其中一个是缓存行为,对于每个查询,在实际针对数据库执行查询之前,会检查缓存的值。到目前为止,这很好用,但是当我有一个 UpdateSomethingCommand 时,delima 就出现了,一旦我更新了数据库中的底层对象,我想用成功保存到数据库的内容刷新缓存。
我的问题是具体何时实际更新缓存:
我必须在某处做错事,因为我在我的concurrentbag中得到了重复的项目,这里是事件链
var listings = new ConcurrentBag<JSonListing>();
Parallel.ForEach(Types, ParallelOptions, (type, state) =>
{
...
status = ProcessType(listings, status, type, state);
....
});
private GeocodeResults ProcessType(ConcurrentBag<JSonListing> listings, GeocodeResults status, XElement type, ParallelLoopState state)
{
....
AddListingsToList(results, type, listings);
....
}
private void AddListingsToList(dynamic results, XElement type, ConcurrentBag<JSonListing> listings)
{
var typeMustMatch = type.Attribute("TypeMustMatch").Value;
var typeID = Convert.ToInt32(type.Attribute("ID").Value);
foreach (var result in results.results)
{
var foundListing = listings.SingleOrDefault(x => x.ID == result.id);
if (foundListing != null)
{
var typeIDs = foundListing.TypeIDs.Split("|".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).ToList();
if …
Run Code Online (Sandbox Code Playgroud) 我有(我的网址列表大约有 1000 个网址),我想知道是否有更有效的方法从同一站点调用多个网址(已经更改了ServicePointManager.DefaultConnectionLimit
)。
另外,是在每次调用时重用相同的HttpClient
还是创建新的更好,下面仅使用一个而不是多个。
using (var client = new HttpClient { Timeout = new TimeSpan(0, 5, 0) })
{
var tasks = urls.Select(async url =>
{
await client.GetStringAsync(url).ContinueWith(response =>
{
var resultHtml = response.Result;
//process the html
});
}).ToList();
Task.WaitAll(tasks.ToArray());
}
Run Code Online (Sandbox Code Playgroud)
正如@cory建议的,
这里是使用的修改后的代码TPL
,但是我必须设置MaxDegreeOfParallelism = 100
以达到与基于任务的速度大致相同的速度,下面的代码可以改进吗?
var downloader = new ActionBlock<string>(async url =>
{
var client = new WebClient();
var resultHtml = await client.DownloadStringTaskAsync(new Uri(url));
}, new ExecutionDataflowBlockOptions { MaxDegreeOfParallelism = 100 }); …
Run Code Online (Sandbox Code Playgroud) 编辑修复-重新安装VS不起作用,重新安装Windows然后VS发挥了作用...我们几乎生活在2019年,为了使vs正常工作,需要这个狗屎吗?
我创建了一个.NET标准类库,它似乎指向1.6,当我将其更改为2.0时,我没有在列表中看到它(请参见附件图像)
还随附了我的机器和版本的最新信息,我已经安装了.NET Core 2.2.1 x64 / x86 SDK,并且拥有.NET Full Framework 4.7.2。
我有一个将实体保存到数据库的命令/处理程序,但在我的代码中,它首先使用 fluentvalidation 进行验证(验证管道)。
我能够创建一个成功测试来测试处理程序,但现在我想确保命令首先通过验证。
我该怎么做?我应该像处理处理程序一样独立调用验证吗?如果是这样我该怎么做
这是我的代码
[Test]
public async Task CreateCoinCommand_Success()
{
var context = new Mock<EventsContext>();
var ownersMock = CreateDbSetMock(new List<Owner>());
context.Setup(x => x.Owners).Returns(ownersMock.Object);
var handler = new CreateCoinCommandHandler(context.Object, mapper.Object );
var cmd = new CreateCoinCommand(1, "sym", "name", null, null, null, 1, "description",
null, "https://google.com", null, null, null, new []{1,2});
var cltToken = new System.Threading.CancellationToken();
var result = await handler.Handle(cmd, cltToken);
Assert.IsInstanceOf<Unit>(result);
}
Run Code Online (Sandbox Code Playgroud)
我的验证器被称为 CreateCoinCommandValidator
由于appsettings.json
与 blazor WA 配合得不好,我正在创建自己的文件并将文件放入.json
,wwwroot
在需要它的页面中,我使用HttpClient
来获取文件,如下所示
config = await Http.GetJsonAsync<Configuration>("/config/appsettings.json");
这很好用,但是我在多个页面中执行此操作,因此我想Configuration
在startup.cs 中注入对象/服务,并让我的页面使用它而不是多次执行此操作。
我很难找到如何正确读取 Startup.cs 中的文件,任何帮助将不胜感激。
更新
似乎App.OnInitializedAsync
之前已被触发Index.OnInitializedAsync
,但Settings = await httpClient.GetJsonAsync<Settings>("/config/appsettings.json").ConfigureAwait(false);
需要更长的时间导致App.OnInitializedAsync
在上面的行之前执行。
这是所有代码
启动.cs
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<ConfigurationManager>();
services.AddBlazoredToast();
}
public void Configure(IComponentsApplicationBuilder app)
{
app.AddComponent<App>("app");
}
}
Run Code Online (Sandbox Code Playgroud)
应用剃刀
protected override async Task OnInitializedAsync()
{
Console.WriteLine("App.OnInitializedAsync");
await configManager.InitializeAsync();
}
Run Code Online (Sandbox Code Playgroud)
索引剃刀
protected override async Task OnInitializedAsync()
{
Console.WriteLine("Index.OnInitializedAsync");
//config = await …
Run Code Online (Sandbox Code Playgroud) 我有以下内容,当DateUtc
从 source转换为 dest 时Date
,我想应用转换。现在我编写的以下转换器将适用于特定字段,因为这是指定的。
我希望我的转换器更灵活,以便我可以传递其他对象而不是SourceDto1
,DestDto1
例如SourceDto2
和DestDto2
public class SourceDto1
{
public DateTime DateUtc {get;set;}
}
public class DestDto1
{
public DateTime Date {get;set;}
}
public class SourceDto2
{
public DateTime DateUtc {get;set;}
}
public class DestDto2
{
public DateTime Date {get;set;}
}
Run Code Online (Sandbox Code Playgroud)
转换器
public class AttachmentCreatedDateResolver : IValueResolver<SourceDto1, DestDto1 DateTime>
{
private readonly Settings _settings;
public AttachmentCreatedDateResolver(Settings settings)
{
_settings = settings;
}
public DateTime Resolve(SourceDto1 source, DestDto1 destination, DateTime …
Run Code Online (Sandbox Code Playgroud) 我需要从网站上抓取数据。我有 1,000 多个链接需要访问,以前我将每个线程划分为 10 个链接,然后开始 100 个线程,每个线程拉取 10 个。经过几个测试用例后,100 个线程是最好的计数,以最大限度地减少它检索内容的时间所有的链接。
我意识到 .NET 4.0 为开箱即用的多线程提供了更好的支持,但这是根据您拥有的内核数量来完成的,在我的情况下,这不会产生足够的线程。我想我要问的是:优化 1,000 个链接拉动的最佳方法是什么。我应该使用.ForEach
并让Parallel
扩展控制产生的线程数量,还是找到一种方法来告诉它启动和划分工作的线程数?
我以前没有工作过,Parallel
所以也许我的方法可能是错误的。
经过几个小时试图找出一段代码不同步的原因后,我意识到TSQL OrderBy与Linq OrderBy的字符串差别很大.所以很自然地我必须找到一种方法来确保来自tsql的语句返回与linq相同的顺序,所以我使用了uniqueidentifier(guid)作为我的主要,但是这似乎很糟糕.
这是使用Linq的订单(使用Guid(UniqueID)类型的属性)
var listings = validListings.Select(x => x.TempListing)
.OrderBy(x => x.UniqueID)
.ToList();
Run Code Online (Sandbox Code Playgroud)
似乎TSQL使用uniqueidentifier的二进制值进行比较,而LINQ使用字符串值,即使该行是GUID
LINQ结果(印刷版)(前几个)
00460400-a41d-465d-83c5-225f697e7bb5
015bef8d-5fa3-4c03-8d05-bfecf74b36b9
0202b433-4748-4660-97a1-94d119209aa6
03f34eb0-45cd-4586-b7d2-6e337b441c43
05e41d20-be24-4f4f-b098-574744dd84f0
0767e5d5-afba-49ab-a047-c9f509c80d3a
08f87ba1-8aa8-48a6-8f98-c3b4c6511b76
0b4157c4-7bdc-4e98-a00c-9259af754844
0bd194d0-fb66-4a69-9718-2128594ff9b0
0cda256a-7632-47de-b867-a2bc46382881
0d36f81a-ca37-446e-a325-87b46ef5b8d3
0d89fd26-4204-4d0a-b187-73a36536a848
0e345ca9-3d5d-43ed-aa75-fbd356f94535
0e767557-87ea-4c31-9f54-75d354a87d0f
0f62fc97-85b0-4611-b3e5-0c5ae4f12a18
1020d776-9810-4122-a9ef-3c527f21970c
Run Code Online (Sandbox Code Playgroud)
TSQL第一个
9C5231CE-01DE-4A20-A4C9-001AD0D28512
3D52B47C-B29C-44A8-99F9-00AA660610A8
FDA7B67D-AEDB-4644-96E4-0147A0EEC29D
C8C7B677-76EB-41D3-B11C-020B9047EB00
487FF542-599B-42D4-BCE3-02C5D569E509
BDAA48DB-60AF-4A36-AFDB-02FA706EE87F
2CD9D59C-C2B5-433C-9FD1-0444F0384BB3
D44695A3-6FEF-4842-BFCB-048C110FA178
28FF051C-38A7-424F-B657-0698452DFE36
D9320EC6-64CD-4C26-8C5C-088C04E22AD7
D9F7FDC1-16D6-4C3A-B117-0908A234DF95
7DB09D09-F10B-4F33-9390-09211F9B2958
D970EE98-B575-4E73-BBAC-0981D6DC1682
9B05CDD9-2D85-486B-BC6B-0BA7E44F6021
539D22ED-FF2A-4376-A650-0BFE184C0E26
0F62FC97-85B0-4611-B3E5-0C5AE4F12A18
5D8EF134-0DC2-4B32-9F02-0C65940C1BCF
Run Code Online (Sandbox Code Playgroud)
如何让它们都返回相同的结果?
我需要在循环中创建以下内容,我有“name”和“id”,其中 name 将用于 json 对象的 value 属性,id 将用于“data”,查询将是一些字符串我可以放。我尝试使用密钥对,但无法弄清楚如何执行此属性。任何帮助,将不胜感激。
{
"query": "Unit",
"suggestions": [
{ "value": "United Arab Emirates", "data": "AE" },
{ "value": "United Kingdom", "data": "UK" },
{ "value": "United States", "data": "US" }
]
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试返回此自动完成小部件的结果 https://www.devbridge.com/sourcery/components/jquery-autocomplete/
我有以下几点:
var options = new RewriteOptions()
.AddRedirect("home/(.*)", "/$1")
.AddRedirect("Home/(.*)", "/$1")
.AddRedirect("downloadics/(.*)", "ics/$1")
.AddRedirect("DownloadICS/(.*)", "ics/$1");
Run Code Online (Sandbox Code Playgroud)
我想拥有它,以便我可以只有一个家庭条目和一个下载 ics 条目,并且不区分大小写。
我试图传递(?)
到正则表达式的前面,但它似乎爆炸了。
我有以下内容json
被发送到 api
{
"Id":22,
"UserId":22,
"Payload":{
"Field":"some payload value"
...more unknown field/values go here
},
"ContextTypeId":1,
"EventTypeId":1,
"SessionId":1
}
Run Code Online (Sandbox Code Playgroud)
我想将其映射到以下内容:
public class CreateTrackItem : IRequest<int>
{
public int Id { get; set; }
public int UserId { get; set; }
public string Payload { get; set; }
public int ContextTypeId { get; set; }
public int SessionId { get; set; }
public int EventTypeId { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
当映射Payload
属性失败,它不能映射json
到字符串,我只是想Payload
成为一个string
版本 …
appsettings.json
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Verbose",
"System": "Information",
"Microsoft": "Information"
},
"CustomSettings": {
"UseDataCaching": true
}
}
}
Run Code Online (Sandbox Code Playgroud)
选项类
public class CustomSettings
{
public bool UseDataCaching { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
startup.cs
public void ConfigureServices(IServiceCollection services)
{
...
services.AddOptions();
services.Configure<CustomSettings>(Configuration);
...
}
public Startup(IHostingEnvironment env, IApplicationEnvironment appEnv)
{
// Set up configuration sources.
var builder = new ConfigurationBuilder()
.SetBasePath(appEnv.ApplicationBasePath)
.AddJsonFile("appsettings.json")
.AddEnvironmentVariables()
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);
if (env.IsDevelopment())
{
// For more details on using the user secret …
Run Code Online (Sandbox Code Playgroud) c# ×9
.net ×2
.net-core ×2
asp.net-core ×2
asp.net-mvc ×2
json ×2
mediatr ×2
asp.net ×1
automapper ×1
blazor ×1
caching ×1
cqrs ×1
linq ×1
regex ×1
t-sql ×1
tpl-dataflow ×1