Problem in casting Generic.List to System.Threadin

2019-07-09 10:53发布

I am trying to create mock of IMemoryCache.TryGetValue method but it returns the following error when it hit cache.Get(cacheKey):

Unable to cast object of type 'System.Collections.Generic.List1[ConnectionsModel]' to type 'System.Threading.Tasks.Task1[System.Collections.Generic.List`1[ConnectionsModel]

Here is the mock:

 private static Mock<IMemoryCache> ConfigureMockCacheWithDataInCache(List<ConnectionsModel> auth0ConnectionsResponse)
 {
        object value = auth0ConnectionsResponse;
        var mockCache = new Mock<IMemoryCache>();
        mockCache
            .Setup(x => x.TryGetValue(
                It.IsAny<object>(), out value
            ))
            .Returns(true);
        return mockCache;
    }

Here is the test method:

var connectionList = new List<ConnectionsModel>();
var connectionsModel= new ConnectionsModel()
{
     id = "1",
    name = "abc",
    enabled_cons = new List<string>() { "test" }
};
connectionList.Add(connectionsModel);
var mockObject = ConfigureMockCacheWithDataInCache(connectionList);
var sut = new MyService(mockCache.Object);
// Act
var result = await sut.GetConnection(_clientId);

and here is the service that it hits:

public async Task<ConnectionsModel> GetConnection(string clientId)
{
    var connections = await _cacheService.GetOrSet("cacheKey", ()=> CallBack());
    var connection = connections.FirstOrDefault();
    return connection;
}
private async Task<List<ConnectionsModel>> CallBack()
{
    string url = url;
    _httpClient.BaseAddress = new Uri(BaseUrl);
    var response = await _httpClient.GetAsync(url);
    response.EnsureSuccessStatusCode();
    return await response.Content.ReadAsAsync<List<ConnectionsModel>>();
}

and the cache extension method:

   public static T GetOrSet<T>(this IMemoryCache cache, string cacheKey, Func<T> getItemCallback, double cacheTimeout = 86000) where T : class
    {
        T item = cache.Get<T>(cacheKey);
        if (item == null)
        {
            item = getItemCallback();
            cache.Set(cacheKey, item, DateTime.Now.AddSeconds(cacheTimeout));
        }
        return item;
    }

After this line T item = cache.Get<T>(cacheKey); I am getting the above mentioned exception. How can I fix this?

2条回答
Rolldiameter
2楼-- · 2019-07-09 11:37

In your extension method, you're calling getItemCallback() without awaiting it. That lambda calls your Callback method in your service which is async, so this is async as well. As a result, you're setting item to Task<List<ConnectionsModel>> and trying to return that as a List<ConnectionsModel>.

item = await getItemCallback();
查看更多
叼着烟拽天下
3楼-- · 2019-07-09 11:50

Consider creating an additional overload of the extension to allow for asynchronous API to be used

public static async Task<T> GetOrSet<T>(this IMemoryCache cache, string cacheKey, Func<Task<T>> getItemCallback, double cacheTimeout = 86000) where T : class
{
    T item = cache.Get<T>(cacheKey);
    if (item == null)
    {
        item = await getItemCallback();
        cache.Set(cacheKey, item, DateTime.Now.AddSeconds(cacheTimeout));
    }
    return item;
}
查看更多
登录 后发表回答