web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Answered

Disposable context and concurrency - Extensible X++

(0) ShareShare
ReportReport
Posted on by

Hello !

Investigating the possibility of passing data between methods I've come across this pattern of using a context object that implements System.IDisposable.

See this post: https://community.dynamics.com/365/financeandoperations/b/mfp/posts/extensible-x-method-signatures for reference.

Basically, it's a singleton that get disposed in order to avoid  stale data, and It's type-safe.

This pattern it's used in in many places as a method to avoid adding extra parameters to an existing method.

For example the classes like this are use to define this kind of singleton.:

ContextPackingSlipTrans_IT,

ProjValEmplProjSetupParameters

RetailTerminalTableContext

As I said, mainly they are used to avoid adding extra parameter to existing methods but also a convenient way to pass data between methods in case of implementing extension classes (see ContextPackingSlipTrans_IT). Some of those objects are instantiated from forms.

ContextPackingSlipTrans_IT It's using the user seesionId to address the concurrency issue, but others do not have something like this.

The question is how this work with concurrency? If multiple users trigger the same functionality at the same time, wouldn't that mean they will steal each others data because of this singleton object?

Thank you.

I have the same question (0)
  • Martin Dráb Profile Picture
    237,978 Most Valuable Professional on at

    I don't know how exactly it works under the hood, but I don't think there will be any problem with multiple users. I believe they have their own set of static values, including their own copy of the singleton.

    But anyway, this is normally considered an anti-pattern and I'm always nervous when I see it. I can imagine case where things may go wrong. It's a hack that you may be forced to use if everything else fail - if you can add a parameter, you don't need this hack.

  • FlorinF Profile Picture
    on at

    There are standard Ax classes that use this approach in Microsoft's models. If you search the AOT for "System.IDisposable", you will find them.

    To test this scenario I was thinking to put a break point in the singleton class and start two separate debug sessions on the same machine - login with two user accounts on the same machine.

    The problem is that each session will have their on IISExpress  and you will not get any concurrency problem. 

    The code must run on the IIS but you can't  attach to that process to debug it . I will try to access the singleton class from two user sessions and simulate some kind of delay in order to get into concurrency issue.

  • Martin Dráb Profile Picture
    237,978 Most Valuable Professional on at

    Yes, I'm familiar with this pattern and I know that it's used in some standard classes.

    Regarding IIS, you can configure Visual Studio to use IIS instead of IISExpress. The option is stored in DynamicsDevConfig.xml (Documents\Visual Studio Dynamics 365).

  • FlorinF Profile Picture
    on at

    Using IIS for debug would not require Administrator rights ? I thought that we don't have Admin rights on developer machines and for that reason IISExpres was introduced .  

    Thank you for your time.

  • Martin Dráb Profile Picture
    237,978 Most Valuable Professional on at

    You don't have admin rights on virtual machines hosted by Microsoft. Using them for development doesn't sound like a good idea to me, exactly because you don't have admin rights, which are useful for quite a few things. I indeed have admin rights on my development machines (hosted in my company's Azure subscription, in my own Azure subscription and running on my local machine).

    By the way, I thought that Microsoft-hoster Tier1 environments aren't available anymore.

  • FlorinF Profile Picture
    on at

    It turns out that my dev machine is also hosted in company's Azure subscription and I can attach to IIS w3wp.exe !

    Thank you

  • Verified answer
    Joris dG Profile Picture
    17,775 on at

    We no longer host dev VMs on our subscriptions, so technically everyone should be able to get admin access.

    Going back to your original issue, I'm not sure what IIS has to do with this? IIS creates worker instances as well if multiple users are connecting.

    In any case, for F&O and X++ everything is single threaded session based, including statics etc. That is one of the main reasons you shouldn't use .NET multithreading features in X++ as essentially nothing is guaranteed to be thread safe. The way certain multi-threading scenarios are handled is by start a completely new session on the server.

  • FlorinF Profile Picture
    on at

    So, that means singleton object data is not accessible by a different user ? In a classical Net ASP web application if you create a singleton, that instance is persisted on w3wp.exe until is restarted or application pool is recycled.

    I've tried this singleton X++ example in D365: 

    1 A singleton class implementation (without system.idisposable)

    2 A runnable class that creates the singleton instance with some random data.

    3 Execute this runnable class from 2 different machines with the same user (access the ax job from browser url, https://xxxdynamics.com/?cmp=USMF&mi=SysClassRunner&cls=testClass).

    The result:

    You get different sessions id, because the job is executed from different physical machines.

    The singleton data is not persisted, as you will expect with .Net ASP application. If you start the job from machine A (from browser) and start the job again from machine B you get new instance of the singleton object each time. If everything is single threaded session based, that means if you have 2 batch jobs (not runnable class, batch jobs that runs on the AOS) that use singleton pattern, they can't have the same singleton instance, because all the data is session base? The concurrency is not a problem because of this reason ?

    Thank you

  • Verified answer
    Joris dG Profile Picture
    17,775 on at

    Correct. If you need something to persist across sessions, you have to store it in the DB.

  • Joris dG Profile Picture
    17,775 on at

    Also note that just opening two different tabs on the same machine in the same browser will also give you two separate sessions.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 467 Super User 2025 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 420 Most Valuable Professional

#3
BillurSamdancioglu Profile Picture

BillurSamdancioglu 241 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans