0

Existing code:

public abstract class MyProjectDb: DB
{ 
    public static DB ActualDb
    {
        get
        {
            DB d1 = CallContext.GetData("_d1_") as DB;
            if (d1 == null)
            {
                d1 = new DB(Settings.AppServerCon1);
                CallContext.SetData("_d1_", d1);
            }
            return d1;
        }
    }
}

d1 is setting only once. Than it is getting from GetData method.

I have created similar one, for second db connection. But d2 is always getting null. Why i cant get d2 from GetData method? i mean each time d2 is filled with SetData method.

public abstract class MyProjectDbSecond : DB
{
    public static DB ActualDbSecond
    {
        get
        {
            DB d2 = CallContext.GetData("_d2_") as DB;
            if (d2 == null)
            {
                d2 = new DB(Settings.AppServerCon2);
                CallContext.SetData("_d2_", d2);
            }
            return d2;
        }
    }
}
  • `CallContext.SetData` and `CallContext.GetData` are working like global variable per context (whatever context is). So you have to check is there a code like `CallContext.SetData("_d2_", d2)` with d2 == null parameter value. Or you may just to set breakpoint on `ActualDbSecond.get` and check is `d2 = new DB();` executed or `_d2_` value is null. – oleksa Dec 29 '20 at 08:44
  • yes d2 = new DB() is executing in each time and CallContext.SetData("_d2_", d2); is executing. than d2 variable is filling with my db instance. – thepasserby Dec 29 '20 at 09:24
  • nice so if `d2 = new DB()` is executed each time it means that `CallContext.SetData("_d2_"` has not the same key value as `CallContext.GetData("_d2_")`. You may try with `var dName ="_d2_"; var d2 = CallContext.GetData(dName) as DB; ... CallContext.SetData(dName, d2);` – oleksa Dec 29 '20 at 09:32
  • Sorry that when i was writng the code, i missed parameter of DB class. (Settings.AppServerCon2) – thepasserby Dec 29 '20 at 10:29
  • oleksa still same result. when i call this class second time so CallContext.GetData("_d2_") as DB; return value of this method is null. – thepasserby Dec 29 '20 at 10:44
  • web application(UI) calls web host application(APP) with remoting. in my MyProjectDb class code context is not changing for the each requests but for MyProjectDbSecond class context is changing. What can be reason? – thepasserby Dec 29 '20 at 13:04

0 Answers0