当前位置: 首页 > 知识库问答 >
问题:

grpc服务器显示“未实现的服务错误”

包丁雨
2023-03-14

我按照这个指令创建了一个grpc服务器和客户端:https://docs . Microsoft . com/en-us/aspnet/core/tutorials/grpc/grpc-start?view=aspnetcore-3.0

当我尝试从客户端调用服务时,客户端会显示以下错误消息:“发生了一个或多个错误。(状态(StatusCode=Unknown,Detail=No Status received)”

这个是服务器:

info: Microsoft.AspNetCore.Hosting.Diagnostics[1]
      Request starting HTTP/2 POST http://STEINI-PC/LocationService/GetLocations application/grpc
info: Microsoft.AspNetCore.Routing.EndpointMiddleware[0]
      Executing endpoint 'gRPC - gRPC - Unimplemented service'
info: Grpc.AspNetCore.Server.Internal.ServerCallHandlerFactory[1]
      Service 'LocationService' is unimplemented.
info: Microsoft.AspNetCore.Routing.EndpointMiddleware[1]
      Executed endpoint 'gRPC - gRPC - Unimplemented service'
info: Microsoft.AspNetCore.Hosting.Diagnostics[2]
      Request finished in 51.812000000000005ms 200 application/grpc

原型文件:

syntax = "proto3";

service EventService {
    rpc GetEvents (Empty) returns (Events) {}
    rpc GetEvent (Id) returns (Event) {}
    rpc GetEventsByLocation (Id) returns (Events) {}
    rpc AddEvent (Event) returns (Empty) {}
    rpc UpdateEvent (Event) returns (Empty) {}
    rpc DeleteEvent (Id) returns (Event) {}
}

service LocationService {
    rpc GetLocations (Empty) returns (Locations) {}
    rpc GetLocation (Id) returns (Location) {}
    rpc AddLocation (Location) returns (Empty) {}
    rpc UpdateLocation (Location) returns (Empty) {}
    rpc DeleteLocation (Id) returns (Location) {}
}

service ParticipantService {
    rpc GetParticipants (Empty) returns (Participants) {}
    rpc GetParticipant (Id) returns (Participant) {}
    rpc GetParticipantsFromEvent (Id) returns (Participants) {}
    rpc AddParticipant (Participant) returns (Empty) {}
    rpc UpdateParticipant (Participant) returns (Empty) {}
    rpc DeleteParticipant (Id) returns (Participant) {}
}

message Empty {

}

message Id {
    string id = 1;
}

message Events{
    repeated Event events = 1;
}

message Locations{
    repeated Location locations = 1;
}

message Participants{
    repeated Participant participants = 1;
}

message Event {
    Id eventid = 1;
    string name = 2;
    string description = 3;
    Id locationID = 4;
    string date = 5;
}

message Location {
    Id locationID = 1;
    string name = 2;
    string adress = 3;
}

message Participant {
    Id participantId = 1;
    string name = 2;
    Id eventId = 3;
}

服务器启动:

public class Startup
    {
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            var connectionString = @"Data Source=STEINI-PC;Initial Catalog=thesisSteinmetz;Persist Security Info=True;User ID=SA;Password=SA123";

            services.AddGrpc(options =>
            {
                options.EnableDetailedErrors = true;
            });
            services.AddDbContext<DataContext>(options => options.UseSqlServer(connectionString));
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            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<EventService>();
                endpoints.MapGrpcService<LocationService>();
                endpoints.MapGrpcService<ParticipantService>();
            });
        }
    }

位置服务:

