Skip to main content
Skip table of contents

Asynchronous Method Dispatch (AMD) in C#

The number of simultaneous synchronous requests a server is capable of supporting is determined by the number of threads in the server's thread pool. If all of the threads are busy dispatching long-running operations, then no threads are available to process new requests and therefore clients may experience an unacceptable lack of responsiveness.

Asynchronous Method Dispatch (AMD), the server-side equivalent of AMI, addresses this scalability issue. Using AMD, a server can receive a request but then suspend its processing in order to release the dispatch thread as soon as possible. When processing resumes and the results are available, the server can provide its results to the Ice runtime for delivery to the client.

AMD is transparent to the client, that is, there is no way for a client to distinguish a request that, in the server, is processed synchronously from a request that is processed asynchronously.

In practical terms, an AMD operation typically queues the request data for later processing by an application thread (or thread pool). In this way, the server minimizes the use of dispatch threads and becomes capable of efficiently supporting thousands of simultaneous clients.

Async Skeleton

The easiest way to use AMD in C# is to make your servant class derive from the async skeleton class generated by the Slice compiler. For example:

C#
// This servant uses AMD
public class Chatbot : VisitorCenter.AsyncGreeter
{
   // your implementation here
};

Enabling AMD Piecemeal

If you prefer to implement some operations asynchronously (with AMD) and other operations synchronously, you can add the ["amd"] metadata directive to the operations you want to implement with AMD and use the default skeleton class.

The metadata directive replaces synchronous dispatch on the default skeleton, that is, a particular operation implementation must use synchronous or asynchronous dispatch and cannot use both.

Consider the following Slice definitions:

CODE
interface Controller
{
    ["amd"] void startProcess();
    int endProcess();
}

In this example, the startProcess of the default skeleton class uses asynchronous dispatch while endProcess uses synchronous dispatch.

AMD Mapping

With AMD, the skeleton’s abstract method is named <operation-name>Async. This method returns a Task and accepts the operation's in-parameters.

For example, suppose we have defined the following operation:

CODE
interface Example
{
    ["cs:identifier:Op"]
    string op(short s, out long l);
}

Operation op is mapped as follows in the async skeleton class:

C#
public record struct Example_OpResult(string returnValue, long l);

public abstract partial class AsyncExampleDisp_ : AsyncExample
{
    public abstract Task<Example_OpResult> OpAsync(short s, Ice.Current current);
    ...
}

You would get the same signature on the default skeleton (Example) if you decorate op with ["amd"].

AMD Exceptions

There are two processing contexts in which the logical implementation of an AMD operation may need to report an exception: the dispatch thread (the thread that receives the request), and the response thread (the thread that completes the task).

These are not necessarily two different threads: it is legal to complete the task from the dispatch thread.

The implementation of the Async method in your servant class can throw an exception synchronously: it’s equivalent to returning a task completed with this exception.

See Also
JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.