简体   繁体   English

无法等待ASP.NET(4.6)MVC5项目中的方法

[英]Unable to await method in ASP.NET (4.6) MVC5 project

I'm currently seeing a problem whereby my await method is just hanging, and causing the response to just hang, not doing anything until I kill the request. 我目前看到一个问题,我的await方法只是挂起,并且导致响应挂起,直到我终止请求后才做任何事情。 This is evident in both Chrome debug tools and Fiddler. 这在Chrome调试工具和Fiddler中都显而易见。

I have the following API action defined: 我定义了以下API操作:

[Route("state/{stateCode}")]
[LogApiCallFilter]
public async Task<IList<MapPlaceDTO>> GetWithinState(string stateCode)
{

    //    
    // Additional code truncated for SO
    // Via debugging I know that the 'state' variable below is correct
    //

    IList<Place> places = await _placeManager.GetPlacesInState(state);

    // Instantiate the list of places.
    IList<MapPlaceDTO> mapPlaces = new List<MapPlaceDTO>();

    // Iterate through the places and add to the map place list
    foreach (Place place in places)
    {
        mapPlaces.Add(MapPlaceDTO.FromPlace(place));
    }

    return mapPlaces;

}

When I step through that code in debug mode for a unit test for the GetWithinState action, the IList<Place> places = await _placeManager.GetPlacesInState(state); 当我在调试模式下逐步执行该代码以进行GetWithinState操作的单元测试时, IList<Place> places = await _placeManager.GetPlacesInState(state); method runs without exception, however I am not able to hover over the places variable to inspect it, nothing happens. 方法运行无一例外,但是我无法将鼠标悬停在places变量上进行检查,没有任何反应。 Nor can I add it to the watch list, I get the following message: error CS0103: The name 'places' does not exist in the current context 我也无法将其添加到监视列表,但会收到以下消息: error CS0103: The name 'places' does not exist in the current context

Interestingly however, if I run the exact same code within a "PlaceManager" unit test, outside of the Web API project, the test runs fine, and I can inspect the places variable. 但是,有趣的是,如果在Web API项目外部的“ PlaceManager”单元测试中运行完全相同的代码,则该测试运行良好,并且我可以检查places变量。

[Fact(DisplayName = "Can_Get_All_Places_Within_State")]
[Trait("Category", "Place Manager")]
public async Task Can_Get_All_Places_Within_State()
{

    State state = new State()
    {
        ShortName = "VIC",
        Name = "Victora",
        CountryCode = "AU"
    };

    IList<Place> places = await _placeManager.GetPlacesInState(state);

    Assert.NotNull(places);
    Assert.True(places.Count > 0);

}

This is the code that runs within the PlaceManager.GetPlacesInState method: 这是在PlaceManager.GetPlacesInState方法中运行的代码:

public async Task<IList<Place>> GetPlacesInState(State state)
{
    if (state == null)
    {
        throw new ArgumentNullException("state", "The 'state' parameter cannot be null.");
    }

    // Build the cache key
    string cacheKey = String.Format("places_state_{0}", state.Id);

    // Get the places from the cache (if they exist)
    IList<Place> places = CacheManager.GetItem<IList<Place>>(cacheKey);

    // Get the places from the database.
    if (places == null)
    {
        // Get the places from the database
        places = await _repository.Find(i => i.State.ToLower() == state.ShortName.ToLower() && i.Country.ToLower() == state.CountryCode.ToLower());

        // If there are places, then add to the cache for next time
        if (places != null && places.Count > 0)
        {
            CacheManager.AddItem(cacheKey, places);
        }
    }

    // return the places
    return (places != null ? places : new List<Place>());
}

Does anyone have any idea why this may be occurring within the API method, but is working fine in unit tests? 有谁知道为什么在API方法中可能会发生这种情况,但是在单元测试中工作正常吗?

As mentioned by Alexei Levenkov above, I was causing a deadlock using the getDataAsync().Result code. 正如上面的Alexei Levenkov所提到的,我正在使用getDataAsync().Result代码导致死锁。

While mapping my Place object to a MapPlaceDTO object, the Place object has a get property to load the place type, which would call an async function in the following way: 在将我的Pl​​ace对象映射到MapPlaceDTO对象时,Place对象具有get属性以加载位置类型,该属性将通过以下方式调用异步函数:

public PlaceType PlaceType
{
    get
    {
        return getPlaceType().Result;
    }
}

Once I removed that property and just called the GetPlaceType method directly using the await keyword, everything started working correctly. 一旦删除了该属性并使用await关键字直接调用了GetPlaceType方法,一切便开始正常工作。 Thanks Alexei! 谢谢阿列克谢!

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

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