C# gRPC接收文件服务

1、先用proto协议生成代理文件

syntax = "proto3";
 
package BimService;

service  BimService {
  rpc Upload(FileRequest) returns (FileReply) {}
}

message FileRequest {
  bytes file = 1;
  string name = 2;
  string type = 3;
}

message FileReply {
  string msg = 1;
}

 

2、服务端代码

            int port = 55551;

            //设置可接收的文件大小
            var options = new List<ChannelOption> {
                new ChannelOption(ChannelOptions.MaxReceiveMessageLength,int.MaxValue),
                new ChannelOption(ChannelOptions.MaxSendMessageLength,int.MaxValue)
            };
            Server server = new Server(options)
            {
                Services = { BimService.BindService(new GrpcImpl()) },
                Ports = { new ServerPort("localhost", port, ServerCredentials.Insecure) }
            };
            server.Start();

            Console.WriteLine("gRPC server listening on port " + port);

 

3、运行代码

    public class GrpcImpl : BimService.BimServiceBase
    {
        /// <summary>
        /// 接收BIM文件
        /// </summary>
        /// <param name="request"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public override Task<FileReply> Upload(FileRequest request, ServerCallContext context)
        {
            var bs = request.File; //文件流
            var fileName = request.Name; //文件名称
            var type = request.Type; //文件类型    
        }
    }

 

posted @ 2020-09-11 11:11  ellennet123  阅读(598)  评论(0)    收藏  举报