Legacy Dev Forum Posts

 View Only

Sign Up

Multiple connections in the same app => all connections use last login resulting in wrong data being returned

  • 1.  Multiple connections in the same app => all connections use last login resulting in wrong data being returned

    Posted 06-05-2025 18:32

    ssteiner | 2024-10-16 11:06:31 UTC | #1

    My application can have multiple connections to the pure cloud. I found that when I tried with two connections, I login the first, perform some operation, login the second, perform some operations. and all is well. Then use the first connection again- and you'll get the data of the second connection.

    The Problem stems from the use of Configuration.Default and stuff therein. A new Configuration is instantiated once, along with the corresponding ApiClient, so the second connection will re-use what was set up in the first one, including the token. So,

    Connection 1 => Login with ClientId1 => Get Data for ClientId1 Connection 2 => Login with ClientId2 => Get Data for ClientId2

    Connection1 => Get Data for ClientId 2 (ooopsie)

    Maybe it would be helpful to document the steps you need to take to make this work. every XXApi.cs class also accessed Configuration.Default unless you instantiate it differently.

    If you instantiate it differently, so

    var myConfig = new Configuration();
    var myCient = myConfig.ApiClient;
    var tokensApi = new TokensApi(myConfig);

    you run into another issue - the constructor of Configuration accesses the singleton Configuration.Default - so in fact two new Configurations are instantiated in the first line of the code above. There's a recursion between Configuration and ApiClient - one depends upon the other and vice versa. This should be unwound and the documentation should be adapted.

    Unwinding the Configuration seems rather simple:

    public static Configuration Default
    {
        get
        {
            lock(defaultConfigLock)
            {
                if (defaultConfig == null)
                    defaultConfig = new Configuration();
            }
            return defaultConfig;
        }
    }
    
    private static Configuration defaultConfig;
    private static readonly object defaultConfigLock = new object();

    In addition, you have to now use defaultConfig in setApiClientUsingDefault instead of Default or you run the re-entrancy gauntled yet again.

    Now you'd only also have to unwind the parameterless ApiClient constructor (or never use it.. currently I'm doing the latter). (sidenote: it seems weird to have two constructors, one parameterless, one with an optional parameter, and then not reuse the first one from the second one if no config was provided).

    @sajidali I'm taking it you managed to resolve your issue you posted 2 years ago. Seems to be the same since instantiating new ApiClient() accesses Configuration.Default, so the next time you instantiate a new ApiClient, you'll share the Configuration.Default.


    system | 2024-11-10 13:00:56 UTC | #2

    This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.


    This post was migrated from the old Developer Forum.

    ref: 29806