# Class BidirectionalStreamingBase&lt;TRequest, TResponse&gt; (4.10.0)

<button class="button button-with-icon cloud-select-dropdown__button " aria-haspopup="true">Version latest</button>

- [4.10.0 (latest)](https://docs.cloud.google.com/dotnet/docs/reference/Google.Api.Gax/latest/Google.Api.Gax.Grpc.BidirectionalStreamingBase-2)
- [4.8.0](https://docs.cloud.google.com/dotnet/docs/reference/Google.Api.Gax/4.8.0/Google.Api.Gax.Grpc.BidirectionalStreamingBase-2)
- [4.4.0](https://docs.cloud.google.com/dotnet/docs/reference/Google.Api.Gax/4.4.0/Google.Api.Gax.Grpc.BidirectionalStreamingBase-2)
- [4.3.1](https://docs.cloud.google.com/dotnet/docs/reference/Google.Api.Gax/4.3.1/Google.Api.Gax.Grpc.BidirectionalStreamingBase-2)
- [4.2.0](https://docs.cloud.google.com/dotnet/docs/reference/Google.Api.Gax/4.2.0/Google.Api.Gax.Grpc.BidirectionalStreamingBase-2)
- [4.0.0](https://docs.cloud.google.com/dotnet/docs/reference/Google.Api.Gax/4.0.0/Google.Api.Gax.Grpc.BidirectionalStreamingBase-2)
- [3.2.0](https://docs.cloud.google.com/dotnet/docs/reference/Google.Api.Gax/3.2.0/Google.Api.Gax.Grpc.BidirectionalStreamingBase-2)

    public abstract class BidirectionalStreamingBase<TRequest, TResponse> : IDisposable

Base class for bidirectional streaming RPC methods. This wraps an underlying call returned by gRPC,
in order to provide a wrapper for the async response stream, allowing users to take advantage
of

    await foreach

support from C# 8 onwards. Additionally, it wraps the request stream in a buffer, allowing multiple requests to be written without waiting for them to be transmitted.

<br />

## Inheritance

[object](https://learn.microsoft.com/dotnet/api/system.object) \> BidirectionalStreamingBase\<TRequest, TResponse\>

## Implements

[IDisposable](https://learn.microsoft.com/dotnet/api/system.idisposable)

## Inherited Members

[object.Equals(object)](https://learn.microsoft.com/dotnet/api/system.object.equals#system-object-equals(system-object)) [object.Equals(object, object)](https://learn.microsoft.com/dotnet/api/system.object.equals#system-object-equals(system-object-system-object)) [object.GetHashCode()](https://learn.microsoft.com/dotnet/api/system.object.gethashcode) [object.GetType()](https://learn.microsoft.com/dotnet/api/system.object.gettype) [object.MemberwiseClone()](https://learn.microsoft.com/dotnet/api/system.object.memberwiseclone) [object.ReferenceEquals(object, object)](https://learn.microsoft.com/dotnet/api/system.object.referenceequals) [object.ToString()](https://learn.microsoft.com/dotnet/api/system.object.tostring)

## Namespace

[Google.Api.Gax.Grpc](https://docs.cloud.google.com/dotnet/docs/reference/Google.Api.Gax/latest/Google.Api.Gax.Grpc)

## Assembly

Google.Api.Gax.Grpc.dll

| ## Type Parameters ||
|---|---|
| **Name** | **Description** |
| `TRequest` | RPC request type |
| `TResponse` | RPC response type |

## Remarks

To avoid memory leaks, users must dispose of gRPC streams.
Additionally, you are strongly advised to read the whole response stream, even if the data
is not required - this avoids effectively cancelling the call.

## Properties

### GrpcCall

    public virtual AsyncDuplexStreamingCall<TRequest, TResponse> GrpcCall { get; }

The underlying gRPC duplex streaming call.
Warning: DO NOT USE `GrpcCall.RequestStream` at all if using
[TryWriteAsync(TRequest)](https://docs.cloud.google.com/dotnet/docs/reference/Google.Api.Gax/latest/Google.Api.Gax.Grpc.BidirectionalStreamingBase-2#Google_Api_Gax_Grpc_BidirectionalStreamingBase_2_TryWriteAsync__0_), [WriteAsync(TRequest)](https://docs.cloud.google.com/dotnet/docs/reference/Google.Api.Gax/latest/Google.Api.Gax.Grpc.BidirectionalStreamingBase-2#Google_Api_Gax_Grpc_BidirectionalStreamingBase_2_WriteAsync__0_),
[TryWriteAsync(TRequest, WriteOptions)](https://docs.cloud.google.com/dotnet/docs/reference/Google.Api.Gax/latest/Google.Api.Gax.Grpc.BidirectionalStreamingBase-2#Google_Api_Gax_Grpc_BidirectionalStreamingBase_2_TryWriteAsync__0_Grpc_Core_WriteOptions_) , or [WriteAsync(TRequest, WriteOptions)](https://docs.cloud.google.com/dotnet/docs/reference/Google.Api.Gax/latest/Google.Api.Gax.Grpc.BidirectionalStreamingBase-2#Google_Api_Gax_Grpc_BidirectionalStreamingBase_2_WriteAsync__0_Grpc_Core_WriteOptions_).
Doing so will cause conflict with the write-buffer used within the `[Try]WriteAsync` methods.

| **Property Value** ||
|---|---|
| **Type** | **Description** |
| `https://github.com/grpc/grpc-dotnet/blob/7c43ddb5d68008782dc0dba2d0feaa3ed91a9fb2/src/Grpc.Core.Api/AsyncDuplexStreamingCall.cs` |   |

## Methods

### Dispose()

    public virtual void Dispose()

Disposes of the underlying gRPC call. There is no need to dispose of both the wrapper
and the underlying call; it's typically simpler to dispose of the wrapper with a

    using

statement as the wrapper is returned by client libraries. **Remarks** The default implementation just calls Dispose on the result of [GrpcCall](https://docs.cloud.google.com/dotnet/docs/reference/Google.Api.Gax/latest/Google.Api.Gax.Grpc.BidirectionalStreamingBase-2#Google_Api_Gax_Grpc_BidirectionalStreamingBase_2_GrpcCall).

### GetResponseStream()

    public virtual AsyncResponseStream<TResponse> GetResponseStream()

Async stream to read streaming responses, exposed as an async sequence.
The default implementation will use [GrpcCall](https://docs.cloud.google.com/dotnet/docs/reference/Google.Api.Gax/latest/Google.Api.Gax.Grpc.BidirectionalStreamingBase-2#Google_Api_Gax_Grpc_BidirectionalStreamingBase_2_GrpcCall) to extract a response
stream, and adapt it to [AsyncResponseStream\<TResponse\>](https://docs.cloud.google.com/dotnet/docs/reference/Google.Api.Gax/latest/Google.Api.Gax.Grpc.AsyncResponseStream-1).

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/dotnet/docs/reference/Google.Api.Gax/latest/Google.Api.Gax.Grpc.AsyncResponseStream-1` |   |

**Remarks** If this method is called more than once, all the returned enumerators will be enumerating over the
same underlying response stream, which may cause confusion. Additionally, the sequence returned by
this method can only be iterated over a single time. Attempting to iterate more than once will cause
an [InvalidOperationException](https://learn.microsoft.com/dotnet/api/system.invalidoperationexception).

### TryWriteAsync(TRequest)

    public virtual Task TryWriteAsync(TRequest message)

Writes a message to the stream, if there is enough space in the buffer and [WriteCompleteAsync()](https://docs.cloud.google.com/dotnet/docs/reference/Google.Api.Gax/latest/Google.Api.Gax.Grpc.BidirectionalStreamingBase-2#Google_Api_Gax_Grpc_BidirectionalStreamingBase_2_WriteCompleteAsync)
hasn't already been called. The same write options will be used as for the previous message.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `message` | `TRequest` The message to write. |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.threading.tasks.task` | `null` if the message queue is full or the stream has already been completed; otherwise, a [Task](https://learn.microsoft.com/dotnet/api/system.threading.tasks.task) which will complete when the message has been written to the stream. |

### TryWriteAsync(TRequest, WriteOptions)

    public virtual Task TryWriteAsync(TRequest message, WriteOptions options)

Writes a message to the stream, if there is enough space in the buffer and [WriteCompleteAsync()](https://docs.cloud.google.com/dotnet/docs/reference/Google.Api.Gax/latest/Google.Api.Gax.Grpc.BidirectionalStreamingBase-2#Google_Api_Gax_Grpc_BidirectionalStreamingBase_2_WriteCompleteAsync)
hasn't already been called.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `message` | `TRequest` The message to write. |
| `options` | `https://github.com/grpc/grpc-dotnet/blob/7c43ddb5d68008782dc0dba2d0feaa3ed91a9fb2/src/Grpc.Core.Api/WriteOptions.cs` The write options to use for this message. |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.threading.tasks.task` | `null` if the message queue is full or the stream has already been completed. |

### TryWriteCompleteAsync()

    public virtual Task TryWriteCompleteAsync()

Completes the stream when all buffered messages have been sent.
Only the first call to this method on any instance will have any effect;
subsequent calls will return `null`.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.threading.tasks.task` | A [Task](https://learn.microsoft.com/dotnet/api/system.threading.tasks.task) which will complete when the stream has finished being completed; or `null` if this method has already been called. |

### WriteAsync(TRequest)

    public virtual Task WriteAsync(TRequest message)

Writes a message to the stream, if there is enough space in the buffer and [WriteCompleteAsync()](https://docs.cloud.google.com/dotnet/docs/reference/Google.Api.Gax/latest/Google.Api.Gax.Grpc.BidirectionalStreamingBase-2#Google_Api_Gax_Grpc_BidirectionalStreamingBase_2_WriteCompleteAsync)
hasn't already been called. The same write options will be used as for the previous message.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `message` | `TRequest` The message to write. |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.threading.tasks.task` | A [Task](https://learn.microsoft.com/dotnet/api/system.threading.tasks.task) which will complete when the message has been written to the stream. |

| **Exceptions** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.invalidoperationexception` | There isn't enough space left in the buffer, or [WriteCompleteAsync()](https://docs.cloud.google.com/dotnet/docs/reference/Google.Api.Gax/latest/Google.Api.Gax.Grpc.BidirectionalStreamingBase-2#Google_Api_Gax_Grpc_BidirectionalStreamingBase_2_WriteCompleteAsync) has already been called. |

### WriteAsync(TRequest, WriteOptions)

    public virtual Task WriteAsync(TRequest message, WriteOptions options)

Writes a message to the stream, if there is enough space in the buffer and [WriteCompleteAsync()](https://docs.cloud.google.com/dotnet/docs/reference/Google.Api.Gax/latest/Google.Api.Gax.Grpc.BidirectionalStreamingBase-2#Google_Api_Gax_Grpc_BidirectionalStreamingBase_2_WriteCompleteAsync)
hasn't already been called.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `message` | `TRequest` The message to write. |
| `options` | `https://github.com/grpc/grpc-dotnet/blob/7c43ddb5d68008782dc0dba2d0feaa3ed91a9fb2/src/Grpc.Core.Api/WriteOptions.cs` The write options to use for this message. |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.threading.tasks.task` | A [Task](https://learn.microsoft.com/dotnet/api/system.threading.tasks.task) which will complete when the message has been written to the stream. |

| **Exceptions** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.invalidoperationexception` | There isn't enough space left in the buffer, or [WriteCompleteAsync()](https://docs.cloud.google.com/dotnet/docs/reference/Google.Api.Gax/latest/Google.Api.Gax.Grpc.BidirectionalStreamingBase-2#Google_Api_Gax_Grpc_BidirectionalStreamingBase_2_WriteCompleteAsync) has already been called. |

### WriteCompleteAsync()

    public virtual Task WriteCompleteAsync()

Completes the stream when all buffered messages have been sent. This method can only be called
once, and further messages cannot be written after it has been called.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.threading.tasks.task` | A [Task](https://learn.microsoft.com/dotnet/api/system.threading.tasks.task) which will complete when the stream has finished being completed. |

| **Exceptions** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.invalidoperationexception` | This method has already been called. |