'access variables inside asp.net-core grpc service class from different thread
I've generated c# code from a .proto-file for grpc communication. My proto file contains a service:
syntax = "proto3";
option csharp_namespace = "GrpcEmployee";
service ClientFileStreamService {
rpc ReceiveFileChunks(stream FileChunks) returns (FileResponse);
}
message FileChunks {
bytes chunk = 1;
int64 timestamp = 2;
int64 token = 3;
int32 totalbytes=4;
int32 sentbytes=5;
}
message FileResponse {
int64 token = 1;
uint32 receivedbytes=2;
}
and I've created the following C# code to be used on the grpc server side:
public class ClientFileStreamServe : ClientFileStreamService.ClientFileStreamServiceBase
{
private readonly ILogger<ClientFileStreamServe> _logger;
public ClientFileStreamServe(ILogger<ClientFileStreamServe> logger)
{
_logger = logger;
}
public override async Task<FileResponse> ReceiveFileChunks(IAsyncStreamReader<FileChunks> request, ServerCallContext context)
{
System.IO.MemoryStream total_message_stream = new System.IO.MemoryStream();
long token = 0;
await foreach (var message in request.ReadAllAsync())
{
var bytearraychunk = request.Current.Chunk.ToByteArray();
total_message_stream.Write(bytearraychunk, 0, bytearraychunk.Length);
token = request.Current.Token;
}
var full_file = total_message_stream.ToArray(); // I WANT TO ACCESS THIS FROM A DIFFERENT THREAD
_logger.LogInformation("Received {} bytes, last token {}", full_file.Length, token);
return new FileResponse { Receivedbytes = full_file.Length, Token = token };
}
}
In the third last code line there is a variable full_file which I would like to access from a different thread (probably via a Getter). Preferable I would just get a reference to the instance of the class ClientFileStreamServe and go from there, but here I struggle. I started my tests off with the example from https://docs.microsoft.com/en-us/aspnet/core/grpc/?view=aspnetcore-6.0 where ASP.NET core is used.
There is a file startup.cs which registers my class CLientFileStreamServe as an endpoint like this:
endpoints.MapGrpcService<GrpcEmployee.ClientFileStreamServe>();
Since I don't have an instance of ClientFileStreamServe here, I can't access it. Also it seems that ClientFileStreamServe is instantiated every time a http-call is made to the server.
How can I access the variable full_file or how can I pass information to a different thread in my program? I had a look at named pipes, but is this the only way?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
