Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Cornelis 12 posts 113 karma points
    Aug 12, 2021 @ 14:08
    Cornelis
    0

    Adding a section to a user group throws an exception with message "Request not available in this context"

    Via code I try to add a section to a usergroup:

            try
            {
                var adminUserGroup = applicationContext.Services.UserService.GetUserGroupByAlias("admin");
                adminUserGroup.AddAllowedSection("media");
                applicationContext.Services.UserService.Save(adminUserGroup);
            }
            catch (Exception exception)
            {
                LoggerResolver.Current.Logger.Error<RegisterMfa>("error", exception);
            }
    

    This throws an exception with the error message: "Request not available in this context" yet the section is added to the user group successfully. So the exception is not breaking stuff yet it is quite ennoying and weird.

    This is the stacktrace:

    at Umbraco.Core.HttpContextExtensions.GetCurrentRequestIpAddress(HttpContextBase httpContext)
       at Umbraco.Core.Auditing.AuditEventHandler.get_PerformingIp()
       at Umbraco.Core.Auditing.AuditEventHandler.OnSavedUserGroupWithUsers(IUserService sender, SaveEventArgs`1 saveEventArgs)
       at Umbraco.Core.Events.EventDefinition`2.RaiseEvent()
       at Umbraco.Core.Events.ScopeEventDispatcher.ScopeExitCompleted()
       at Umbraco.Core.Events.ScopeEventDispatcherBase.ScopeExit(Boolean completed)
       at Umbraco.Core.Scoping.Scope.<>c__DisplayClass70_0.<RobustExit>b__1()
       at Umbraco.Core.Scoping.Scope.TryFinally(Int32 index, Action[] actions)
       at Umbraco.Core.Scoping.Scope.TryFinally(Int32 index, Action[] actions)
       at Umbraco.Core.Scoping.Scope.RobustExit(Boolean completed, Boolean onException)
       at Umbraco.Core.Scoping.Scope.DisposeLastScope()
       at Umbraco.Core.Scoping.Scope.Dispose()
       at Umbraco.Core.Persistence.UnitOfWork.ScopeUnitOfWork.DisposeResources()
       at Umbraco.Core.DisposableObjectSlim.Dispose(Boolean disposing)
       at Umbraco.Core.DisposableObjectSlim.Dispose()
       at Umbraco.Core.Services.UserService.Save(IUserGroup userGroup, Int32[] userIds, Boolean raiseEvents)
       at Register.ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext) in D:\...\Register.cs:line 27
    

    It seems to try to get an IpAddress from (I assume the) request context but this seems to fail for some reason.

    Is this the way to add sections to usergroups? And if so, how to prevent the exception to be thrown on a proper way?

Please Sign in or register to post replies

Write your reply to:

Draft