锐利:
// configure application:
app.UseRouting();
app.UseEndpoints(endpoints =>
{
// Communication with gRPC endpoints must be made through a gRPC client.
// To learn how to create a client, visit: https://go.microsoft.com/fwlink/?linkid=2086909
endpoints.MapGrpcService<SSOAdapterGRPCImplementation>();
});
// configure services:
services.AddGrpc();
执行代码:
func main() {
ctx := context.Background()
addr := "localhost:5001"
log.Print("trying to connect to grpc")
var opts []grpc.DialOption
opts = append(opts, grpc.WithDefaultCallOptions())
certFilePath := `c:\Keys\local-v2\localhost.cer`
cred, err := credentials.NewClientTLSFromFile(certFilePath, "")
if err != nil {
log.Fatal(err)
}
opts = append(opts, grpc.WithTransportCredentials(cred))
conn, err := grpc.Dial(addr, opts...)
if err != nil {
log.Fatal(err)
}
c := ssoadapter.NewSSOAdapterGRPCClient(conn)
resp, err :=c.GetConfiguration(ctx, &ssoadapter.GetConfigurationRequest{
AdapterId: 1,
})
if err != nil{
log.Fatal(err)
}
log.Printf("GetConfiguration [%v]",resp)
resp2, err := c.SetConfiguration(ctx, &ssoadapter.SetConfigurationRequest{
PartnerId: 100,
AdapterId: 1,
Configuration: map[string]string{
"k1": "v1",
"k2": "v2",
},
Signature: "123",
})
if err != nil {
log.Fatal(err)
}
log.Printf("SetConfiguration: %v", resp2)
}
调用此代码时,我从客户端代码中得到错误:
2020/07/28 10:05:29 trying to connect to grpc
2020/07/28 10:05:29 rpc error: code = Internal desc = server closed the stream without sending trailers
Process finished with exit code 1
在服务器端获取:
INFO 2020-07-28 10:04:03,867 class:Microsoft.AspNetCore.Hosting.Diagnostics topic:null server:IL-ARTHURVA-VIM ip:::1 reqid:0HM1IOCJG13HA:00000005 partner:null action:null uid:null msg:Request finished in 7.4006ms 404
INFO 2020-07-28 10:05:29,273 class:Microsoft.AspNetCore.Hosting.Diagnostics topic:null server:IL-ARTHURVA-VIM ip:null reqid:null partner:null action:null uid:null msg:Request starting HTTP/2 POST https://localhost:5001/SSOAdapterGRPC/GetConfiguration application/grpc
DEBUG 2020-07-28 10:05:29,277 class:Core.Middleware.RequestLoggingMiddleware topic:null server:IL-ARTHURVA-VIM ip:::1 reqid:0HM1IOCJG13HB:00000001 partner:null action:null uid:null msg:Url: https://localhost:5001/SSOAdapterGRPC/GetConfiguration
Headers: Content-Type:application/grpc,Host:localhost:5001,TE:trailers,User-Agent:grpc-go/1.30.0,:method:POST,:scheme:https,:path:/SSOAdapterGRPC/GetConfiguration,:authority:localhost:5001
Body:
INFO 2020-07-28 10:05:29,278 class:Microsoft.AspNetCore.Routing.EndpointMiddleware topic:null server:IL-ARTHURVA-VIM ip:::1 reqid:0HM1IOCJG13HB:00000001 partner:null action:null uid:null msg:Executing endpoint 'gRPC - /SSOAdapterGRPC/GetConfiguration'
ERROR 2020-07-28 10:05:29,280 class:Grpc.AspNetCore.Server.ServerCallHandler topic:null server:IL-ARTHURVA-VIM ip:::1 reqid:0HM1IOCJG13HB:00000001 partner:null action:null uid:null msg:Error reading message.
Grpc.Core.RpcException: Status(StatusCode=Internal, Detail="Incomplete message.")
at Grpc.AspNetCore.Server.Internal.PipeExtensions.ReadSingleMessageAsync[T](PipeReader input, HttpContextServerCallContext serverCallContext, Func`2 deserializer)
INFO 2020-07-28 10:05:29,282 class:Grpc.AspNetCore.Server.ServerCallHandler topic:null server:IL-ARTHURVA-VIM ip:::1 reqid:0HM1IOCJG13HB:00000001 partner:null action:null uid:null msg:Error status code 'Internal' raised.
Grpc.Core.RpcException: Status(StatusCode=Internal, Detail="Incomplete message.")
at Grpc.AspNetCore.Server.Internal.PipeExtensions.ReadSingleMessageAsync[T](PipeReader input, HttpContextServerCallContext serverCallContext, Func`2 deserializer)
at Grpc.AspNetCore.Server.Internal.CallHandlers.UnaryServerCallHandler`3.HandleCallAsyncCore(HttpContext httpContext, HttpContextServerCallContext serverCallContext)
at Grpc.AspNetCore.Server.Internal.CallHandlers.ServerCallHandlerBase`3.<HandleCallAsync>g__AwaitHandleCall|8_0(HttpContextServerCallContext serverCallContext, Method`2 method, Task handleCall)
INFO 2020-07-28 10:05:29,286 class:Microsoft.AspNetCore.Routing.EndpointMiddleware topic:null server:IL-ARTHURVA-VIM ip:::1 reqid:0HM1IOCJG13HB:00000001 partner:null action:null uid:null msg:Executed endpoint 'gRPC - /SSOAdapterGRPC/GetConfiguration'
INFO 2020-07-28 10:05:29,288 class:Microsoft.AspNetCore.Hosting.Diagnostics topic:null server:IL-ARTHURVA-VIM ip:::1 reqid:0HM1IOCJG13HB:00000001 partner:null action:null uid:null msg:Request finished in 14.4426ms 200 application/grpc