Loading ...

New Class In Acumatica Pximpersonationcontext

Hello everybody,

here I want to document new scope in Acumatica: PXImpersonationContext. 

As often is the case try to look at presented code:

var thr = new Thread(
                                   () =>
                                   {
                                       try
                                       {
                                           using (new PXImpersonationContext(PX.Data.Update.PXInstanceHelper.ScopeUser))
.

.

.

}
                            foreach (var thread in threads)
                            {
                                lock (thisLock)
                                {
                                    thread.Start();
                                }
                            }
 
                            foreach (var thread in threads)
                            {
                                thread.Join();
                            }

It does the following: creates threads, inside of threads users some logic for persistance to database through graphs and then waits for joining threads.

Recently I've had following problem. I've created processing screen that worked great in manual mode, but absoultely refused to work in Automation schedule mode. Refused means thread was crashed without any exception message.

Adding PXImpersonationContext solved the issue for me.

Now you may wonder, how PXImpersonationContext achieves it?

Let's consult with JetBrains decompiler:

public class PXImpersonationContext : IDisposable
{
  private IPrincipal a;
  private readonly WindowsImpersonationContext b;
 
  public PXImpersonationContext()
    : this(PXDatabase.Companies.Length != 0 ? "sys@" + PXDatabase.Companies[0] : "sys")
  {
  }
 
  public PXImpersonationContext(string userName)
    : this(userName, new string[0])
  {
  }
 
  public PXImpersonationContext(string userName, string[] roles)
  {
    WindowsIdentity windowsIdentity = (WindowsIdentitynull;
    IPrincipal principal = (IPrincipalnew GenericPrincipal((IIdentitynew GenericIdentity(userName), roles);
    IdentitySection section = (IdentitySection) WebConfigurationManager.GetSection("system.web/identity");
    if (section != null && section.Impersonate && HttpContext.Current != null)
    {
      HttpWorkerRequest service = (HttpWorkerRequest) ((IServiceProvider) HttpContext.Current).GetService(typeof (HttpWorkerRequest));
      if (service != null)
      {
        IntPtr userToken = service.GetUserToken();
        if (userToken != IntPtr.Zero)
          windowsIdentity = new WindowsIdentity(userToken);
      }
    }
    this.a = PXContext.PXIdentity.User;
    PXContext.PXIdentity.User = principal;
as you can see from decompiled code sample, PXImpersonationContext uses user name "sys@currentlyloggedcompany" then it tries to create WindowsIdentity user and then uses that user for processing your staff.
Pretty cool idea, huh?
One more addition.
PX.Data.Update.PXInstanceHelper.ScopeUser also will give you "sys@currentlyloggedcompany" which leads me to to conclusion that "sys@currentlyloggedcompany" is most cool user in Acumatica system.
Also it is not presented in table users which means that such a user is some kind of virtual user.

If you become puzzled from this long technical description, then go straight to the summary:
Summary

If your processing screen crashes with no reason in automation schedule mode, add to your code 

using (new PXImpersonationContext(PX.Data.Update.PXInstanceHelper.ScopeUser))

{

    // here shold be your processing logic

}

and try again. In mine case such addition "resurected" mine automation schedules. Hopefully yours will also come back to life.