我已经开发了一种新的WCF服务,该服务可以在IIS中运行,并且可以使用ASP.NET Web应用程序使用该服务。在服务器上运行它时,它可以正常工作。但是,当我通过IE浏览器和Chrome浏览器以多个用户(2个用户)访问该服务时,我注意到当第二个用户登录时,他们看到的是与第一个用户会话相关的数据,因此不可能有多个用户一次使用该服务。我以为WCF服务一旦配置了以下内容,便会自动处理多个会话:
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]

但这似乎并非如此。以下是我如何在服务中定义我的类以及如何初始化MyGlob的方法。如果有人能指出我做错了什么,我将不胜感激。


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;

最佳答案

您的问题是“静态”关键字。将应用程序加载到内存中后,将创建一次任何“静态”类。因此,单个实例将在所有请求中共享。

关于c# - C#WCF的行为类似于单个用户实例,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36105484/

10-10 07:53