C#保存对象值

时间:2013-05-21 20:40:53

标签: c# wcf object persistence instance

希望有人可以帮助我。我有一个方法(user_login),而不是提示用户登录,然后生成并返回发票和令牌。

问题是,如果用户已经登录(我将其保留在List中),则不得调用方法svr.logonex。我应该从前一个实例中检索值。我想我必须保存svr变量,但我不知道该怎么做。任何帮助将不胜感激。感谢

以下是代码:

public EPS30Ora.LogonResult User_Login(input_params inputparams)
        {
          EPS30Ora.EPS30Svr svr = new EPS30Ora.EPS30Svr();
         if (Users.Exists(Username => Username.Username.Equals(inputparams.Username)))
            {
                List<input_params> selected = Users.Where(Username => Username.Username.Equals(inputparams.Username)).ToList();
                svr.PrepareByMoney(selected[0].ck, 1, inputparams.meter_number, 10, 0, ref cal);
            }
            else 
            {

                Log_Res = svr.LogOnEx("EDEESTE", inputparams.VSID, inputparams.Username, inputparams.Password, "EPS30Ora", ref inputparams.ck);

                if (Log_Res == EPS30Ora.LogonResult.psOK)
                {
                    try
                    {
                        Users.Add(inputparams);
                        object tr_bl = svr.GetInvoiceData(inputparams.ck, svr.Confirm(inputparams.ck));
                    }
                    catch (System.Runtime.InteropServices.COMException ex)
                    {
                        Trace.WriteLine("Se ha producido una excepción: " + ex);

                    }
              }


            }

            return Log_Res;
        }

2 个答案:

答案 0 :(得分:1)

听起来你需要拉这个:

EPS30Ora.EPS30Svr svr = new EPS30Ora.EPS30Svr();

退出Login方法,并将其移动到调用此方法的任何内容中,以便将此对象保持在更长的范围内。否则,从方法返回时会破坏对象。

然后,您可以将其作为参数传递给此方法,例如:

// Start user process

EPS30Ora.EPS30Svr svr = new EPS30Ora.EPS30Svr();

if (Users.Exists(Username => Username.Username.Equals(inputparams.Username))) {
 // do something
}
else {

}

...

public LogOn(EPS30Ora.EPS30Svr svr, input_params inputparams)
{
Log_Res = svr.LogOnEx("EDEESTE", inputparams.VSID, inputparams.Username, inputparams.Password, "EPS30Ora", ref inputparams.ck);
}

答案 1 :(得分:0)

namespace EPSInvoice_ServiceLibrary
{

    public class EPS_Service : IEPS_Service
    {
        List<input_params> Users = new List<input_params>();
        List<token_data> token_data = new List<token_data>();
        public decimal cal;


        public EPS30Ora.LogonResult Invoice(input_params inputparams)
        {
            EPS30Ora.EPS30Svr svr = new EPS30Ora.EPS30Svr();
            EPS30Ora.LogonResult Log_Res = new EPS30Ora.LogonResult();
            Trace.Listeners.Add(new TextWriterTraceListener(Console.Out));
            Trace.AutoFlush = true;
            Trace.Indent();

            if (Users.Exists(Username => Username.Username.Equals(inputparams.Username)))
            {
                List<input_params> selected = Users.Where(Username => Username.Username.Equals(inputparams.Username)).ToList();
                Trace.WriteLine("Username: " + inputparams.Username + " ya está logeado " + selected[0].ck);
                //    svr.PrepareByMoney(selected[0].ck, 1, inputparams.meter_number, 10, 0, ref cal);
            }
            else
            {

                try
                {
                    Log_Res = svr.LogOnEx("EDEESTE", inputparams.VSID, inputparams.Username, inputparams.Password, "EPS30Ora", ref inputparams.ck);
                    if (Log_Res == EPS30Ora.LogonResult.psOK)
                    {
                        Users.Add(inputparams);
                        Trace.WriteLine("Login OK. Username: " + inputparams.Username);
                        svr.PrepareByMoney(inputparams.ck, 1, inputparams.meter_number, 10, 0, ref cal);
                        object tr_bl = svr.GetInvoiceData(inputparams.ck, svr.Confirm(inputparams.ck));
                        token_data.Add(new token_data() { transfer_number = (string)((object[])(((object[])(((object[])(((object[])(tr_bl))[4]))[1]))[0]))[0] });
                        Trace.WriteLine("Transacción Finalizada. NTC obtenido: " + inputparams.ck);
                        Trace.Unindent();
                    }
                }

                catch (System.Runtime.InteropServices.COMException ex)
                {
                    Trace.WriteLine("Se ha producido una excepción: " + ex);
                }
            }
         return Log_Res;
        }




    }



}