public Department GetOrgUnitByDepartmentId(long departmentId, Guid SecurityToken) { if (!m_cache.TryGetValue(departmentId + DepartmentCacheKey, out Department cachedDepartment)) { var department = new Department(); var depUrl = m_configurationProvider.FullDepartmentServiceUrl; var departmentsResponse = m_wcfClient.CallServiceAndReturnResult <IDepartmentService, OrganizationalUnits.Interface.DTOs.DepartmentResponse>(t => t.GetDepartment(departmentId), depUrl, SecurityToken); if (departmentsResponse == null) { m_log.Error($"Null returned for GetDepartmentId WCF call that used department id - {department.DepartmentId}"); throw new Exception("Failed to load organizational unit"); } var departmentList = departmentsResponse.Departments; var departmentItem = departmentList?.FirstOrDefault(); if (departmentList.Count == 0) { m_log.Error($"No departments returned for the department id - {departmentId} , please validate this department id."); throw new Exception("No units found for the department"); } department.DepartmentId = departmentId; department.DepartmentName = departmentItem.DepartmentName; department.DepartmentUnitGid = departmentItem.Unitgid; var locationList = new List <LocationListItem>(); m_log.Debug($"Number of locations returned for department id {departmentId} = {departmentItem.DepartmentLocation.Count}"); locationList.AddRange(departmentItem.DepartmentLocation.Select(loc => new LocationListItem { LocationId = loc.LocalizationId, LocationDisplayName = loc.LocalizationName, UnitGid = loc.Unitgid })); department.LocationList = locationList; var sectionList = GetSectionListByDepartmentId(departmentId, SecurityToken); foreach (var section in sectionList) { section.WardList = GetWardListBySectionId(section.SectionId, SecurityToken); } department.SectionList = sectionList; department.WardList = GetWardListByDepartmentId(departmentId, SecurityToken); department.OPDList = GetOPDListByDepartmentId(departmentId, SecurityToken); var entryOptions = new MemoryCacheEntryOptions().SetPriority(CacheItemPriority.NeverRemove); m_cache.Set(departmentId + DepartmentCacheKey, department, entryOptions); return(department); } return(cachedDepartment); }