There are some events you can handle:
https://learn.microsoft.com/en-us/dotnet/api/system.servicem...
https://learn.microsoft.com/en-us/dotnet/api/system.servicem...
Could you use these to cancel the stream?
replies(1):
https://learn.microsoft.com/en-us/dotnet/api/system.servicem...
https://learn.microsoft.com/en-us/dotnet/api/system.servicem...
Could you use these to cancel the stream?
public class StreamingService : IStreamingService { private readonly ILogger<StreamingService> _logger;
public StreamingService(ILogger<StreamingService> logger)
{
_logger = logger;
}
public Stream GetRandomStream()
{
var random = Random.Shared;
var isClientConnected = true;
var context = OperationContext.Current;
context.InstanceContext.Closed += (s, e) =>
{
_logger.LogInformation("Client closed connection.");
isClientConnected = false;
};
context.InstanceContext.Faulted += (s, e) =>
{
_logger.LogWarning("Client connection faulted.");
isClientConnected = false;
};
return new MonitorableRandomStream(random, () => isClientConnected, _logger);
}
}public class MonitorableRandomStream : Stream { private readonly Random _random; private readonly Func<bool> _isClientConnected; private readonly ILogger _logger; private long _sequence;
public MonitorableRandomStream(Random random, Func<bool> isClientConnected, ILogger logger)
{
_random = random;
_isClientConnected = isClientConnected;
_logger = logger;
}
public override bool CanRead => true;
public override bool CanSeek => false;
public override bool CanWrite => false;
public override long Length => throw new NotSupportedException();
public override long Position { get => _sequence; set => throw new NotSupportedException(); }
public override void Flush() {}
public override int Read(byte[] buffer, int offset, int count)
{
if (!_isClientConnected())
{
_logger.LogInformation("Stopping stream: client disconnected.");
return 0;
}
var span = new Span<byte>(buffer, offset, count);
_random.NextBytes(span);
_sequence += count;
return count;
}
public override int Read(Span<byte> buffer)
{
if (!_isClientConnected())
{
_logger.LogInformation("Stopping stream: client disconnected.");
return 0;
}
_random.NextBytes(buffer);
_sequence += buffer.Length;
return buffer.Length;
}
public override long Seek(long offset, SeekOrigin origin) => throw new NotSupportedException();
public override void SetLength(long value) => throw new NotSupportedException();
public override void Write(byte[] buffer, int offset, int count) => throw new NotSupportedException();
}