Best Practice using RODL functions

Hello,

What’s the best practice to use RODL functions client side? To create the service each time, for example:

public static int CountUnreadMessages(string regioPrimkeys)
        {
            IAdireServerService service = CoAdireServerService.Create(_instance.binMessage, _instance.MyClientChannel);
            return service.CountUnReadMessages(regioPrimkeys);
        }

public static void SyncTerminalClientID(string aClientID)
        {
            IAdireServerService service = CoAdireServerService.Create(_instance.binMessage, _instance.MyClientChannel);
            service.SetClientId(aClientID);
        }

Or to instantiate the service in the constructor and reuse it in the functions, like this:

IAdireServerService _service;
public MainForm()
		{
			InitializeComponent();

                       _service = CoAdireServerService.Create(_instance.binMessage, _instance.MyClientChannel);
		}

public static int CountUnreadMessages(string regioPrimkeys)
        {
            return _service.CountUnReadMessages(regioPrimkeys);
        }

public static void SyncTerminalClientID(string aClientID)
        {
            _service.SetClientId(aClientID);
        }

And can you tell me why?

Thank you.

Greetings,
Wouter

Hi,

I prefer to use 2nd way.
In general, both ways are acceptable

1 Like

In general there is not much difference. Client-side service proxy (one that is created by the CoAdireServerService.Create call) is a light-weight proxy object for client → server calls.
So it is up to you. In the ideal world for better testability I would consider create a service factory class and to inject it via constructor.

Another question is that in general it is not recommended to perform service calls on the main UI thread of application. You need to either use background workers or to use asynchronous service method calls on the client side. Your _Intf code file in the client project already contains pre-generated method definitions that support async / await calls

1 Like

Hello Anton,

Thank you for the clarification.

You say that service calls shouldn’t be on the main UI thread. I have a legacy Windows Forms application with a Windows.Forms.Timer. Every 30 second the tick events fires a service call to check how many new text messages are received. How would you do this asynchronously and return to the main UI thread to show the count of unread text messages?

Thank you so much.
Stay safe.

Greetings,
Wouter

I believe that if you call the async versions of your function via await from the Main thread, you should be safe, as the request will be dispatched asynchronously and your method (eg the Timer’s event handler) returns immediately, without blocking. Once the request succeeded, the remainder of your handler method will be re-invoked on (i would hope, @antonk will know for sure) the correct thread again.

Windows.Forms.Timer raises its Tick event on the UI thread. This can cause issues if, f.e. server is not accessible application UI would stop to respond.
You can either try to use methods provided by the IAdireServerService_Async interface (ie to perform asynchronous calls instead of synchronous ones) or try to use different Timer implementation (f.e. System.Threading.Timer).

Regards

1 Like