小编Mar*_*ann的帖子

连接到不安全的 gRPC 通道时“响应提前结束”

我正在尝试与不安全的 gRPC 服务器建立连接。我使用 gRPC 在 Docker 容器内的两个进程之间进行通信,这就是为什么我不需要任何加密或强身份验证。

服务器按预期运行,我可以像这样使用 grpcurl 进行调用:

grpcurl -plaintext localhost:42652 SomeService.DoSomething
Run Code Online (Sandbox Code Playgroud)

现在我正在尝试从 .Net Core 应用程序调用相同的 RPC 方法:

// Registration of the DI service
services.AddGrpcClient<DaemonService.DaemonServiceClient>(options => {
    // Enable support for unencrypted HTTP2
    AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);

    options.Address = new Uri("http://localhost:42652");

    // Just a test, doesn't change anything.
    options.ChannelOptionsActions.Add(channelOptions => channelOptions.Credentials = ChannelCredentials.Insecure);
});

// Call
var reply = _someServiceClient.DoSomething(new Request());
Run Code Online (Sandbox Code Playgroud)

但是最后一行中的调用导致异常:

fail: Grpc.Net.Client.Internal.GrpcCall[6]
      Error starting gRPC call.
System.Net.Http.HttpRequestException: An error occurred while sending the request.
 ---> System.IO.IOException: The response …
Run Code Online (Sandbox Code Playgroud)

.net c# .net-core grpc

10
推荐指数
1
解决办法
1万
查看次数

如何解决服务层内部的循环依赖

我知道其他人已经遇到了同样的问题,但我找不到任何令人满意的解决方案,所以我在这里询问其他想法。

我的业务逻辑包含在这样的服务层中:

public class RoomService : IRoomService
{
    private readonly IRoomRepository _roomRepository;
    private readonly ICourseService _courseService;

    public RoomService(IRoomRepository roomRepository, ICourseService courseService)
    {
        _roomRepository = roomRepository ?? throw new ArgumentNullException(nameof(roomRepository));
        _courseService = courseService ?? throw new ArgumentNullException(nameof(courseService));
    }

    public Task DeleteRoomAsync(string id)
    {
        // Check if there are any courses for this room (requires ICourseService)
        // Delete room
    }
}

public class CourseService : ICourseService
{
    private readonly ICourseRepository _courseRepository;
    private readonly IRoomService _roomService;

    public CourseService(ICourseRepository courseRepository, IRoomService roomService)
    {
        _courseRepository …
Run Code Online (Sandbox Code Playgroud)

c# asp.net dependency-injection asp.net-core

7
推荐指数
1
解决办法
8810
查看次数