---
title: "Dynamic Invocation"
url: https://docs.zeroc.com/ice/3.8/runtime/invocation/dynamic-invocation
language: csharp
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

Programming language: C#. Other languages: [C++](https://docs.zeroc.com/ice/3.8/runtime/invocation/dynamic-invocation/cpp.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), [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`](https://code.zeroc.com/ice/3.8/api/csharp/api/Ice.ObjectPrx.html#Ice_ObjectPrx_ice_invoke_) method of [ObjectPrx](https://code.zeroc.com/ice/3.8/api/csharp/api/Ice.ObjectPrx.html) 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/csharp.md)

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

### Operation Mode

The operation mode is `Idempotent` for a Slice operation declared [idempotent](https://docs.zeroc.com/ice/3.8/slice/operations/csharp.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/csharp.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.

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);
```

[Input and Output Streams](https://docs.zeroc.com/ice/3.8/runtime/input-and-output-streams/csharp.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/csharp.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/csharp.md) the batch.

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;
    }
}
```

## 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/csharp.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.

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#.

## See Also

- [Dispatcher API](https://docs.zeroc.com/ice/3.8/runtime/dispatch/dispatcher-api/csharp.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/csharp.md#reply-message-body)
