gpt4 book ai didi

c# - grpc 服务器显示 "unimplemented service error"

转载 作者:行者123 更新时间:2023-12-04 11:36:48 31 4
gpt4 key购买 nike

我在这条指令之后创建了一个 grpc 服务器和客户端:https://docs.microsoft.com/en-us/aspnet/core/tutorials/grpc/grpc-start?view=aspnetcore-3.0&tabs=visual-studio .

当我尝试从客户端调用服务时,客户端显示此错误消息:“发生了一个或多个错误。(Status(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

原型(prototype)文件:

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

最佳答案

我找到了问题所在。

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

关于c# - grpc 服务器显示 "unimplemented service error",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56794993/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com