我刚刚安装了 Visual studio2022 并使用 Razor 页面创建了一个项目 .NET6 Web 应用程序。但我发现即使对 Index.cshtml 进行很小的更改(屏幕截图中的“TEST”到“TEST1”),热重新加载也不起作用,但错误弹出窗口显示“已进行无法编译的编辑。所以我不得不重新-构建项目以查看更改。但是当我使用 start 而不进行调试时,它会以某种方式工作。
有人可以帮忙解决这个问题吗?
我正在使用 .net core3 构建 API。
在控制器中,我曾经使用静态类从 sql 获取信息列表或从 web 服务获取一些结果。
但我认为使用依赖注入可能会更好。所以我修改了代码以使用DI。
但我不确定这些改变实际上是否更好,如果是的话,为什么?
我能否了解它们有何不同以及哪一个更好?
原始代码:
public class TestController : Controller
{
[HttpGet("{id}")]
public async Task<IActionResult> Product(int id)
{
///SearchManager is static class, static GetProductCodesFromSearch method get list of productcode from sql
List<string> productCodes = SearchManager.GetProducCodeFromSearch();
//ProductManager is static class, GetProducts mehtod make a webrequest as parallel to some webservice get list of product
List<Product> products = await ProductManager.GetProducts();
....
return Json(new { success = true, message = "Ok" });
}
} …Run Code Online (Sandbox Code Playgroud) 您好,我需要一些关于 Task.WhenAll() 运行的异步方法的建议。它是 dotnet core 3 项目,上下文和服务被添加为 AddScoped。
当我运行 DoWorksAysnc 方法时,我总是遇到一个问题“在上一个操作完成之前,在此上下文上启动了第二个操作。这通常是由使用同一 DbContext 实例的不同线程引起的。有关如何避免 DbContext 线程问题的更多信息”
我知道 DbContext 不是线程安全的。但我是否可以同时运行 DowksAsync 方法..?
谢谢,
public async Task DoWorksAsync(List<int> ids)
{
if (ids.Count > 0)
{
var listOfTasks = new List<Task>();
foreach(var id in ids)
{
var task = Task.Run(async () => await UpdateDataById(id));
listOfTasks.Add(task);
}
await Task.WhenAll(listOfTasks);
}
}
Run Code Online (Sandbox Code Playgroud)
UpdateDataById 方法是
public async Task UpdateDataById (int id)
{
try
{
//Get products from webservice
List<Product> products = await _searchService.GetItemsByIdAsync(id);
await _dataService.CreateTableIfNotExist(id); //Method inside …Run Code Online (Sandbox Code Playgroud) c# concurrency multithreading entity-framework thread-safety
c# ×2
.net-6.0 ×1
.net-core ×1
asp.net-core ×1
concurrency ×1
hot-reload ×1
razor-pages ×1
static ×1