Method breaking after while without return

S

SoabTI

Guest
I am trying to build an api to talk to network assets, so I created a class that implements the connection and conversation with these assets, but when calling the class through the controller the sending method is broken without exception after a while that traverses the MemoryStream.

using Provisioning.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace Provisioning.Contexts
{
public class TL1Context : IDisposable, ITL1Context
{
private static readonly TcpClient _tcpClient = new TcpClient();
public TL1Context(string addr, int port)
{

_tcpClient.Connect(addr, port);

if (!_tcpClient.Connected)
throw new TimeoutException("Not has possible connect to TL1 server.");

Login();
}

public TL1Context()
{
_tcpClient.Connect("10.1.5.12", 3337);

if (!_tcpClient.Connected)
throw new TimeoutException("Not has possible connect to TL1 server.");

Login();
}
private void Login()
{
var strin = $"LOGIN:::CTAG::UN=GEPON,PWD=GEPON;" +
$"LOGIN:::C000::UN=GEPON,PWD=GEPON;" +
$"SHAKEHAND:::CTAG::;";

var returnedMessage = this.SendMessage(strin);
}

private string SendMessage(string Message)
{
try
{
var stream = _tcpClient.GetStream();

byte[] messageSent = Encoding.ASCII.GetBytes(Message);

stream.Write(messageSent);

var messageReceived = new byte[1024];
string textedMessage = string.Empty;
using (var ms = new MemoryStream())
{
int numBytesRead;
while ((numBytesRead = stream.Read(messageReceived, 0, messageReceived.Length)) > 0)
{
ms.Write(messageReceived, 0, numBytesRead);
textedMessage += Encoding.ASCII.GetString(ms.ToArray(), 0, (int)ms.Length);
}

Console.WriteLine("NOT EXECUTE THIS");
return textedMessage;
}


}
catch (Exception e)
{
throw;
}
}

public void Logout()
{
var strin = $"LOGOUT:::CTAG::;";
this.SendMessage(strin);

}

public IList<ONUAvailable> GetOnusUnauthorizated(List<OLT> olts)
{
// TODO
foreach (var olt in olts)
{
var strin = $"LST-UNREGONU::OLTID={olt.IpAddress.ToString()}:CTAG::;" +
$"LST-UNREGONU::OLTID={olt.IpAddress.ToString()}:C000::;";
var response = this.SendMessage(strin);
}
return null;
}

public void Dispose()
{
this.Logout();
_tcpClient.Dispose();
}
}
}



Controller call

[HttpGet]
public ActionResult<string> Test()
{
try
{
var tl1 = new TL1Context();
//OnuAuthService.Test();

}
catch (Exception e)
{
return BadRequest(e);
throw;
}
return Ok();
}


Console App for debugging

using Provisioning.Contexts;
using Provisioning.Models;
using System;
using System.Collections.Generic;

namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
try
{
var tl1 = new TL1Context();

// NOT EXECUTE THIS
Console.WriteLine(tl1._tcpClient.Connected);
tl1.GetOnusUnauthorizated(new List<OLT>() {
new OLT() { IpAddress = new System.Net.IPAddress(new byte[] {192, 168, 99, 2 })
}
});
Console.WriteLine(tl1._tcpClient.Connected);
tl1.Dispose();

Console.WriteLine(tl1._tcpClient.Connected);


}
catch (Exception)
{

throw;
}

}
}
}


In both cases, the follows lines of TL1Context never are executed

Console.WriteLine("NOT EXECUTE THIS");
return textedMessage;


@Edit: removed async call on controller @Edit: included console app for debugging

Continue reading...
 
Top