question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Saving members without HttpContext throwing errors

See original GitHub issue

When 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

  1. Either save a member or adding a member to a role when there’s no HttpContext. We do it via Hangfire
  2. Make sure that you raise events when saving the member
  3. Note that when adding members to a role it will always raise events and you can therefore not get around the error
  4. 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:closed
  • Created 3 years ago
  • Reactions:3
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
gmanrodtcommented, Apr 15, 2021

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(); } }

0reactions
gmanrodtcommented, Apr 16, 2021

Yes, I saw that, Shannon, thanks. Unfortunately, we are in a position where we cannot upgrade. I appreciate you pointing that out, however.

Read more comments on GitHub >

github_iconTop Results From Across the Web

setting HttpContext.Current.User throws null reference ...
The reason you can't set it is most likely that the request has finished. Setting the User property results in ASP.NET trying to...
Read more >
HttpContext.Current.Session Object Reference Exception
cs class I do not get this exception. Can someone please explain why trying to create a session variable like this (above) will...
Read more >
ASP.NET Error Handling
An exception is thrown from an area of code where a problem has ... the ArgumentNullException class, do not implement additional members.
Read more >
Mocking the HttpContext Session object in ASP.NET Core 2.0
My functions have an optional parameter which takes an instance of an ISession object. If one is not passed as an argument then...
Read more >
Can't able to read the httpcontext object in catch block ...
Don't use middleware, use a request filter instead. Middleware should be used to handle exceptions. It should have always consider the ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found