You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
gRPC protocol is message oriented and in server streaming calls does not allow to pass back to a caller external information.
The only way to achieve this is by using response headers.
As an example file download from server. Together with content i need also file creation date, owner information and file version:
[OperationContract]Task<IAsyncEnumerable<byte[]>>DownloadFile(stringfileName,CallContextcontext);// server-side implementationasyncTask<IAsyncEnumerable<byte[]>>DownloadFile(stringfileName,CallContextcontext){DateTimefileCreationDate= ...// resolve file creation date
string fileOwner = ...// resolve owner information
string fileVersion = ...// resolve file version// pass external information in response headers
ServerCallContext serverContext =context;await serverContext.WriteResponseHeadersAsync(new Metadata
{{"creation-date", fileCreationDate.ToString()},{"owner", fileOwner },{"version", fileVersion }});// pass file content via server streamingreturn ReadFileContent(fileName);}privateIAsyncEnumerable<byte[]>ReadFileContent(stringfileName){// convert file content to gRPC byte[] stream// the code is for demonstration purposes only !!!using(varstream= File.OpenRead(fileName)){varbuffer=newbyte[4*1204];while((await stream.ReadAsync(buffer,0, buffer.Length))>0){yieldreturnbuffer;}}}// client-side implementationvarcontext=new CallContext();varcontent=await DownloadFile("file name", context);// extract external information from response headersvarfileCreationDate= context.ResponseHeaders.First(i => i.Key =="creation-date").Value;varfileOwner= context.ResponseHeaders.First(i => i.Key =="owner").Value;varfileVersion= context.ResponseHeaders.First(i => i.Key =="version").Value;// download file contentawaitforeach(var buffer in content){// ...}
Idea
The idea is to move "response headers" related implementation into ServiceModel.Grpc framework:
// no CallContext in the call, only CancellationToken// pass external information in the method return type[OperationContract]Task<(DateTime CreationDate,string Owner,string Version,IAsyncEnumerable<byte[]> Content)>DownloadFile(stringfileName,CancellationTokentoken);// server-side implementationasyncTask<(DateTime,string,string,IAsyncEnumerable<byte[]>)>DownloadFile(stringfileName,CancellationTokentoken){DateTimefileCreationDate= ...// resolve file creation date
string fileOwner = ...// resolve owner information
string fileVersion = ...// resolve file versionreturn(fileCreationDate, fileOwner, version, ReadFileContent(fileName));}// client-side implementationvar(fileCreationDate, fileOwner, fileVersion, content)=await DownloadFile("file name", CancellationToken);// download file contentawaitforeach(var buffer in content){// ...}
The text was updated successfully, but these errors were encountered:
Problem
gRPC protocol is message oriented and in server streaming calls does not allow to pass back to a caller external information.
The only way to achieve this is by using response headers.
As an example file download from server. Together with content i need also file creation date, owner information and file version:
Idea
The idea is to move "response headers" related implementation into ServiceModel.Grpc framework:
The text was updated successfully, but these errors were encountered: