cofoundry-cms / cofoundry

Cofoundry is an extensible and flexible .NET Core CMS & application framework focusing on code first development

Home Page:https://www.cofoundry.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Getting Image Data

sourabhCofoundry opened this issue · comments

I have to try to get image data but I have faced this issue.
An unhandled exception occurred while processing the request.
InvalidOperationException: A second operation started on this context before a previous operation was completed. This is usually caused by different threads using the same instance of DbContext. For more information on how to avoid threading issues with DbContext, see https://go.microsoft.com/fwlink/?linkid=2097913.

public class BlogPostListViewComponent : ViewComponent
{
private readonly IContentRepository _contentRepository;
[Obsolete]
private readonly ICustomEntityRepository _customEntityRepository;
[Obsolete]
private readonly IImageAssetRepository _imageAssetRepository;
private readonly IPageResponseDataCache _pageRenderDataCache;
private readonly IVisualEditorStateService _visualEditorStateService;
private IDomainRepository _domainRepository;

    [Obsolete]
    public BlogPostListViewComponent(
        IContentRepository contentRepository,
        ICustomEntityRepository customEntityRepository,
        IImageAssetRepository imageAssetRepository,
        IPageResponseDataCache pageRenderDataCache,
        IVisualEditorStateService visualEditorStateService,
        IDomainRepository domainRepository         
        )
    {
        _customEntityRepository = customEntityRepository;
        _contentRepository = contentRepository;
        _imageAssetRepository = imageAssetRepository;
        _pageRenderDataCache = pageRenderDataCache;
        _visualEditorStateService = visualEditorStateService;
        _domainRepository = domainRepository;        
    }
    [Obsolete]
    public async Task<IViewComponentResult> InvokeAsync()
    {
        var webQuery = new SearchBlogPostsQuery();
        var query = new SearchCustomEntityRenderSummariesQuery();
        query.CustomEntityDefinitionCode = BlogPostCustomEntityDefinition.DefinitionCode;
        query.PageNumber = webQuery.PageNumber;
        query.PageSize = 6;
        if (Convert.ToInt32(TempData["pageId"]) == 0)
        {
            query.PageNumber = 1;
        }
        else
        {
            query.PageNumber = Convert.ToInt32(TempData["pageId"]);
        }
        // Publish status defaults to live, but we can use the current visual editor
        // state to allow us to show draft blog posts when previewing a draft page.
        var state = await _visualEditorStateService.GetCurrentAsync();
        query.PublishStatus = state.GetAmbientEntityPublishStatusQuery();

        // TODO: Filtering by Category (webQuery.CategoryId)
        // Searching/filtering custom entities is not implemented yet
        var entities = await _customEntityRepository.SearchCustomEntityRenderSummariesAsync(query);
        var viewModel = MapBlogPostsAsync(entities);
        var pagedResult = await _domainRepository.ExecuteQueryAsync(query);

        //// Examples of various properties and methods on IPagedResult<TResult>
        ICollection<CustomEntityRenderSummary> pagedItems = pagedResult.Items;
        int totalNumberOfItemsWithoutPaging = pagedResult.TotalItems;
        bool isFirstPage = pagedResult.IsFirstPage();
        bool isLastPage = pagedResult.IsLastPage();
        return View(viewModel);
    }

    [Obsolete]
    private async Task<PagedQueryResult<BlogPostSummary>> MapBlogPostsAsync(PagedQueryResult<CustomEntityRenderSummary> customEntityResult)
    {

        var imageAssetIds = customEntityResult
            .Items
            .Select(i => (BlogPostDataModel)i.Model)
            .Select(m => m.ThumbnailImageAssetId)
            .Distinct();         


        var imageLookup = await  _contentRepository
            .ImageAssets()
            .GetByIdRange(imageAssetIds)
            .AsRenderDetails()
            .ExecuteAsync();

        var blogPosts = new List<BlogPostSummary>();
        foreach (var customEntity in customEntityResult.Items)
        {
            var model = (BlogPostDataModel)customEntity.Model;
            var blogPost = new BlogPostSummary();
            blogPost.Title = customEntity.Title;
            blogPost.ShortDescription = model.ShortDescription;
            DateTime tempDate = customEntity.CreateDate;
            blogPost.ThumbnailImageAsset = imageLookup.GetOrDefault(model.ThumbnailImageAssetId);
            blogPost.PostDate = tempDate.ToString("MMMM dd, yyyy");
            blogPost.FullPath = customEntity.PageUrls.FirstOrDefault();
            blogPost.CategoryId = model.CategoryIds.SingleOrDefault();
            blogPosts.Add(blogPost);
            
        }
        return customEntityResult.ChangeType(blogPosts);

This is my code please check this.

Any update on it.

You need to await when you call var viewModel = MapBlogPostsAsync(entities);. See the docs you referenced:

Always await EF Core asynchronous methods immediately.

Okay Thanks it's working.