{"record":{"id":"7a802643461e7f2d","repo":"aspnetboilerplate/aspnetboilerplate","slug":"session-tenantid-is-null-possible-problems-no-us","errorCode":null,"errorMessage":"Session.TenantId is null! Possible problems: No user logged in or current logged in user in a host user (TenantId is always null for host users).","messagePattern":"Session\\.TenantId is null! Possible problems: No user logged in or current logged in user in a host user \\(TenantId is always null for host users\\)\\.","errorType":"exception","errorClass":"AbpException","httpStatus":null,"severity":"error","filePath":"src/Abp/Runtime/Session/AbpSessionExtensions.cs","lineNumber":35,"sourceCode":"            {\n                throw new AbpException(\"Session.UserId is null! Probably, user is not logged in.\");\n            }\n\n            return session.UserId.Value;\n        }\n\n        /// <summary>\n        /// Gets current Tenant's Id.\n        /// Throws <see cref=\"AbpException\"/> if <see cref=\"IAbpSession.TenantId\"/> is null.\n        /// </summary>\n        /// <param name=\"session\">Session object.</param>\n        /// <returns>Current Tenant's Id.</returns>\n        /// <exception cref=\"AbpException\"></exception>\n        public static int GetTenantId(this IAbpSession session)\n        {\n            if (!session.TenantId.HasValue)\n            {\n                throw new AbpException(\"Session.TenantId is null! Possible problems: No user logged in or current logged in user in a host user (TenantId is always null for host users).\");\n            }\n\n            return session.TenantId.Value;\n        }\n\n        /// <summary>\n        /// Creates <see cref=\"UserIdentifier\"/> from given session.\n        /// Returns null if <see cref=\"IAbpSession.UserId\"/> is null.\n        /// </summary>\n        /// <param name=\"session\">The session.</param>\n        public static UserIdentifier ToUserIdentifier(this IAbpSession session)\n        {\n            return session.UserId == null\n                ? null\n                : new UserIdentifier(session.TenantId, session.GetUserId());\n        }\n    }\n}","sourceCodeStart":17,"sourceCodeEnd":53,"githubUrl":"https://github.com/aspnetboilerplate/aspnetboilerplate/blob/2323c13a152aa0ebfb37af3830f687d7f5b53795/src/Abp/Runtime/Session/AbpSessionExtensions.cs#L17-L53","documentation":"AbpSessionExtensions.GetTenantId() is a convenience extension that returns the current tenant Id as a non-nullable int. ABP throws this AbpException because TenantId is nullable (int?) and is null both when no user is logged in and when the current user is the host (tenant-less) user, for which a tenant Id has no meaning. The library refuses to guess a default (like 0 or 1) so callers must explicitly handle the null case.","triggerScenarios":"Calling session.GetTenantId() when session.TenantId is null: (1) before a user logs in, (2) from code executing on the host side (no tenant), (3) in a background job/thread where IAbpSession is not populated (no ambient user context), (4) in unit tests with a stub session that has no TenantId set.","commonSituations":"Application services called by anonymous users; code scheduled via background workers or Hangfire without session override; multi-tenant apps where an admin operating at host level invokes tenant-specific logic; tests using NullAbpSession or DefaultSession without Use(tenantId, userId).","solutions":["Check session.TenantId.HasValue before calling GetTenantId and branch on the null case","Use session.GetTenantId() only after authenticating the user, or use session.GetUserId()/GetMultiTenancySideCode appropriately","In background jobs, inject and call IAbpSession.Use(tenantId, userId) scope to supply the tenant context","In tests, wrap calls in a session 'Use' scope or mock IAbpSession with TenantId set"],"exampleFix":"// before\nint tenantId = _abpSession.GetTenantId();\n\n// after\nif (_abpSession.TenantId.HasValue)\n{\n    int tenantId = _abpSession.GetTenantId();\n    // ...\n}\nelse\n{\n    // handle anonymous or host-user context\n}","handlingStrategy":"type-guard","validationCode":"if (session.TenantId.HasValue) { var tenantId = session.GetTenantId(); }","typeGuard":"bool hasTenant = session is IAbpSession s && s.TenantId.HasValue;","tryCatchPattern":"try { tenantId = session.GetTenantId(); } catch (AbpException) { /* anonymous/host context: use default or skip */ }","preventionTips":["Check TenantId.HasValue before calling Get* extensions","Use IAbpSession.Use(tenantId, userId) scopes in background jobs and tests","Never assume a tenant context in host-side or anonymous code paths"],"tags":["session","multi-tenancy","null-reference"],"backgroundTag":"authentication-required","analyzedSha":"2323c13a152aa0ebfb37af3830f687d7f5b53795","analyzedAt":"2026-09-08T08:00:50.638Z","contentChangedAt":"2026-09-08T08:00:50.638Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}