执行服务方法时出错 'getStats'
Error when executing service method 'getStats'
我正在尝试进行客户端-服务器流式传输,当我尝试从客户端流式传输到服务器时,此错误记录在服务器控制台中:执行服务方法时出错 'getStats'。
System.InvalidOperationException:尝试激活 't_stat_grpc.Services.HotelStatService' 时无法解析类型 'System.Object' 的服务。
getStats 方法
public class HotelStatService : hotelStat.hotelStatBase
{
private readonly ILogger<HotelStatService> _logger;
public HotelStatService(ILogger<HotelStatService> logger, object httpClientFactory)
{
_logger = logger;
}
public override async Task<dataToServer> getStats(IAsyncStreamReader<dataToServer> requestStream, ServerCallContext context)
{
var response = null as dataToServer;
var config = new MapperConfiguration(cfg => {
cfg.CreateMap<dataToServer, dataToServer>();
});
IMapper mapper = config.CreateMapper();
await foreach(var request in requestStream.ReadAllAsync())
{
response = mapper.Map<dataToServer>(request);
}
return response;
}
}
这就是我所说的
while (true)
{
Console.WriteLine("Clique enter para enviar os dados para o servidor");
Console.ReadLine();
Console.WriteLine("Conectando ao servidor...");
var channel = GrpcChannel.ForAddress("https://localhost:5002");
var client = new hotelStat.hotelStatClient(channel);
var stream = client.getStats();
Console.WriteLine("Conectado");
Console.WriteLine("A enviar dados para o servidor...");
string connectionString = $"Data Source=(localhost);Initial Catalog=hotel;Integrated Security=True";
SqlConnection connection = new SqlConnection(connectionString);
connection.Open();
var listaStats = connection.Query<dataToServer>("SELECT top(5) * FROM t_stat").ToList();
foreach (var item in listaStats)
{
try
{
var data = mapper.Map<dataToServer>(item);
await stream.RequestStream.WriteAsync(data);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
Console.WriteLine("Dados enviados com sucesso!");
Console.WriteLine("Disconectando...");
connection.Close();
}
(如果你需要检查任何代码,只要问我就可以给你看)
在您的 Program.cs
中,添加:
// Add services to the container.
builder.Services.AddHttpClient();
在您的 HotelStatService
class 中,将构造函数更改为:
private readonly ILogger<HotelStatService> _logger;
private readonly IHttpClientFactory _httpClientFactory;
public HotelStatService(ILogger<HotelStatService> logger, IHttpClientFactory httpClientFactory)
{
_logger = logger;
_httpClientFactory = httpClientFactory;
}
或者,如果您不使用 IHttpClientFactory 参数,只需完全删除。
我正在尝试进行客户端-服务器流式传输,当我尝试从客户端流式传输到服务器时,此错误记录在服务器控制台中:执行服务方法时出错 'getStats'。 System.InvalidOperationException:尝试激活 't_stat_grpc.Services.HotelStatService' 时无法解析类型 'System.Object' 的服务。
getStats 方法
public class HotelStatService : hotelStat.hotelStatBase
{
private readonly ILogger<HotelStatService> _logger;
public HotelStatService(ILogger<HotelStatService> logger, object httpClientFactory)
{
_logger = logger;
}
public override async Task<dataToServer> getStats(IAsyncStreamReader<dataToServer> requestStream, ServerCallContext context)
{
var response = null as dataToServer;
var config = new MapperConfiguration(cfg => {
cfg.CreateMap<dataToServer, dataToServer>();
});
IMapper mapper = config.CreateMapper();
await foreach(var request in requestStream.ReadAllAsync())
{
response = mapper.Map<dataToServer>(request);
}
return response;
}
}
这就是我所说的
while (true)
{
Console.WriteLine("Clique enter para enviar os dados para o servidor");
Console.ReadLine();
Console.WriteLine("Conectando ao servidor...");
var channel = GrpcChannel.ForAddress("https://localhost:5002");
var client = new hotelStat.hotelStatClient(channel);
var stream = client.getStats();
Console.WriteLine("Conectado");
Console.WriteLine("A enviar dados para o servidor...");
string connectionString = $"Data Source=(localhost);Initial Catalog=hotel;Integrated Security=True";
SqlConnection connection = new SqlConnection(connectionString);
connection.Open();
var listaStats = connection.Query<dataToServer>("SELECT top(5) * FROM t_stat").ToList();
foreach (var item in listaStats)
{
try
{
var data = mapper.Map<dataToServer>(item);
await stream.RequestStream.WriteAsync(data);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
Console.WriteLine("Dados enviados com sucesso!");
Console.WriteLine("Disconectando...");
connection.Close();
}
(如果你需要检查任何代码,只要问我就可以给你看)
在您的 Program.cs
中,添加:
// Add services to the container.
builder.Services.AddHttpClient();
在您的 HotelStatService
class 中,将构造函数更改为:
private readonly ILogger<HotelStatService> _logger;
private readonly IHttpClientFactory _httpClientFactory;
public HotelStatService(ILogger<HotelStatService> logger, IHttpClientFactory httpClientFactory)
{
_logger = logger;
_httpClientFactory = httpClientFactory;
}
或者,如果您不使用 IHttpClientFactory 参数,只需完全删除。