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

Steini

按照以下说明创建了grpc服务器和客户端:https ://docs.microsoft.com/zh-cn/aspnet/core/tutorials/grpc/grpc-start?view=aspnetcore-3.0&tabs=visual-studio

当我尝试从客户端调用服务时,客户端显示以下错误消息:“发生一个或多个错误。(状态(StatusCode =未知,Detail =“没有收到状态”)))

和服务器这一个:

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;
        }
    }
Steini

我发现了问题。

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章