public class LocationService : LocationServiceBase
    {
        private readonly DataContext _context;
        private readonly ILocationDataHandler _locationDataHandler;


        //public LocationService()
        //{
        //    var connectionString = @"Data Source=STEINI-PC;Initial Catalog=thesisSteinmetz;Persist Security Info=True;User ID=SA;Password=SA123";
        //    var contextOptions = new DbContextOptionsBuilder<DataContext>();
        //    contextOptions.UseSqlServer(connectionString);

        //    _context = new DataContext(contextOptions.Options);
        //    _locationDataHandler = new EFCoreLocationDataHandler(_context);
        //}

        public LocationService(DataContext context)
        {
            _context = context;
            _locationDataHandler = new EFCoreLocationDataHandler(_context);
        }


        public override async Task<Empty> AddLocation(Location request, ServerCallContext context)
        {
            await _locationDataHandler.AddAsync(LocationConverter.LocationFromGRPC(request));

            return new Empty();
        }

        public override async Task<Location> DeleteLocation(Id request, ServerCallContext context)
        {
            try
            {
                CommonLibrary.Models.Location location = await GetLocation(request);

                await _locationDataHandler.DeleteAsync(location.LocationId);

                return LocationConverter.LocationToGRPC(location);
            }
            catch (Exception ex)
            {
                throw new RpcException(new Status(StatusCode.NotFound, ex.Message));
            }
        }

        public override async Task<Location> GetLocation(Id request, ServerCallContext context)
        {
            try
            {
                return LocationConverter.LocationToGRPC(await GetLocation(request));
            }
            catch (Exception ex)
            {
                throw new RpcException(new Status(StatusCode.NotFound, ex.Message));
            }
        }

        public override async Task<Locations> GetLocations(Empty request, ServerCallContext context)
        {
            return LocationConverter.LocationsToGrpc(await _locationDataHandler.GetAsync());
        }

        public override async Task<Empty> UpdateLocation(Location request, ServerCallContext context)
        {
            try
            {
                await _locationDataHandler.UpdateAsync(LocationConverter.LocationFromGRPC(request));
            }
            catch (Exception ex)
            {

                throw new RpcException(new Status(StatusCode.NotFound, ex.Message));
            }
            return new Empty();
        }


        private async Task<CommonLibrary.Models.Location> GetLocation(Id request)
        {
            var location = await _locationDataHandler.GetAsync(IdConverter.IdToGuid(request));

            if (location == null)
            {
                throw new Exception($"Location with id: {location.LocationId.ToString()} Not Found");
            }

            return location;
        }
    }

共有3个答案

云炜
2023-03-14

如果您在原型中使用“包”,请确保服务器端与客户端匹配。

缪晋
2023-03-14

我已经找到了问题。

我的问题是生成的文件的不同命名空间,我手动编辑了这些文件。

司徒运锋
2023-03-14

进入programs.cs并添加您的服务。


// Configure the HTTP request pipeline.
app.MapGrpcService<GreeterService>(); // here's a service
app.MapGrpcService<UserAuthService>(); // another service i created
app.MapGrpcService<BusinessService>(); // also another service created

app.MapGet("/", () => "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");

app.Run();

 类似资料:
  • 我正在尝试用NestJS和gRPC构建微服务。它有两个服务,第一个是gRPC服务,第二个是调用gRPC服务的REST服务。 首先,它可以很好地处理一元调用,即findRepoById。但它不适用于服务器流式调用findAllRepos。当我试图调用findAllRepos时,它抛出如下错误 UnhandledPromiseRejection警告:错误:12 UNIMPLEMENTED:服务器没有实

  • gRPC 官方文档中的 quickstart - php, 很容易给 PHPer 产生误导, 按照官网的文档, 运行起来 gRPC 服务就很麻烦, 更不用说整套的 RPC 服务了. 推荐阅读 tech| 再探 grpc, 讲解了在 PHP 中实现 gRPC 相关基础知识. hyperf 对 gRPC 支持做了更进一步的封装, hyperf-skeleton 项目为例, 详细讲解整个步骤: .pro

  • 在之前的几篇教程中,我们讲的是如何查询和Mutation操作,这些都是在客户端那边所进行的,那么服务器这边是如何处理这些请求的呢?这就是这篇教程所要说的东西了. 准备工作 克隆库: git clone https://github.com/zhouyuexie/learn-graphql 安装依赖: cd learn-graphql && npm install cd learn-graphql

  • 我刚刚开始通过Spring Cloud了解微服务,首先我尝试从本文中重现基本示例https://spring.io/blog/2015/07/14/microservices-with-spring.这是我的代码: 尤里卡服务器 资源/registration-server.yml: 示例服务: 帐户-服务. yml: 你能打电话给我,请问我做错了什么吗? 编辑

  • 我正在用C++开发双向流gRPC。我想在服务器端设置一个超时限制,如果连接超过一个阈值,就关闭连接。 但是我发现的唯一超时机制是在客户端(https://grpc.io/blog/deadlines/#c)。我找不到任何API可以用于ServerContext(https://grpc.github.io/grpc/cpp/classgrpc_1_1_server_context.html)。有人

  • 背景 当前有两个服务,分别是user-service和order-service,nacos服务列表中无法发现两个服务 排查 Nacos v2.2.3 依赖已引入,配置文件已配置addr 运行时未出现连接nacos的日志: 希望大佬们可以帮忙看看是什么问题 问题程序链接 https://oss-20001.oss-cn-qingdao.aliyuncs.com/cloud-demo.zip