I use Inetlab.SMPP Library to implement a RE (Routing Entity in SMPP 5.0 terms). I have some questions about Stop() and Dispose() methods of an SmppServer instance:
- Is it necessary to call the
Stop()method before disposingSmppServerinstance? - Does the
Stop()method throw an exception ifSmppServerinstance is already stopped? - May the methods throw an exception?
So, is the following snippet correct?
class MyService : IHostedService, IDisposable
{
private readonly SmppServer _server = null;
public MyService()
{
_server = new SmppServer()
{
// configure SMPP server here
};
}
public Task StartAsync(CancellationToken cancellationToken)
{
_server.Start(cancellationToken);
return Task.CompletedTask;
}
public Task StopAsync(CancellationToken cancellationToken)
{
_server.Stop();
return Task.CompletedTask;
}
public void Dispose()
{
_server?.Stop();
_server?.Dispose();
}
// implement logic here
}
Do I need to use try...catch block within the Dispose() method? Is it possible to exclude _server?.Stop() line?
No, you don't need to call Stop method before Dispose. Dispose method as well as Stop method closes all active connections and stops the SmppServer.
Stop method does nothing when SmppServer is already stopped.
I would change the snippet to