Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I don't understand where I missed async\await

My code returns Task<List<...>>.
There is a type conversion error from Task<List<...>> to List<...>.

Tell me, please, where I did not finish await ?

    public async Task<List<DepartamentsResponse>> Handle(GetDepartmentsRequest token, CancellationToken cancellationToken)
    {
        var departments = await _departmentApiClient.GetReception(token.accessToken, OdpgDepartmentType.Reception);

        var result = departments.ConvertAll(async d => new DepartamentsResponse
        {
            FederalDistrict = GetFederalDistrictCode(d.FederalRegion.DistrictCode),
            SubjectName = d.Name,
            Supervisor = GetDirector(d.Users.Where(u => u.InOdpgRole(OdpgUserRole.Director)).FirstOrDefault()),
            ContactsSupervisor =  GetContacts(d.Users.Where(u => u.InOdpgRole(OdpgUserRole.Director)).FirstOrDefault()),
            Schedule = "C 9:00 18:00",
            ReceptionContacts = await GetAddressAsync(d.Addresses.FirstOrDefault(d => d.AddressType == DepartmentAddressType.Reception), token)

        });
        return result;
    }

    private async Task<string> GetAddressAsync(DepartmentAddressDto? address, GetDepartmentsRequest token)
    {
        if (address != null)
        {
            var fullAddress = await  _fiasApiClient.GetFullAddress(token.accessToken,
                new ESOG.Fias.Api.Model.GetFullAddressRequest
                { BaseAddressId = address.FiasId, Building = address.Building, Room = address.Room });
            //var res = JsonConvert.DeserializeObject<DepartmentAddress>(fullAddress);
            return fullAddress;
        }
        return "";
    }

GetFederalDistrictCode, GetDirector, GetContacts – these methods are not asynchronous

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

It should just return a List<>, not Task<List<>>

>Solution :

Your call to departments.ConvertAll is returning a List<Task<DepartamentsResponse>>, but you want a Task<List<DepartamentsResponse>>.

Look at the Task.WhenAll method for how you would convert a collection of Tasks to a single Task that returns a collection. Then await that single Task and build your final list.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading