简体   繁体   English

ASP.NET MVC控制器动作-代码混乱运行

[英]ASP.NET MVC Controller actions -code runs chaotically

I have really no ideea how to ask this. 我真的不怎么想这个问题。 I have a set of async actions in my controller, one of which should populate a list through a foreach statement. 我的控制器中有一组异步操作,其中一个应通过foreach语句填充一个列表。 The strange thing is, imagine I have 10 lines of code, it runs line 1 then jumps to line 4 then line 10 then line 1 again, at one point, the list has all the items it should but then it jumps again to the first line where I declared that list and then it exists with the empty list. 奇怪的是,假设我有10行代码,它先运行第1行,然后跳到第4行,然后跳到第10行,然后再跳到第1行,在某一点,列表包含了所有它应该的项目,但随后又跳到了第一个我声明该列表的行,然后它与空列表一起存在。 Can anyone explain why this happens and how to fix it? 谁能解释为什么会这样以及如何解决?

Here is the action in question 这是有问题的动作

[HttpGet]
public async Task<ActionResult> GetBuyingListItems(string buyingListId, string vendorId, string orderId)
{
    var buyingListService = new BuyingListService(_buyingListRepository);
    ItemService itemService = new ItemService(_itemRepository);
    var vendorService = new VendorService(_vendorRepository);
    om.BuyingList = await buyingListService.GetOne(buyingListId);
    om.BuyingListItems = await buyingListService.GetBuyingListItems(buyingListId);
    om.Vendor = await vendorService.GetOne(vendorId);
    om.ItemsList = new List<OrderItemsDTO>();
    foreach (var buyingListItem in om.BuyingListItems)
    {
        var item = await itemService.GetOne(buyingListItem.ItemId);
        var orderItem = new OrderItemsDTO();
        orderItem.OrderId = orderId;
        orderItem.ItemID = item.Id;
        orderItem.Qty = buyingListItem.Qty;
        orderItem.TotalPrice = item.UnitPrice * buyingListItem.Qty;
        orderItem.Currency = om.Vendor.Currency;
        orderItem.VendorId = om.Vendor.Id;
        om.ItemsList.Add(orderItem);
    }

    return RedirectToAction("ItemListWithBuyingList", new { vendorId = vendorId, orderId = orderId, ItemsList = om.ItemsList });
}

om -> this is the model, i declered it for the entire controller. om->这是模型,我将其用于整个控制器。

Ps I have no ideea how to formulate this question in order to search for the answer so sorry if it's already been answered. 附言:我没有想法如何提出这个问题,以寻找答案,因此很抱歉,如果已经回答了。

pps this isn't the only method that runs like this but i never had this problem until now. pps,这不是像这样运行的唯一方法,但直到现在我都没有遇到过这个问题。 I always assumed it's because it runs asynchroniously. 我一直以为是因为它异步运行。

ppps this might help, if i'm debuging from the browser, on the network part, my ajax calls are being made multiple times (for instance: first time - 3 times, then 6, then 12, etc... ). ppps这可能会有所帮助,如果我正在从浏览器进行调试,那么在网络部分,我的ajax调用会被多次调用(例如:第一次-3次,然后6次,然后12次,等等...)。 I am returning false on my ajax calls and there's no href="#" in my code. 我的ajax调用返回false,代码中没有href =“#”。

Thank you. 谢谢。

What you are seeing is quite common when deubgging code that is being accessed by multiple threads. 当对多个线程正在访问的代码进行调试时,您看到的内容很常见。 It looks chaotic but it's simply down to debugging jumping across different threads. 它看起来很混乱,但是完全取决于调试跨越不同线程进行。

As a start you can use "lock" to avoid this during debugging so you can concentrate on the business logic not the threading issue. 首先,您可以在调试过程中使用“锁定”来避免这种情况,因此您可以专注于业务逻辑而不是线程问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM