c# - Getting exception while reading Connected property on TcpClient instance -
public bool isconnected { { return _tcpclient == null ? false : _tcpclient.connected; } } throws
"object reference not set instance of object."
at
at system.net.sockets.tcpclient.get_connected() @ project.viewmodel.modbusoutputcounter.get_isconnected() in c:...\modbusoutputcounter.cs:line 115
how possible, , how can prevent receiving exception?
edit:
as per svk's anwer problem in disposing, internally called in close() method. workaround:
return _tcpclient?.client != null ? _tcpclient.connected : false;
according reference source tcpclient, connected directly returns connected of underlying socket. means connected throw nullreferenceexception when socket null. though skimming reference source, found 2 cases when can happen:
- when
tcpclienthas beendisposed. - when explicitly set
clientsocketnull.
Comments
Post a Comment