C# 例外情况&引用;对象引用未设置为对象的实例;试图在字典中保留一个实例
我需要一些帮助。我在代码行C# 例外情况&引用;对象引用未设置为对象的实例;试图在字典中保留一个实例,c#,.net,wcf,object,C#,.net,Wcf,Object,我需要一些帮助。我在代码行“e_svr.svr=svr;”中遇到异常。我不知道为什么,因为svr不是空的。任何帮助都将不胜感激。 我正在尝试将该对象的一个实例保存到字典中 [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single,ConcurrencyMode=ConcurrencyMode.Multiple)] public class EPS_Service : IEPS_Service { publ
“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;
}
}
[服务行为(InstanceContextMode=InstanceContextMode.Single,ConcurrencyMode=ConcurrencyMode.Multiple)]
公共类EPS_服务:IEPS_服务
{
公共十进制cal;
静态字典打开连接=
新字典();
公共发票数据发票发送至客户机(输入参数inputparams)
{
EPS_实例e_svr=新的EPS_实例();
发票\数据发票=新发票\数据();
if(OpenConnections.TryGetValue(inputparams.VSId+inputparams.Username,out e_svr))
{
Trace.WriteLine(“找到了!”);
e_svr.svr.PrepareByMoney(e_svr.ck,inputparams.VSId,inputparams.meter_编号,inputparams.amount,0,参考校准);
对象tr_bl=e_svr.svr.GetInvoiceData(e_svr.ck,e_svr.svr.Confirm(e_svr.ck));
Invoice.Invoice_header=填写发票_header(tr_bl);
Invoice.Invoice\u body=填写发票\u body(tr\u bl);
Invoice.Invoice\u extra=填写发票\u extra(tr\u bl);
Invoice.Invoice\u steps=填写发票步骤(tr\u bl);
Invoice.Invoice\u transfer=填写发票\u transfer(tr\u bl);
}
其他的
{
Trace.WriteLine(“未找到”);
EPS30Ora.EPS30Svr svr=新的EPS30Ora.EPS30Svr();
如果(svr.LogOnEx(“EDENOR”,inputparams.VSId,inputparams.Username,inputparams.Password,“EPS30Ora”,ref inputparams.ck)==EPS30Ora.LogonResult.psOK)
{
e_svr.ck=输入参数s.ck;
e_svr.svr=svr;
添加(inputparams.VSId+inputparams.Username,e_svr);
svr.PrepareByMoney(inputparams.ck,inputparams.VSId,inputparams.meter_编号,inputparams.amount,0,参考校准);
对象tr_bl=svr.GetInvoiceData(inputparams.ck,svr.Confirm(inputparams.ck));
Invoice.Invoice_header=填写发票_header(tr_bl);
Invoice.Invoice\u body=填写发票\u body(tr\u bl);
Invoice.Invoice\u extra=填写发票\u extra(tr\u bl);
Invoice.Invoice\u steps=填写发票步骤(tr\u bl);
Invoice.Invoice\u transfer=填写发票\u transfer(tr\u bl);
}
}
退货发票;
}
}
我不知道更改为struct是否是个好主意。
如果你知道你在做什么,那就顺其自然吧。但是请记住,如果EPS\u Instance
是OpenConnections中的大结构传递参数,那么Add
会使股票溢出
问题在else
语句中。
如果TryGetValue
返回false,则参数out
设置为null。所以下一步如果你尝试这样做
e_svr.ck = inputparams.ck;
将引发NullReferenceException,因为e_svr为null
在e_svr
的声明中,没有必要创建新实例。所以你的代码应该是
EPS\u实例e\u svr;
if(OpenConnections.TryGetValue(inputparams.VSId+inputparams.Username,out e_svr))
{
//...
}
其他的
{
e_svr=新的EPS_实例();
//...
}
e_svr似乎是发生错误的语句中可能的候选者,但这似乎不可能,因为如果e_svr为null,则在前一行中会出现异常。您需要调试它,并查看哪个对象为null(并确保它确实在该行抛出异常)。谢谢您的回答。问题解决了。我把e_svr定义为一个公共类。现在我把它改为公共结构,它工作得很好。@ USER88660,这只是一个(可怕的)创可贴解决方案。您应该调试代码以确定什么是null。在您的示例中,e_svr的可能副本肯定是null。或者,如果e_svr.svr是一个属性,那么它内部会发生空引用异常。