-
|
I'm trying to write a helper class for clients which encapsulates the code for making a JsonRPC connection over WebSocket to a service with a specific interface. When using a service interface that contains an
Is this expected or am I doing something wrong? Client helper: class RpcServiceClient<TService> : IAsyncDisposable
where TService : class
{
private readonly ClientWebSocket _socket;
private readonly JsonRpc _jsonRpc;
private readonly TService _proxy;
public static async Task<RpcServiceClient<TService>> ConnectAsync(string url, CancellationToken token)
{
var socket = new ClientWebSocket();
await socket.ConnectAsync(new Uri(url), token).ConfigureAwait(false);
try
{
var jsonRpc = new JsonRpc(new WebSocketMessageHandler(socket));
jsonRpc.StartListening();
//jsonRpc.AllowModificationWhileListening = true;
var proxy = jsonRpc.Attach<TService>();
//jsonRpc.AllowModificationWhileListening = false;
return new RpcServiceClient<TService>(socket, jsonRpc, proxy);
}
catch
{
await socket.CloseAsync(WebSocketCloseStatus.ProtocolError, "JSON-RPC setup failed", CancellationToken.None).ConfigureAwait(false);
throw;
}
}
private RpcServiceClient(ClientWebSocket socket, JsonRpc jsonRpc, TService proxy)
{
_socket = socket;
_jsonRpc = jsonRpc;
_proxy = proxy;
}
public TService Service => _proxy;
public async ValueTask DisposeAsync()
{
await _socket.CloseAsync(WebSocketCloseStatus.NormalClosure, "Client closing", CancellationToken.None).ConfigureAwait(false);
_jsonRpc.Dispose();
}
}Example problematic interface: public interface IBarService : IDisposable
{
Task SumAsync(IEnumerable<int> values);
event EventHandler<int> SumResult;
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
|
If I move the |
Beta Was this translation helpful? Give feedback.
The original order works if the interface has no events. The reason event handlers can't be added after listening was started is you may have already missed events and no warning would be given, which might be very hard to diagnose. If you point me at the doc you were reading, I can look to see if we can clarify this point.