Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

ExceptionFilterAttribute - Nullable object must have a value #3004


User avatar
0
bilalhaidar created

Hi, In an AppService, I am getting from Database a list of entities that implement IMustHaveTenant.

The following code is throwing an exception:

[UnitOfWork]
        public async Task<PagedResultDto<PocListDto>> GetPocs(GetPocsInput input)
        {
            // Build query
            var query = CreatePocRegisterQuery(input);

            // Apply filter if any
            query = query
                .WhereIf(
                   !string.IsNullOrEmpty(input.Filter),
                   poc =>
                       poc.FirstName.ToLower().Contains(input.Filter.Trim().ToLower()) ||
                       poc.LastName.ToLower().Contains(input.Filter.Trim().ToLower())
               );

            int pocCount;
            List<PersonofConcern> pocs;
            using (UnitOfWorkManager.Current.SetTenantId(AbpSession.TenantId.Value))
            {
                // Execute the query
                pocCount = await query.CountAsync();
                pocs = await query.OrderBy(input.Sorting).PageBy(input).ToListAsync();
            }

            // Convert to Dtos
            var pocListDtos = pocs.MapTo<List<PocListDto>>();

            return new PagedResultDto<PocListDto>(
                pocCount,
                pocListDtos);
        }

Exception:

ERROR 2017-04-23 20:10:42,681 [10   ] nHandling.AbpApiExceptionFilterAttribute - Nullable object must have a value.
System.InvalidOperationException: Nullable object must have a value.
   at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)
   at System.Nullable`1.get_Value()
   at Drc.OnlineSystems.Drc.Sms.PersonofConcernAppService.&lt;GetPocs&gt;d__7.MoveNext() in ...\PersonofConcernAppService.cs:line 69
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Abp.Threading.InternalAsyncHelper.&lt;AwaitTaskWithPostActionAndFinallyAndGetResult&gt;d__5`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Abp.Threading.InternalAsyncHelper.<AwaitTaskWithFinallyAndGetResult>d__3`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Threading.Tasks.TaskHelpersExtensions.&lt;CastToObject&gt;d__3`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__0.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Abp.WebApi.Uow.AbpApiUowFilter.<ExecuteActionFilterAsync>d__5.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Abp.WebApi.Validation.AbpApiValidationFilter.<ExecuteActionFilterAsync>d__5.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Abp.WebApi.Auditing.AbpApiAuditFilter.<ExecuteActionFilterAsync>d__4.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__2.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Abp.WebApi.Security.AntiForgery.AbpAntiForgeryApiFilter.<ExecuteAuthorizationFilterAsync>d__10.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Abp.WebApi.Authorization.AbpApiAuthorizeFilter.<ExecuteAuthorizationFilterAsync>d__7.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Web.Http.Controllers.AuthenticationFilterResult.<ExecuteAsync>d__0.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Web.Http.Controllers.ExceptionFilterResult.<ExecuteAsync>d__0.MoveNext()
ERROR 2017-04-23 20:11:34,029 [23   ] nHandling.AbpApiExceptionFilterAttribute - Nullable object must have a value.

4 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Can FirstName or LastName might be null in your database ? By the way which is the line 69 ? It might help to understand problem as well.

    Thanks.

  • User Avatar
    0
    bilalhaidar created

    Hi Ismail,

    At line 59, I am using

    using (UnitOfWorkManager.Current.SetTenantId(AbpSession.TenantId.Value))
    

    The data I am retrieving must have a Tenant. But, I need to access it from Host.

    Thanks Bilal

  • User Avatar
    0
    bilalhaidar created

    Hi Ismail, Problem solved.

    I am using this line of code:

    using (UnitOfWorkManager.Current.SetTenantId(AbpSession.TenantId.Value))
    

    When a user is under host context, then TenantId is Null, hence Value property cannot be accessed. So I just changed the line to:

    using (UnitOfWorkManager.Current.SetTenantId(AbpSession.TenantId))
    

    Now things work smooth.

    Thanks for hinting me in the right direction.

    Regards Bilal

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Thanks for the feedback :)