Initialize member in IApplicationEventHandler and receive Request is not available in this context
Hi All
I am newbie to Umbraco, I want to set up a default member during application start. I have the following code invoked from OnApplicationStarted(UmbracoApplicationBase umbracoApplication,
ApplicationContext applicationContext) method.
private void InitializeMembers(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
string username = "[email protected]";
var memberService = applicationContext.Services.MemberService;
int total;
var existingMember = memberService.FindByUsername(username, 0, 1, out total, Umbraco.Core.Persistence.Querying.StringPropertyMatchType.Exact).FirstOrDefault();
if (existingMember == null)
{
var newMember = memberService.CreateWithIdentity(username, username, "Test1234$", "member", true);
memberService.Save(newMember);
}
}
The aforementioned code works fine in my controller, but not in IApplicationEventHandler, and I keep getting Request is not available in this context, however the user was created successfully.
I think it's to do with the "member" memberType passed on the CreateWithIdentity call. However, you'll hit other issues using this version of memberService, namely the password will need to be passed encrypted. I tend to do my membership as follows...(shortened for simplicity, and changed for your example)
public class Bootstrap : ApplicationEventHandler
{
protected override void ApplicationStarted(UmbracoApplicationBase app, ApplicationContext ctx)
{
InitializeMembers(app, ctx);
}
private void InitializeMembers(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
string username = "[email protected]";
var memberService = applicationContext.Services.MemberService;
var memberTypeService = applicationContext.Services.MemberTypeService;
var memberType = memberTypeService.Get("Member");
if (memberService.GetByEmail(username) != null)
{
return;
}
try
{
var member = memberService.CreateMemberWithIdentity(username, username, memberType);
// to set the password using Umbraco's hashing you need to call this method
memberService.SavePassword(member, "Test1234$");
member.IsApproved = true;
memberService.Save(member);
}
catch (Exception ex)
{
// Log error
return;
}
}
}
Thanks for your help. The issue doesn't seem coming from the "member" memberType passed on the CreateWithIdentity call.
I tried CreateMember method with MemberService, and it threw the same exception.
Having said that, by using your code sample with try and catch, obviously I just can avoid the YSOD, but problem still exists.
2019-01-30 22:25:18,335 [P7852/D3/T15] ERROR Umbraco.Core.CoreBootManager - An error occurred running OnApplicationStarted for handler UmbracoDemo2.Logic.Application.MyApplicationEventHandler
System.Web.HttpException (0x80004005): Request is not available in this context
at System.Web.HttpContext.get_Request()
at System.Web.HttpContextWrapper.get_Request()
at Umbraco.Core.HttpContextExtensions.GetCurrentRequestIpAddress(HttpContextBase httpContext)
at Umbraco.Core.Auditing.AuditEventHandler.get_PerformingIp()
at Umbraco.Core.Auditing.AuditEventHandler.OnSavedMember(IMemberService sender, SaveEventArgs`1 saveEventArgs)
at Umbraco.Core.Events.TypedEventHandler`2.Invoke(TSender sender, TEventArgs e)
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.MemberService.CreateMemberWithIdentity(String username, String email, String name, String passwordValue, IMemberType memberType, Boolean isApproved)
at Umbraco.Core.Services.MemberService.CreateMemberWithIdentity(String username, String email, IMemberType memberType)
at UmbracoDemo2.Logic.Application.MyApplicationEventHandler.InitializeMembers(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext) in C:\dev\XXX\UmbracoDemo2\Logic\Application\MyApplicationEventHandler.cs:line 135
at UmbracoDemo2.Logic.Application.MyApplicationEventHandler.OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext) in C:\dev\XXX\UmbracoDemo2\Logic\Application\MyApplicationEventHandler.cs:line 37
at Umbraco.Core.CoreBootManager.<Complete>b__38_0(IApplicationEventHandler x)
I should've tested without the try/catch block first, sorry!
I would put the try catch back when you're happy the issue has been resolved but try this code.
private void InitializeMembers(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
string username = "[email protected]";
var memberService = ApplicationContext.Current.Services.MemberService;
var memberTypeService = ApplicationContext.Current.Services.MemberTypeService;
var memberType = memberTypeService.Get("Member");
if (memberService.GetByEmail(username) != null)
{
return;
}
var member = memberService.CreateMemberWithIdentity(username, username, memberType);
// to set the password using Umbraco's hashing you need to call this method
memberService.SavePassword(member, "Test1234$");
member.IsApproved = true;
memberService.Save(member);
}
Initialize member in IApplicationEventHandler and receive Request is not available in this context
Hi All
I am newbie to Umbraco, I want to set up a default member during application start. I have the following code invoked from OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext) method.
The aforementioned code works fine in my controller, but not in IApplicationEventHandler, and I keep getting Request is not available in this context, however the user was created successfully.
Is there anything I missed in my code?
Appreciate for your help.
Had anyone experienced this issue? Can anyone give an idea?
Appreciate for your help.
Hi,
I think it's to do with the "member" memberType passed on the CreateWithIdentity call. However, you'll hit other issues using this version of memberService, namely the password will need to be passed encrypted. I tend to do my membership as follows...(shortened for simplicity, and changed for your example)
HTH,
David
Hi David
Thanks for your help. The issue doesn't seem coming from the "member" memberType passed on the CreateWithIdentity call.
I tried CreateMember method with MemberService, and it threw the same exception.
Having said that, by using your code sample with try and catch, obviously I just can avoid the YSOD, but problem still exists.
Hi Javafun,
As per my comment on Slack, this is code that has been used in the past to fake a context in the start up events.
If you create that method in your class, and call it as the first line in your InitializeMembers method it "might" solve your issue but I'm not sure.
Nik
Lol!
I should've tested without the try/catch block first, sorry! I would put the try catch back when you're happy the issue has been resolved but try this code.
David
is working on a reply...