pat Clarke
pat Clarke

Reputation: 11

C# WCF is behaving like a single user instance

I have developed a new WCF service which runs in IIS and a ASP.NET web application to consume this service. When running this on the server it works ok. However as soon as I accessed the service as multiple users (2 users) through an IE browser and Chrome Browser I noticed that when the second user signed in they were seeing data related to the 1st users session, making it impossible for more than 1 user to use the service at a time. I had assumed that the WCF service would automatically handle multiple sessions once the service was configured with: [ServiceBehavior(InstanceContextMode=InstanceContextMode.PerSession)]

But this does not seem to be the case. Below is how I have defined my classes within the service and how MyGlob is initialized. I would realy appreciate if someone could point out what I am doing wrong ?

public UserMaster LoadUserDtls(string loggedInUserId, string userPassword)
      {
      try
         {
          // Create all objects required to bootstrap initial menu transaction
                MyGlob.CrtUserMaster();
                MyGlob.CrtEnterpriseMaster();
                MyGlob.CrtDataBaseIO();
                MyGlob.CrtExecuteTrans();
                MyGlob.CrtErrorMsgs();
                MyGlob.CrtPromptMsgs();
                MyGlob.CrtConnectionStrings();
                MyGlob.CrtLogMessages();
                MyGlob.CrtRuleTypeOpSeq();
                MyGlob.CrtErrMsgsList();
                MyGlob.CrtEnvironments();
                MyGlob.CrtPromptMsgsList();
                MyGlob.CrtConnStringList();
                MyGlob.CrtEnvironmentList();
                MyGlob.CrtLogMessagesDict();
                MyGlob.CrtRuleTypeOpSeqDict();
                MyGlob.CrtKeywordsDict();
                MyGlob.CrtCommands();
                MyGlob.CrtObjDict();
                MyGlob.CrtNextNumbers();
                MyGlob.CrtAppDistLstDict();
                MyGlob.CrtNextNumbersDict();
                g.MyDataBaseIO.ReceiveMyGlob(MyGlob);
                g.MyCommands.ReceiveMyGlob(MyGlob);
**Definition of Classes within the WCF Service**

namespace DCRules2
{
    public class globals
    {
        public static UserMaster MyUser;
        public static EnterpriseMaster MyEnterprise;
        public static TransactionHdr MyTransHdr;
        public static TransactionSeq MyTransSeq;
        public static TransactionsWaitingToLoad MyTransWaiting;
        public static ExecuteTrans MyExe;
        public static Commands MyCommands;
        public static ErrorMsgs MyErrMsgs;
        public static PromptMsgs MyPromptMsgs;
        public static ConnectionStrings MyConnString;
        public static Environments MyEnvironments;
        public static RuleTypeOpSeq MyRuleTypeOpSeq;
        public static LogMessages MyLogMessages;
        public static DataBaseIO MyDataBaseIO;
        public static ErrorsEncountered MyErrorsEncountered;
        public static DisplaySeq MyDisplaySeq;
        public static PassToServiceParms MyPassToServiceParms;
        public static RtnFromServiceParms MyRtnFromServiceParms;
        public static InputValues MyInpFldVal;
        public static Object MyVarObj;
        public static NextNumbers MyNextNumbers;
        public static TransOutput MyTransOutput;

        public static AppDistLst MyAppDistLst;

        public static List<TransactionHdr> MyTransHdrList;
        public static List<TransactionSeq> MyTransSeqList;
        public static List<Environments> MyEnvironmentList;
        public static List<ErrorMsgs> MyErrMsgsList;
        public static List<PromptMsgs> MyPromptMsgsList;
        public static List<ConnectionStrings> MyConnStringList;
        public static List<DisplaySeq> MyDisplaySeqList;
        public static List<TransactionsWaitingToLoad> MyTransWaitingList;
        public static List<ErrorsEncountered> MyErrorsEncounteredList ;
        public static List<InputValues> MyInpFldValList ;


        public static Dictionary<string, int> MyTagsDict;
        public static Dictionary<string, string> MyVarValuesDict;
        public static Dictionary<string, string> MyVarTypesDict;
        public static Dictionary<string, string> MyCondLvlsDict;
        public static Dictionary<string, string> MyRuleTypeOpSeqDict;
        public static Dictionary<string, string> MyLogMessagesDict;
        public static Dictionary<string, string> MyKeywordsDict;
        public static Dictionary<string, int[]> MyPrevSelDict;
        public static Dictionary<string, object> MyObjDict ;
        public static Dictionary<string, string> MyAppDistLstDict;
        public static Dictionary<string, string> MyNextNumbersDict;

        public static TransLvlValues MyTransLvlValues;
        public static SessionLvlValues MySessionLvlValues;
        public static AppLvlValues MyAppLvlValues;
        //*****************************
        // Create new Objects
        //*****************************
        public UserMaster CrtUserMaster()
        {
            MyUser = new UserMaster();
            return MyUser;
        }
        public EnterpriseMaster CrtEnterpriseMaster()
        {
            MyEnterprise = new EnterpriseMaster();
            return MyEnterprise;
        }
        public TransactionHdr CrtTransactionHdr()
        {
            MyTransHdr = new TransactionHdr();
            return MyTransHdr;
        }

        public TransactionSeq CrtTransactionSeq()
        {
            MyTransSeq = new TransactionSeq();
            return MyTransSeq;
        }

        public TransactionsWaitingToLoad CrtTransactionsWaitingToLoad()
        {
            MyTransWaiting = new TransactionsWaitingToLoad();
            return MyTransWaiting;
        }

        public ExecuteTrans CrtExecuteTrans()
        {
            MyExe = new ExecuteTrans();
            return MyExe;
        }

        public Commands CrtCommands()
        {
            MyCommands = new Commands();
            return MyCommands;
        }

        public ErrorMsgs CrtErrorMsgs()
        {
            MyErrMsgs = new ErrorMsgs();
            return MyErrMsgs;
        }

        public PromptMsgs CrtPromptMsgs()
        {
            MyPromptMsgs = new PromptMsgs();
            return MyPromptMsgs;
        }

        public ConnectionStrings CrtConnectionStrings()
        {
            MyConnString = new ConnectionStrings();
            return MyConnString;
        }

        public Environments CrtEnvironments()
        {
            MyEnvironments = new Environments();
            return MyEnvironments;
        }

        public RuleTypeOpSeq CrtRuleTypeOpSeq()
        {
            MyRuleTypeOpSeq = new RuleTypeOpSeq();
            return MyRuleTypeOpSeq;
        }

        public LogMessages CrtLogMessages()
        {
            MyLogMessages = new LogMessages();
            return MyLogMessages;
        }

        public DataBaseIO CrtDataBaseIO()
        {
            MyDataBaseIO = new DataBaseIO();
            return MyDataBaseIO;
        }

        public ErrorsEncountered CrtErrorsEncountered()
        {
            MyErrorsEncountered = new ErrorsEncountered();
            return MyErrorsEncountered;
        }

        public DisplaySeq CrtDisplaySeq()
        {
            MyDisplaySeq = new DisplaySeq();
            return MyDisplaySeq;
        }

        public PassToServiceParms CrtPassToServiceParms()
        {
            MyPassToServiceParms = new PassToServiceParms();
            return MyPassToServiceParms;
        }

        public RtnFromServiceParms CrtRtnFromServiceParms()
        {
            MyRtnFromServiceParms = new RtnFromServiceParms();
            return MyRtnFromServiceParms;
        }

        public InputValues CrtInputValues()
        {
            MyInpFldVal = new InputValues();
            return MyInpFldVal;
        }
        public Object CrtVarObj()
        {
            MyVarObj = new Object();
            return MyVarObj;
        }
        public NextNumbers CrtNextNumbers()
        {
            MyNextNumbers = new NextNumbers();
            return MyNextNumbers;
        }
        public AppDistLst CrtAppDistLst()
        {
            MyAppDistLst = new AppDistLst();
            return MyAppDistLst;
        }
        public TransLvlValues CrtTransLvlValues()
        {
            MyTransLvlValues = new TransLvlValues();
            return MyTransLvlValues;
        }
        public SessionLvlValues CrtSessionLvlValues()
        {
            MySessionLvlValues = new SessionLvlValues();
            return MySessionLvlValues;
        }
        public AppLvlValues CrtAppLvlValues()
        {
            MyAppLvlValues = new AppLvlValues();
            return MyAppLvlValues;
        }
        public TransOutput CrtTransOutput()
        {
            MyTransOutput = new TransOutput();
            return MyTransOutput;
        }
        //*****************************
        // Create new List Objects
        //*****************************


        public List<TransactionHdr> CrtTransHdrList()
        {
            MyTransHdrList = new List<TransactionHdr>();
            return MyTransHdrList;
        }

        public List<TransactionSeq> CrtTransSeqList()
        {
            MyTransSeqList = new List<TransactionSeq>();
            return MyTransSeqList;
        }

        public List<Environments> CrtEnvironmentList()
        {
            MyEnvironmentList = new List<Environments>();
            return MyEnvironmentList;
        }

        public List<ErrorMsgs> CrtErrMsgsList()
        {
            MyErrMsgsList = new List<ErrorMsgs>();
            return MyErrMsgsList;
        }

        public List<PromptMsgs> CrtPromptMsgsList()
        {
            MyPromptMsgsList = new List<PromptMsgs>();
            return MyPromptMsgsList;
        }

        public List<ConnectionStrings> CrtConnStringList()
        {
            MyConnStringList = new List<ConnectionStrings>();
            return MyConnStringList;
        }

        public List<DisplaySeq> CrtDisplaySeqList()
        {
            MyDisplaySeqList = new List<DisplaySeq>();
            return MyDisplaySeqList;
        }

        public List<TransactionsWaitingToLoad> CrtTransWaitingList()
        {
            MyTransWaitingList = new List<TransactionsWaitingToLoad>();
            return MyTransWaitingList;
        }
        public List<ErrorsEncountered> CrtErrorsEncounteredList()
        {
            MyErrorsEncounteredList = new List<ErrorsEncountered>();
            return MyErrorsEncounteredList;
        }

        public List<InputValues> CrtInpFldValList()         
        {
            MyInpFldValList = new List<InputValues>();
            return MyInpFldValList;
        }


        //*****************************
        // Create new Dictionary Objects
        //*****************************

        public Dictionary<string, int> CrtTagsDict()
        {
            MyTagsDict = new Dictionary<string, int>();
            return MyTagsDict;
        }

        public Dictionary<string, string> CrtVarValuesDict()
        {
            MyVarValuesDict = new Dictionary<string, string>();
            return MyVarValuesDict;
        }

        public Dictionary<string, string> CrtVarTypesDict()
        {
            MyVarTypesDict = new Dictionary<string, string>();
            return MyVarTypesDict;
        }

        public Dictionary<string, string> CrtCondLvlsDict()
        {
            MyCondLvlsDict = new Dictionary<string, string>();
            return MyCondLvlsDict;
        }

        public Dictionary<string, string> CrtRuleTypeOpSeqDict()
        {
            MyRuleTypeOpSeqDict = new Dictionary<string, string>();
            return MyRuleTypeOpSeqDict;
        }

        public Dictionary<string, string> CrtLogMessagesDict()
        {
            MyLogMessagesDict = new Dictionary<string, string>();
            return MyLogMessagesDict;
        }

        public Dictionary<string, string> CrtKeywordsDict()
        {
            MyKeywordsDict = new Dictionary<string, string>();
            return MyKeywordsDict;
        }

        public Dictionary<string, int[]> CrtPrevSelDict()
        {
            MyPrevSelDict = new Dictionary<string, int[]>();
            return MyPrevSelDict;
        }

        public Dictionary<string, object> CrtObjDict()
        {
            MyObjDict = new Dictionary<string, Object>();
            return MyObjDict;
        }
        public Dictionary<string, string> CrtAppDistLstDict()
        {
            MyAppDistLstDict = new Dictionary<string, string>();
            return MyAppDistLstDict;
        }
        public Dictionary<string, string> CrtNextNumbersDict()
        {
            MyNextNumbersDict = new Dictionary<string, string>();
            return MyNextNumbersDict;
        }
**Here is how I initialize MyGlob**
namespace DCRules2
 {
     [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
     class TransactionService : ITransaction
     {
         public static globals MyGlob = new globals();

         //public static globals MyGlob = globals.CrtGlobals();
         //globals MyGlob = g.MyGlob.CrtGlobals();
         RegDetails MyRegDetails;
         Object MyVarObj;
         DataSet MyDs;
         DataTable dt2;
         DataRow dtRow;

Upvotes: 1

Views: 53

Answers (1)

CodingYoshi
CodingYoshi

Reputation: 27039

Your problem is the "static" keywords. Any "static" class will be created once when the application is loaded into memory. Therefore, a single instance will be shared amongst all requests.

Upvotes: 1

Related Questions