-1

我需要一些帮助。我在代码行中遇到异常"e_svr.svr = svr;"。我不知道为什么,因为svr不为空。任何帮助将不胜感激。我正在尝试将对象的实例保存在字典中。

  [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single,ConcurrencyMode=ConcurrencyMode.Multiple)]   
public class EPS_Service : IEPS_Service
{
    public decimal cal;
    static Dictionary<string, EPS_Instance> OpenConnections =
        new Dictionary<string, EPS_Instance>();

    public invoice_data Invoice_to_Client(input_params inputparams)
    {


        EPS_Instance e_svr = new EPS_Instance();
        invoice_data Invoice = new invoice_data();

        if (OpenConnections.TryGetValue(inputparams.VSId + inputparams.Username, out e_svr))
        {
            Trace.WriteLine("FOUND!");
            e_svr.svr.PrepareByMoney(e_svr.ck, inputparams.VSId, inputparams.meter_number, inputparams.amount, 0, ref cal);
            object tr_bl = e_svr.svr.GetInvoiceData(e_svr.ck, e_svr.svr.Confirm(e_svr.ck));
            Invoice.invoice_header = fill_invoice_header(tr_bl);
            Invoice.invoice_body = fill_invoice_body(tr_bl);
            Invoice.invoice_extra = fill_invoice_extra(tr_bl);
            Invoice.invoice_steps = fill_invoice_steps(tr_bl);
            Invoice.invoice_transfer = fill_invoice_transfer(tr_bl);
        }
        else
        {
            Trace.WriteLine("NOT FOUND");
            EPS30Ora.EPS30Svr svr = new EPS30Ora.EPS30Svr();
            if (svr.LogOnEx("EDENOR", inputparams.VSId, inputparams.Username, inputparams.Password, "EPS30Ora", ref inputparams.ck) == EPS30Ora.LogonResult.psOK)
            {
                e_svr.ck = inputparams.ck;
                e_svr.svr = svr; 
                OpenConnections.Add(inputparams.VSId + inputparams.Username, e_svr);
                svr.PrepareByMoney(inputparams.ck, inputparams.VSId, inputparams.meter_number, inputparams.amount, 0, ref cal);
                object tr_bl = svr.GetInvoiceData(inputparams.ck, svr.Confirm(inputparams.ck));
                Invoice.invoice_header = fill_invoice_header(tr_bl);
                Invoice.invoice_body = fill_invoice_body(tr_bl);
                Invoice.invoice_extra = fill_invoice_extra(tr_bl);
                Invoice.invoice_steps = fill_invoice_steps(tr_bl);
                Invoice.invoice_transfer = fill_invoice_transfer(tr_bl);

            }
        }



      return Invoice; 
    }
}
4

1 回答 1

1

我不知道更改为 struct 是否是个好主意。如果你知道你在做什么,那就这样吧。但请记住,如果EPS_Instance大结构传入参数OpenConnections.Add会溢出库存。

问题在else陈述中。如果TryGetValue返回 false,则参数out设置为 null。所以接下来如果你尝试这样做

e_svr.ck = inputparams.ck;

NullReferenceException 将引发,因为 e_svr 为空。

在声明中e_svr没有必要创建新实例。所以你的代码应该看起来像

EPS_Instance e_svr;
if (OpenConnections.TryGetValue(inputparams.VSId + inputparams.Username, out e_svr))
{
    //...
}
else
{
    e_svr = new EPS_Instance();
    //...
}
于 2013-09-11T17:44:46.663 回答