Код: Выделить всё
namespace RandomNumberCore;
[ServiceContract]
public interface IStreamingService
{
[OperationContract]
Stream GetRandomStream();
}
public class StreamingService : IStreamingService
{
public Stream GetRandomStream()
{
var stream = new RandomStream(Random.Shared);
return stream;
}
}
Код: Выделить всё
namespace RandomNumberCore;
public class RandomStream : Stream
{
public RandomStream(Random random)
{
this._random = random;
}
private int _sequence;
private readonly Random _random;
public override bool CanRead => true;
public override bool CanSeek => false;
public override bool CanWrite => false;
public override long Length => throw new NotSupportedException();
// ReSharper disable once ValueParameterNotUsed
public override long Position { get => _sequence; set => throw new NotSupportedException(); }
public override void Flush()
{}
public override int Read(byte[] buffer, int offset, int count)
{
var internalBuffer = new Span(buffer, offset, count);
_random.NextBytes(internalBuffer);
_sequence+=count;
return count;
}
public override int Read(Span buffer)
{
_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();
}
}
< /code>
и простой клиент: < /p>
async Task Main()
{
var cts = new CancellationTokenSource();
Util.Cleanup += (_,_) => cts.Cancel();
var endpointBinding = new BasicHttpBinding(BasicHttpSecurityMode.Transport){TransferMode = TransferMode.Streamed, MaxReceivedMessageSize = 1_000_000_000 };
var address = "https://localhost:7151/StreamingService.svc";
using var channelFactory = new ChannelFactory(endpointBinding, new EndpointAddress(address));
using var service = channelFactory.CreateChannel();
service.Open();
using var randomStream = service.GetRandomStream();
byte[] buffer = new byte[4];
await randomStream.ReadExactlyAsync(buffer, cts.Token);
buffer.Dump();
service.Close();
}
Полный код https://github.com/richardcocks/wcftest ... sueonclose
Подробнее здесь: https://stackoverflow.com/questions/796 ... -streaming