예제 #1
0
        /// <summary>Snippet for Write</summary>
        public async Task Write()
        {
            // Snippet: Write(CallSettings, BidirectionalStreamingSettings)
            // Create client
            FirestoreClient firestoreClient = FirestoreClient.Create();

            // Initialize streaming call, retrieving the stream object
            FirestoreClient.WriteStream response = firestoreClient.Write();

            // Sending requests and retrieving responses can be arbitrarily interleaved
            // Exact sequence will depend on client/server behavior

            // Create task to do something with responses from server
            Task responseHandlerTask = Task.Run(async() =>
            {
                // Note that C# 8 code can use await foreach
                AsyncResponseStream <WriteResponse> responseStream = response.GetResponseStream();
                while (await responseStream.MoveNextAsync())
                {
                    WriteResponse responseItem = responseStream.Current;
                    // Do something with streamed response
                }
                // The response stream has completed
            });

            // Send requests to the server
            bool done = false;

            while (!done)
            {
                // Initialize a request
                WriteRequest request = new WriteRequest
                {
                    Database    = "",
                    StreamId    = "",
                    Writes      = { new Write(), },
                    StreamToken = ByteString.Empty,
                    Labels      = { { "", "" }, },
                };
                // Stream a request to the server
                await response.WriteAsync(request);

                // Set "done" to true when sending requests is complete
            }

            // Complete writing requests to the stream
            await response.WriteCompleteAsync();

            // Await the response handler
            // This will complete once all server responses have been processed
            await responseHandlerTask;
            // End snippet
        }
        /// <summary>Snippet for Write</summary>
        public async Task Write()
        {
            // Snippet: Write(CallSettings,BidirectionalStreamingSettings)
            // Create client
            FirestoreClient firestoreClient = FirestoreClient.Create();

            // Initialize streaming call, retrieving the stream object
            FirestoreClient.WriteStream duplexStream = firestoreClient.Write();

            // Sending requests and retrieving responses can be arbitrarily interleaved.
            // Exact sequence will depend on client/server behavior.

            // Create task to do something with responses from server
            Task responseHandlerTask = Task.Run(async() =>
            {
                IAsyncEnumerator <WriteResponse> responseStream = duplexStream.ResponseStream;
                while (await responseStream.MoveNext())
                {
                    WriteResponse response = responseStream.Current;
                    // Do something with streamed response
                }
                // The response stream has completed
            });

            // Send requests to the server
            bool done = false;

            while (!done)
            {
                // Initialize a request
                WriteRequest request = new WriteRequest
                {
                    Database = new DatabaseRootName("[PROJECT]", "[DATABASE]").ToString(),
                };
                // Stream a request to the server
                await duplexStream.WriteAsync(request);

                // Set "done" to true when sending requests is complete
            }
            // Complete writing requests to the stream
            await duplexStream.WriteCompleteAsync();

            // Await the response handler.
            // This will complete once all server responses have been processed.
            await responseHandlerTask;
            // End snippet
        }