Saving members without HttpContext throwing errors
See original GitHub issueWhen saving members og adding roles to members throws an error when there’s no HttpContext available.
Umbraco version
I am seeing this issue on Umbraco version: 8.5.2 But it does not look like the issue have been fixed in newer versions
Reproduction
Bug summary
The save of the member or the adding of the role works like it should but one of the events afterwards throws and error. More specific PerformingIp property on AuditEventsComponent.cs throws a null pointer exception. In the Exception below the error is thrown when trying to add user to roles:
InnerException: System.NullReferenceException: Object reference not set to an instance of an object. at Umbraco.Core.Compose.AuditEventsComponent.get_PerformingIp() in D:\a\1\s\src\Umbraco.Core\Compose\AuditEventsComponent.cs:line 69 at Umbraco.Core.Compose.AuditEventsComponent.OnAssignedRoles(IMemberService sender, RolesEventArgs args) in D:\a\1\s\src\Umbraco.Core\Compose\AuditEventsComponent.cs:line 109 at Umbraco.Core.Events.EventDefinition2.RaiseEvent() in D:\a\1\s\src\Umbraco.Core\Events\EventDefinition.cs:line 69 at Umbraco.Core.Events.QueuingEventDispatcher.ScopeExitCompleted() in D:\a\1\s\src\Umbraco.Core\Events\QueuingEventDispatcher.cs:line 23 at Umbraco.Core.Events.QueuingEventDispatcherBase.ScopeExit(Boolean completed) in D:\a\1\s\src\Umbraco.Core\Events\QueuingEventDispatcherBase.cs:line 337 at Umbraco.Core.Scoping.Scope.<>c__DisplayClass71_0.<RobustExit>b__1() in D:\a\1\s\src\Umbraco.Core\Scoping\Scope.cs:line 434 at Umbraco.Core.Scoping.Scope.TryFinally(Int32 index, Action[] actions) in D:\a\1\s\src\Umbraco.Core\Scoping\Scope.cs:line 472 at Umbraco.Core.Scoping.Scope.TryFinally(Int32 index, Action[] actions) in D:\a\1\s\src\Umbraco.Core\Scoping\Scope.cs:line 476 at Umbraco.Core.Scoping.Scope.RobustExit(Boolean completed, Boolean onException) in D:\a\1\s\src\Umbraco.Core\Scoping\Scope.cs:line 420 at Umbraco.Core.Scoping.Scope.Dispose() in D:\a\1\s\src\Umbraco.Core\Scoping\Scope.cs:line 363 at Umbraco.Core.Services.Implement.MemberService.AssignRoles(String[] usernames, String[] roleNames) in D:\a\1\s\src\Umbraco.Core\Services\Implement\MemberService.cs:line 1031 at System.Web.Security.Roles.AddUserToRoles(String username, String[] roleNames)
Steps to reproduce
- Either save a member or adding a member to a role when there’s no HttpContext. We do it via Hangfire
- Make sure that you raise events when saving the member
- Note that when adding members to a role it will always raise events and you can therefore not get around the error
- The error occurs
Expected result
The code should be independent of a HttpContext both when raising events and when you are not.
Actual result
The member is saved and/or the role is added to the member, but the Umbraco Core throws an error.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:3
- Comments:10 (6 by maintainers)
I ended up just injecting an IScopeProvider and the IMemberGroupRepository, and going that route. That bypassed the audit events as well, and my members are now in their roles.
private void AssignRole(int memberId, string role) { using (var scope = _scopeProvider.CreateScope()) { scope.WriteLock(Constants.Locks.MemberTree); _memberGroupRepository.AssignRoles(new[] { memberId }, new[] { role }); scope.Complete(); } }
Yes, I saw that, Shannon, thanks. Unfortunately, we are in a position where we cannot upgrade. I appreciate you pointing that out, however.