简体   繁体   English

在 C# 示例中从客户端向服务器 gRPC 发送文件

[英]Sending a file from client to server gRPC in C# Example

Sending any type of file through gRPC with C#使用 C# 通过 gRPC 发送任何类型的文件

I didn't find a proper example of gRPC sending a file so I wrote the code and shared it with the community .我没有找到 gRPC 发送文件的合适示例,所以我编写了代码并与社区共享

Client Code客户代码

{
using var channel = GrpcChannel.ForAddress("http://localhost:5242");
var fileService = new IPaperService.IPaperServiceClient(channel);
byte[] file = File.ReadAllBytes("E:\\malicious_phish.csv");
PaperUploadRequest request = new PaperUploadRequest();
request.Paper = new PaperModel();
request.Paper.Day = "Day 1";
request.Paper.Session = "Session 1";
request.Paper.PaperBytes = Google.Protobuf.ByteString.CopyFrom(file);
request.Paper.FileExtention = "jpg";
request.Paper.StartTime = "08:30";
request.Paper.CombinationId = 1;
request.Paper.Decryptionkey ="Eex";
request.Paper.PaperTimeMinutes = 90;

request.Request = new GeneralRequest();
request.Request.Ip = "192.168.88.5";
request.Request.Mac = " asdf";
request.Request.HddSerial = "Asdfadf";
request.Request.Timestamp = "ASdf";

var call = fileService.UploadPaper();
await call.RequestStream.WriteAsync(request);
await call.RequestStream.CompleteAsync();
PaperUploadResponse response = await call;
Console.WriteLine(response.Response.Message);


Console.WriteLine("Press anykey to continue");
Console.ReadKey();
}

Server Code服务器代码

public override async Task<PaperUploadResponse> UploadPaper(IAsyncStreamReader<PaperUploadRequest> requestStream, ServerCallContext context)
        {
            GeneralResponse response = new GeneralResponse();
            Console.WriteLine("Paper upload request received");
            if (await requestStream.MoveNext())
            {
                PaperUploadRequest request = new PaperUploadRequest();
                request = requestStream.Current;
                GeneralRequest generalRequest = request.Request;

                if(generalRequest != null)
                {
                    Paper paper = new Paper();
                    paper.Day =  request.Paper.Day;
                    paper.Session = request.Paper.Session;
                    paper.PaperStartTime = request.Paper.StartTime;
                    paper.PaperTimeMinutes = request.Paper.PaperTimeMinutes;
                    paper.DecryptionKey = request.Paper.Decryptionkey;
                    paper.FileExtention = request.Paper.FileExtention;
                    paper.PaperBytes = request.Paper.PaperBytes.ToByteArray();
                    try
                    {
                        myDbContext.Add(paper);
                        myDbContext.SaveChanges();
                    }
                    catch(Exception e)
                    {
                        response.Code = 500;
                        response.Status = "Failed";
                        response.Message= "Database Exception Occured";
                        response.Details = e.Message;
                        return new PaperUploadResponse { 
                            Response = response 
                        };
                    }
                }
            }

           
            response.Code = 200;
            response.Status = "Success";
            response.Message= "Paper uploaded successfully";
            Console.WriteLine("Done");
            return new PaperUploadResponse
            {
                Response = response
            };
        }

Proto Files原型文件

Paper.proto纸质原型

syntax = "proto3";
import "Protos/general.proto";  

// The greeting service definition.
service IPaperService {
    rpc UploadPaper(stream PaperUploadRequest) returns (PaperUploadResponse);
    rpc FindAll(GeneralRequest) returns (stream PaperDownloadResponse);
    rpc Delete(DeleteRequest) returns (GeneralResponse);
}

message PaperDownloadRequest{
    GeneralRequest request = 1;
    string institute_id = 2;
}

message PaperDownloadResponse{
    repeated PaperModel paper = 1;
    GeneralResponse response = 2;
}

message PaperUploadRequest{
    PaperModel paper = 1;
    GeneralRequest request = 2;
}
message PaperUploadResponse{
    GeneralResponse response = 1;
}

message DeleteRequest{
    int32 id = 1;
    GeneralRequest request = 2;
}

message PaperModel{
    int32 id = 1;
    int32 PaperTimeMinutes = 2;
    string Decryptionkey = 3;
    string Day = 4;
    string Session = 5;
    int32 CombinationId = 6;
    string StartTime = 7;
    string FileExtention = 8;
    bytes PaperBytes = 9;
}

General.proto通用.proto

syntax = "proto3";

message GeneralResponse{
    int32 code= 1;
    string status = 2;
    string message = 3;
    string details = 4;
}

message GeneralRequest{
    string ip = 1;
    string mac = 2;
    string hddSerial = 3;
    string timestamp = 4;
    string appId =  5;
}

Both General and Paper.proto files are in the same folder named Proto General 和 Paper.proto 文件都在同一个名为 Proto 的文件夹中

I didn't find a proper example of gRPC sending a file so I wrote the code and shared with the community.我没有找到 gRPC 发送文件的合适示例,所以我编写了代码并与社区共享。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM