Do you get the same behaviour if you replace ApplicationSettings.ScheduledJobUsername with the username you are logged in as (that works when you run the job manually)?
Yes, same behaviour. Actually its with any user.
You could try running the job as a specific role:
Hi Per, thanks for that post. That is exactly what i did to work around the problem, however, it is not ideal and it is still not clear why the other approach is not working.
I have finally found a problem - application pool running with more than one worker processes (in my instance I had two worker processes). Once I set worker processes limit to one everything started to work again.
Our scheduled jobs started failing since yesterday with the following error message:
CustomUpdate.Execute - System.NullReferenceException: Object reference not set to an instance of an object.
at System.Web.Security.Roles.GetRolesForUser(String username)
at EPiServer.Security.PrincipalInfo.CreatePrincipal(String username)
The scheduled job uses anonymous execution and logs in programmatically using the following call:
if (PrincipalInfo.CurrentPrincipal.Identity.Name == string.Empty)
PrincipalInfo.CurrentPrincipal = PrincipalInfo.CreatePrincipal(ApplicationSettings.ScheduledJobUsername);
I have put in some more logging around PrincipalInfo.CreatePrincipal call which is in Episerver.Security and noticed that PrincipalInfo.CreatePrincipal calls System.Web.Security.Roles.GetRolesForUser(username) and Roles.GetRolesForUser(username) returns an empty string array.
There were no changes code wise or on the server (updates, etc).
I checked that the user name used to run the task is in the database and has roles associated with it.
I checked that applicationname is set up correctly and is associated with the user
If i run the job manually using the same user it executes with no issues (i know there is a difference between running the job manually and using the scheduler)
I also tried creating a new user, that didn’t work either.
Has anyone come across the same or similar issue? Any thoughts how to resolve this issue? Thanks in advance.