-1

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:

  1. Is it necessary to call the Stop() method before disposing SmppServer instance?
  2. Does the Stop() method throw an exception if SmppServer instance is already stopped?
  3. 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?

Alexander
  • 4,420
  • 7
  • 27
  • 42
  • 1
    You could likely answer the second question easily enough by trial-and-error. As Unfortunately, since [the documentation for `SmppServer`](https://docs.inetlab.com/smpp/v2.9/api/Inetlab.SMPP.SmppServer.html) doesn't list the exceptions that can be thrown, nor do they offer their source code, it's going to be challenging to answer the third question comprehensively. (You could try decompiling the library to look for explicit throw statements, but that's not going to identify exceptions thrown by the CLR based on their logic.) Based on that, it may be worth taking this up with the vendor. – Jeremy Caney Jul 25 '21 at 18:04
  • As an aside, [the documentation](https://docs.inetlab.com/smpp/v2.9/api/Inetlab.SMPP.SmppServer.Stop.html) _does_ note that the `Stop()` method is now obsolete, in favor of `StopAsync()`. – Jeremy Caney Jul 25 '21 at 18:07

1 Answers1

1
  1. Is it necessary to call the Stop() method before disposing SmppServer instance?

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.

  1. Does the Stop() method throw an exception if SmppServer instance is already stopped?

Stop method does nothing when SmppServer is already stopped.

I would change the snippet to


class MyService : IHostedService, IDisposable
{
    private readonly SmppServer _server;

    public MyService()
    {
        _server = new SmppServer()
        {
            // configure SMPP server here
        };
    }

   public Task StartAsync(CancellationToken cancellationToken)
    {
        return _server.StartAsync(cancellationToken);
    }

    public Task StopAsync(CancellationToken cancellationToken)
    {
        return _server.StopAsync(cancellationToken);
    }

    public void Dispose()
    {
        _server.Dispose();
    }

    // implement logic here
}
 
Aleksey
  • 26
  • 1