сервер grpc показывает "неосуществленную ошибку сервиса" - PullRequest
0 голосов
/ 27 июня 2019

Я создал сервер и клиент grpc после этой инструкции: https://docs.microsoft.com/en-us/aspnet/core/tutorials/grpc/grpc-start?view=aspnetcore-3.0&tabs=visual-studio.

Когда я пытаюсь вызвать службу из клиента, клиент показывает это сообщение об ошибке: «Произошла одна или несколько ошибок. (Состояние(StatusCode = Unknown, 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;
        }
    }

1 Ответ

0 голосов
/ 28 июня 2019

Я нашел проблему.

Моя проблема была в разных пространствах имен сгенерированных файлов, которые я редактировал вручную.

...