---
title: "Operation"
description: "Learn how to define operations in Slice."
url: https://docs.icerpc.dev/slice/language-guide/operation
docs_index: https://docs.icerpc.dev/llms.txt
---

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

# Operation

Learn how to define operations in Slice.

## Anatomy of an operation

An operation consists of:

- operation [attributes](https://docs.icerpc.dev/slice/language-guide/attributes.md) (optional)
- a name (the name of the operation)
- a list of [parameters](https://docs.icerpc.dev/slice/language-guide/parameters.md) (the operation parameters)
- an arrow followed by one or more return [parameters](https://docs.icerpc.dev/slice/language-guide/parameters.md) (optional)

For example:

```slice
greet(name: string) -> string
```

Operation `greet` has a name (`greet`), an operation parameter (`name`), and a nameless return parameter.

Here are a few additional examples:

```slice
op() // no operation parameter

[oneway] op(x: int32) // an attribute and a single operation parameter

op(x: int32, y: int32) -> int32 // two operation parameters and a return parameter

// Another way to write the operation above
op(
    x: int32
    y: int32
) -> int32

opNoReturn() // returns nothing (no ->)

opReturnString() -> string // returns a string

opReturnPair() -> (x: int32, y: int32) // returns two return parameters
```

## Tagged parameters

The operation parameters and return parameters can include [tagged parameters](https://docs.icerpc.dev/slice/language-guide/parameters.md#tagged-parameters). For example:

```slice
// An operation with many tagged parameters.
op(
    tag(5) x: int64?
    is: string
) -> (
    tag(5) x: int32?
    y: int32?
    tag(1) s: string?
)
```

A single nameless return parameter can also get tagged. For example:

```slice
// returns a tagged string
op() -> tag(1) string?
```

## Idempotent operation

An operation can be marked as idempotent, which means calling this operation several times with the same arguments is semantically equivalent to calling this operation once.

For example:

```slice
idempotent setTemperature(newValue: float64)
```

Setting the temperature over-and-over to the same value is like setting it once.

The `idempotent` keyword ensures that both the caller and the implementation of the service have compatible understandings of the idempotent-ness of this operation. If the caller believes an operation is idempotent, the service implementation must see and treat this operation as idempotent too.

## Operation-specific attributes

### compress attribute

The `compress` [attribute](https://docs.icerpc.dev/slice/language-guide/attributes.md) instructs the generated code to request compression when sending the arguments or return value of the operation. Its argument can be `Args`, `Return`, or both.

For example:

```slice
interface Greeter {
    // Request compression in both directions.
    [compress(Args, Return)] greet(name: string) -> string
}
```

```slice
interface FileServer {
    // Request compression when sending the return value.
    [compress(Return)] getTextFile(name: string) -> stream string
}
```

In C# with IceRPC, the generated code sets the [ICompressFeature](https://code.icerpc.dev/csharp/0.7.x/api/reference/IceRpc.Features.ICompressFeature.html) in the outgoing request features (for `Args`) or in the incoming request features (for `Return`).

This compression request is typically fulfilled by the compressor interceptor or middleware, which needs to be installed in your invocation resp. dispatch pipeline. If you neglect to install this interceptor or middleware, the corresponding payloads are not compressed.

Keep in mind the compressor interceptor and middleware require the icerpc protocol. They do nothing for ice invocations and dispatches.

### oneway attribute

The `oneway` [attribute](https://docs.icerpc.dev/slice/language-guide/attributes.md) instructs the generated code to create one-way outgoing requests for this operation. It has no effect on the server-side generated code (the I*Name*Service interface).

A one-way request is a "fire and forget" request: the request is considered successful as soon as it's sent successfully. This attribute can only be applied to operations with no return type. It does not accept any argument. For example:

```slice
interface Logger {
    [oneway] logMessage(message: string)
}
```

## C# mapping

*This section is specific to the [IceRPC + Slice integration](https://docs.icerpc.dev/slice.md#slice-and-icerpc).*

A Slice operation named *opName* in interface `Greeter` is mapped to abstract method *OpName*Async in the interface `IGreeter` and to abstract method *OpName*Async in interface `IGreeterService`.

The mapped method name is always in Pascal case, per the usual C# naming conventions, even when *opName* is not in Pascal case.

For example:

```slice
module VisitorCenter

// An interface with a single operation.
interface Greeter {
    greet(name: string) -> string
}
```

```csharp
namespace VisitorCenter;

internal partial interface IGreeter
{
    Task<string> GreetAsync(
        string name,
        IFeatureCollection? features = null,
        CancellationToken cancellationToken = default);
}

internal partial interface IGreeterService
{
    ValueTask<string> GreetAsync(
        string name,
        IFeatureCollection features,
        CancellationToken cancellationToken);
}
```

While the two methods are similar, please note they are not the same:

- the client-side method returns a `Task` or `Task<T>` while the service method returns a `ValueTask` or `ValueTask<T>`
- the `features` parameter is nullable and defaults to null only in the client-side method
- the cancellation token parameter has a default value only in the client-side method

### Request and Response helper classes

The code generator generates helper nested static classes named Request and Response in *Name*Proxy and I*Name*Service. These nested classes provide helper methods to encode and decode the payloads of requests and responses associated with the interface operations, with 4 helper methods per operation.

For example:

```slice
module VisitorCenter

interface Greeter {
    greet(name: string) -> string
}
```

produces 4 helper methods:

```csharp
internal readonly partial record struct GreeterProxy : IGreeter, ISliceProxy
{
    internal static class Request
    {
        // Encodes the name argument into a request payload (a PipeReader).
        internal static PipeReader EncodeGreet(string name, SliceEncodeOptions? encodeOptions = null)
        {
            ...
        }
    }

    internal static class Response
    {
        // Decodes the response payload into a string (the greeting).
        internal static ValueTask<string> DecodeGreetAsync(
            IncomingResponse response,
            OutgoingRequest request,
            ISliceProxy sender,
            CancellationToken cancellationToken)
        {
            ...
        }
    }
}

internal partial interface IGreeterService
{
    internal static class Request
    {
        // Decodes the name argument from the request payload.
        internal static ValueTask<string> DecodeGreetAsync(
            IncomingRequest request,
            CancellationToken cancellationToken)
        {
            ...
        }
    }

    internal static class Response
    {
        // Encodes the greeting return value into a response payload.
        internal static PipeReader EncodeGreet(string returnValue, SliceEncodeOptions? encodeOptions = null)
        {
            ...
        }
    }
}
```

> **Note**
>
> If your operation has a stream parameter, the encode helper (in *Name*Proxy.Request) does not encode the stream argument; instead, *Name*Proxy.Request provides an additional EncodeStreamOf*OpName* helper that encodes the stream argument into a [PipeReader](https://learn.microsoft.com/en-us/dotnet/api/system.io.pipelines.pipereader) for the `PayloadContinuation` of the outgoing request. The decode helper (in I*Name*Service.Request) decodes all arguments, including the stream.
>
> Likewise, if your operation returns a stream, the encode helper (in I*Name*Service.Response) does not encode the stream return value; instead, I*Name*Service.Response provides an additional EncodeStreamOf*OpName* helper that encodes the stream return value into a [PipeReader](https://learn.microsoft.com/en-us/dotnet/api/system.io.pipelines.pipereader) for the `PayloadContinuation` of the outgoing response. The decode helper (in *Name*Proxy.Response) decodes everything.

These helper methods allow you to create/consume plain IceRPC requests and responses while still using the generated code for their payloads.

### cs::encodedReturn attribute

The `cs::encodedReturn` attribute allows you to change the return type of the mapped method on the generated Service interface: this attribute makes the method return a `ValueTask<PipeReader>` instead of the usual `ValueTask<T>`.

The returned [PipeReader](https://learn.microsoft.com/en-us/dotnet/api/system.io.pipelines.pipereader) represents the encoded return value. You would typically produce this value using the Encode*OpName* method provided by the helper [`Response` class](#request-and-response-helper-classes).

If the operation also returns a stream, only the non-stream return values are replaced by this [PipeReader](https://learn.microsoft.com/en-us/dotnet/api/system.io.pipelines.pipereader): the method returns a tuple with the encoded return value and the stream, such as `ValueTask<(PipeReader Payload, IAsyncEnumerable<float> Samples)>` for an operation that returns `(count: int32, samples: stream float32)`. The `cs::encodedReturn` attribute can't be applied to an operation that returns nothing or only a stream.

There are two somewhat common use-cases for this attribute:

1. You want to encode a mutable collection field of your class (such as `List<T>`) while holding a mutex lock; this lock prevents other operations from modifying this field while it's being encoded.
2. You want to return over and over the same return value that is costly to encode; this attribute allows you to encode the return value once, cache the encoded bytes and then return over and over these bytes.
