---
title: "Dynamic Invocation"
url: https://docs.zeroc.com/ice/3.8/runtime/invocation/dynamic-invocation
docs_index: https://docs.zeroc.com/llms.txt
---

> For an index of every page, see [llms.txt](https://docs.zeroc.com/llms.txt).

# Dynamic Invocation

This page is written for C++, C#, Java, Python, and Swift.

Parts of this page are for some programming languages only, and name them. For one language's text alone, see [C++](https://docs.zeroc.com/ice/3.8/runtime/invocation/dynamic-invocation/cpp.md), [C#](https://docs.zeroc.com/ice/3.8/runtime/invocation/dynamic-invocation/csharp.md), [Java](https://docs.zeroc.com/ice/3.8/runtime/invocation/dynamic-invocation/java.md), [Python](https://docs.zeroc.com/ice/3.8/runtime/invocation/dynamic-invocation/python.md), and [Swift](https://docs.zeroc.com/ice/3.8/runtime/invocation/dynamic-invocation/swift.md).

A typed proxy marshals the parameters of an operation and unmarshals its results with code generated from the Slice definition of the operation. The `ice_invoke` ([C++](https://code.zeroc.com/ice/3.8/api/cpp/classIce_1_1ObjectPrx_ad8e9a5bc7efd45641c1059a3174055ab.html#ad8e9a5bc7efd45641c1059a3174055ab), [C#](https://code.zeroc.com/ice/3.8/api/csharp/api/Ice.ObjectPrx.html#Ice_ObjectPrx_ice_invoke_), [Java](https://code.zeroc.com/ice/3.8/api/java/com.zeroc.ice/com/zeroc/Ice/ObjectPrx.html#ice_invoke(java.lang.String,com.zeroc.Ice.OperationMode,byte[])), [Python](https://code.zeroc.com/ice/3.8/api/python/Ice.ObjectPrx.html#Ice.ObjectPrx.ice_invoke), and [Swift](https://code.zeroc.com/ice/3.8/api/swift/documentation/ice/objectprx/ice_invoke(operation:mode:inencaps:context:))) method of ObjectPrx ([C++](https://code.zeroc.com/ice/3.8/api/cpp/classIce_1_1ObjectPrx.html), [C#](https://code.zeroc.com/ice/3.8/api/csharp/api/Ice.ObjectPrx.html), [Java](https://code.zeroc.com/ice/3.8/api/java/com.zeroc.ice/com/zeroc/Ice/ObjectPrx.html), [Python](https://code.zeroc.com/ice/3.8/api/python/Ice.ObjectPrx.html), and [Swift](https://code.zeroc.com/ice/3.8/api/swift/documentation/ice/objectprx)) invokes an operation by name instead: the application supplies the marshaled in-parameters and receives the marshaled reply. An application that forwards requests it does not unmarshal, or that calls operations without the generated code for the target interface, uses `ice_invoke`.

## Invoking an Operation

`ice_invoke` accepts the following arguments:

- the name of the operation
- the operation mode, `Normal` or `Idempotent`
- an encapsulation holding the marshaled in-parameters
- an optional [request context](https://docs.zeroc.com/ice/3.8/runtime/invocation/request-contexts.md)

> **C++**
>
> `Ice::ObjectPrx` provides a synchronous [`ice_invoke`](https://code.zeroc.com/ice/3.8/api/cpp/classIce_1_1ObjectPrx_ad8e9a5bc7efd45641c1059a3174055ab.html#ad8e9a5bc7efd45641c1059a3174055ab) and asynchronous [`ice_invokeAsync`](https://code.zeroc.com/ice/3.8/api/cpp/classIce_1_1ObjectPrx_a4c456a506fc4db4c7c8a7503dec13282.html#a4c456a506fc4db4c7c8a7503dec13282) overloads that return a future or accept callbacks.

> **C# and Python**
>
> `Ice.ObjectPrx` provides a synchronous `ice_invoke` ([C#](https://code.zeroc.com/ice/3.8/api/csharp/api/Ice.ObjectPrx.html#Ice_ObjectPrx_ice_invoke_) and [Python](https://code.zeroc.com/ice/3.8/api/python/Ice.ObjectPrx.html#Ice.ObjectPrx.ice_invoke)) and an asynchronous `ice_invokeAsync` ([C#](https://code.zeroc.com/ice/3.8/api/csharp/api/Ice.ObjectPrx.html#Ice_ObjectPrx_ice_invokeAsync_) and [Python](https://code.zeroc.com/ice/3.8/api/python/Ice.ObjectPrx.html#Ice.ObjectPrx.ice_invokeAsync)).

> **Java**
>
> `ObjectPrx` provides a synchronous [`ice_invoke`](https://code.zeroc.com/ice/3.8/api/java/com.zeroc.ice/com/zeroc/Ice/ObjectPrx.html#ice_invoke(java.lang.String,com.zeroc.Ice.OperationMode,byte[])) and an asynchronous [`ice_invokeAsync`](https://code.zeroc.com/ice/3.8/api/java/com.zeroc.ice/com/zeroc/Ice/ObjectPrx.html#ice_invokeAsync(java.lang.String,com.zeroc.Ice.OperationMode,byte[])). Their result holds the success flag in its `returnValue` field and the encapsulation in its `outParams` field.

> **Swift**
>
> `ObjectPrx` provides an asynchronous [`ice_invoke`](https://code.zeroc.com/ice/3.8/api/swift/documentation/ice/objectprx/ice_invoke(operation:mode:inencaps:context:)).

### Operation Mode

The operation mode is `Idempotent` for a Slice operation declared [idempotent](https://docs.zeroc.com/ice/3.8/slice/operations.md#idempotent-operations), and `Normal` otherwise. The Ice runtime uses this mode to decide whether it can [retry](https://docs.zeroc.com/ice/3.8/runtime/invocation/automatic-retries.md#automatic-retries-for-idempotent-operations) an invocation that fails after the request was sent, and sends the mode with the request. Pass `Idempotent` when you know the operation is idempotent, or when you forward a request that arrived with this mode.

## Marshaling the In-Parameters

You marshal the in-parameters into an [encapsulation](https://docs.zeroc.com/ice/3.8/encoding/basic-data-encoding.md#encoding-for-encapsulations): the required parameters in the order of their declaration, followed by the optional parameters in the order of their tags, each marshaled as described in [Ice Encoding](https://docs.zeroc.com/ice/3.8/encoding.md). For an operation without in-parameters, you can pass an empty byte sequence; the Ice runtime then sends an empty encapsulation.

> **C++**
>
> You marshal the in-parameters with an `Ice::OutputStream`. The following example invokes the `greet` operation of the `Greeter` interface, which takes a `string` parameter:
>
> ```cpp
> Ice::ObjectPrx greeter{communicator, "greeter:tcp -h localhost -p 4061"};
>
> Ice::OutputStream outputStream{communicator};
> outputStream.startEncapsulation();
> outputStream.write("alice");
> outputStream.endEncapsulation();
> std::vector<std::byte> inParams;
> outputStream.finished(inParams);
>
> std::vector<std::byte> outParams;
> bool ok = greeter.ice_invoke("greet", Ice::OperationMode::Normal, inParams, outParams);
> ```

> **C#**
>
> You marshal the in-parameters with an `OutputStream`. The following example invokes the `greet` operation of the `Greeter` interface, which takes a `string` parameter:
>
> ```csharp
> ObjectPrx greeter = ObjectPrxHelper.createProxy(
>     communicator,
>     "greeter:tcp -h localhost -p 4061");
>
> var outputStream = new OutputStream(communicator);
> outputStream.startEncapsulation();
> outputStream.writeString("alice");
> outputStream.endEncapsulation();
> byte[] inEncaps = outputStream.finished();
>
> Object_Ice_invokeResult result = await greeter.ice_invokeAsync(
>     "greet",
>     OperationMode.Normal,
>     inEncaps);
> ```

> **Java**
>
> You marshal the in-parameters with an `OutputStream`. The following example invokes the `greet` operation of the `Greeter` interface, which takes a `string` parameter:
>
> ```java
> ObjectPrx greeter = ObjectPrx.createProxy(communicator, "greeter:tcp -h localhost -p 4061");
>
> var outputStream = new OutputStream(communicator);
> outputStream.startEncapsulation();
> outputStream.writeString("alice");
> outputStream.endEncapsulation();
> byte[] inParams = outputStream.finished();
>
> com.zeroc.Ice.Object.Ice_invokeResult result =
>     greeter.ice_invoke("greet", OperationMode.Normal, inParams);
> ```

> **Python**
>
> The Python mapping has no public stream API, so a Python application passes an encapsulation that it received already marshaled, such as the in-parameters of a request that it forwards.
>
> ```py
> proxy = Ice.ObjectPrx(communicator, "greeter:tcp -h localhost -p 4061")
> ok, outParams = await proxy.ice_invokeAsync("greet", Ice.OperationMode.Normal, inParams)
> ```

> **Swift**
>
> You marshal the in-parameters with an `OutputStream`. The following example invokes the `greet` operation of the `Greeter` interface, which takes a `string` parameter:
>
> ```swift
> let greeter = try makeProxy(
>     communicator: communicator,
>     proxyString: "greeter:tcp -h localhost -p 4061",
>     type: ObjectPrx.self)
>
> let outputStream = OutputStream(communicator: communicator)
> outputStream.startEncapsulation()
> outputStream.write("alice")
> outputStream.endEncapsulation()
> let inEncaps = outputStream.finished()
>
> let (ok, outEncaps) = try await greeter.ice_invoke(
>     operation: "greet",
>     mode: .normal,
>     inEncaps: inEncaps)
> ```

> **C++, C#, Java, and Swift**
>
> [Input and Output Streams](https://docs.zeroc.com/ice/3.8/runtime/input-and-output-streams.md) shows how to read and write each kind of Slice type with these streams.

## Reading the Reply

For a twoway invocation, `ice_invoke` returns a success flag and an encapsulation:

- When the flag is `true`, the operation completed successfully, and the encapsulation holds the required out-parameters, followed by the required return value, followed by the optional out-parameters and optional return value in the order of their tags.
- When the flag is `false`, the operation completed with a user exception, and the encapsulation holds the [marshaled user exception](https://docs.zeroc.com/ice/3.8/encoding/data-encoding-for-exceptions.md).

For any other failure, `ice_invoke` throws a [local or dispatch exception](https://docs.zeroc.com/ice/3.8/runtime/local-and-dispatch-exceptions.md).

For a oneway or datagram invocation, `ice_invoke` returns `true` and an empty byte sequence once the transport accepts the request. For a batch oneway or batch datagram invocation, it returns `true` and an empty byte sequence once it adds the request to the batch; the Ice runtime sends the request when it [flushes](https://docs.zeroc.com/ice/3.8/runtime/invocation/invocation-mode/batched-invocations.md) the batch.

> **C++**
>
> You unmarshal the reply with an `Ice::InputStream`. `throwException` unmarshals a user exception and throws it:
>
> ```cpp
> Ice::InputStream inputStream{communicator, outParams};
> inputStream.startEncapsulation();
> if (ok)
> {
>     std::string greeting;
>     inputStream.read(greeting);
>     inputStream.endEncapsulation();
> }
> else
> {
>     try
>     {
>         inputStream.throwException();
>     }
>     catch (const Ice::UserException&)
>     {
>         inputStream.endEncapsulation();
>         throw;
>     }
> }
> ```
>
> With the callback overload of `ice_invokeAsync`, the Ice runtime does not call `response` for a oneway or datagram invocation: it calls `sent` once the transport accepts the request. When it adds a request to a batch, it calls none of the callbacks.

> **C#**
>
> You unmarshal the reply with an `InputStream`. `throwException` unmarshals a user exception and throws it:
>
> ```csharp
> var inputStream = new InputStream(communicator, result.outEncaps);
> inputStream.startEncapsulation();
> if (result.returnValue)
> {
>     string greeting = inputStream.readString();
>     inputStream.endEncapsulation();
> }
> else
> {
>     try
>     {
>         inputStream.throwException();
>     }
>     catch (UserException)
>     {
>         inputStream.endEncapsulation();
>         throw;
>     }
> }
> ```

> **Java**
>
> You unmarshal the reply with an `InputStream`. `throwException` unmarshals a user exception and throws it:
>
> ```java
> var inputStream = new InputStream(communicator, result.outParams);
> inputStream.startEncapsulation();
> if (result.returnValue) {
>     String greeting = inputStream.readString();
>     inputStream.endEncapsulation();
> } else {
>     try {
>         inputStream.throwException();
>     } catch (UserException ex) {
>         inputStream.endEncapsulation();
>         throw ex;
>     }
> }
> ```

> **Swift**
>
> You unmarshal the reply with an `InputStream`. `throwException` unmarshals a user exception and throws it:
>
> ```swift
> let inputStream = InputStream(communicator: communicator, bytes: outEncaps)
> try inputStream.startEncapsulation()
> if ok {
>     let greeting: String = try inputStream.read()
>     try inputStream.endEncapsulation()
> } else {
>     do {
>         try inputStream.throwException()
>     } catch let error as UserException {
>         try inputStream.endEncapsulation()
>         throw error
>     }
> }
> ```

## Forwarding Requests

An application can forward the requests that its object adapter receives with `ice_invoke`, without unmarshaling their parameters. The forwarding code passes the encapsulation of the in-parameters from the incoming request to `ice_invoke` on the target proxy, and builds the response from the success flag and the encapsulation that `ice_invoke` returns. The operation name, mode and [context](https://docs.zeroc.com/ice/3.8/runtime/invocation/request-contexts.md) of the outgoing request are the forwarder's to set: a forwarder usually passes on those of the incoming request, and an intermediary such as Glacier2 adds or removes context entries along the way.

> **C++**
>
> A forwarding dispatcher derives from `Ice::Object` and overrides `dispatch`.
>
> The [Ice forwarder demo](https://github.com/zeroc-ice/ice-demos/tree/3.8/cpp/Ice/forwarder) provides a complete example of a forwarding server in C++.

> **C#**
>
> A forwarding dispatcher implements `Ice.Object` and its `dispatchAsync` method.
>
> The [Ice forwarder demo](https://github.com/zeroc-ice/ice-demos/tree/3.8/csharp/Ice/Forwarder) provides a complete example of a forwarding server in C#.

> **Java**
>
> A forwarding dispatcher implements `com.zeroc.Ice.Object` and its `dispatch` method.
>
> The [Ice forwarder demo](https://github.com/zeroc-ice/ice-demos/tree/3.8/java/Ice/forwarder) provides a complete example of a forwarding server in Java.

> **Python**
>
> A forwarding servant derives from `Ice.Blobject` and implements its `ice_invoke` method, which receives the encapsulation of the in-parameters and returns the success flag and the encapsulation of the reply, or an awaitable for them, such as the one that `ice_invokeAsync` returns.

> **Swift**
>
> A forwarding dispatcher adopts the `Dispatcher` protocol.
>
> The [Ice forwarder demo](https://github.com/zeroc-ice/ice-demos/tree/3.8/swift/Ice/Forwarder) provides a complete example of a forwarding server in Swift.

## See Also

- [Dispatcher API](https://docs.zeroc.com/ice/3.8/runtime/dispatch/dispatcher-api.md)
- [Basic Data Encoding](https://docs.zeroc.com/ice/3.8/encoding/basic-data-encoding.md)
- [Reply Message Body](https://docs.zeroc.com/ice/3.8/protocol/protocol-messages.md#reply-message-body)
