Код: Выделить всё
using System.Diagnostics;
var builder = WebApplication.CreateBuilder();
builder.Services.AddServiceModelServices();
builder.Services.AddServiceModelMetadata();
builder.Services.AddSingleton();
builder.WebHost.UseKestrel(options =>
{
options.AllowSynchronousIO = true;
options.ListenLocalhost(7151, listenOptions =>
{
listenOptions.UseHttps();
if (Debugger.IsAttached)
{
listenOptions.UseConnectionLogging();
}
});
});
var app = builder.Build();
app.UseServiceModel(serviceBuilder =>
{
serviceBuilder.AddService();
serviceBuilder.AddServiceEndpoint(new BasicHttpBinding(BasicHttpSecurityMode.Transport){TransferMode = TransferMode.Streamed}, $"https://localhost:7151/StreamingService.svc");
var serviceMetadataBehavior = app.Services.GetRequiredService();
serviceMetadataBehavior.HttpsGetEnabled = true;
});
app.Run();
Код: Выделить всё
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>
namespace RandomNumberCore;
[ServiceContract]
public interface IStreamingService
{
[OperationContract]
Stream GetRandomStream();
}
public class StreamingService : IStreamingService
{
public Stream GetRandomStream()
{
return new RandomStream(Random.Shared);
}
}
< /code>
и простой клиент: < /p>
using System.ServiceModel;
namespace RandomNumberConsumerNet8
{
[ServiceContract]
public interface IStreamingService
{
[OperationContract]
Stream GetRandomStream();
}
public interface IStreamingServiceChannel : IStreamingService, IClientChannel;
internal class Program
{
public static async Task Main(string[] args)
{
var cts = new CancellationTokenSource();
using var channelFactory = new ChannelFactory(new BasicHttpBinding(BasicHttpSecurityMode.Transport){TransferMode = TransferMode.Streamed, MaxReceivedMessageSize = 1_000_000_000 }, new EndpointAddress("https://localhost:7151/StreamingService.svc"));
using var service = channelFactory.CreateChannel();
service.Open();
using var randomStream = service.GetRandomStream();
byte[] buffer = new byte[4];
await randomStream.ReadExactlyAsync(buffer, cts.Token);
service.Close();
channelFactory.Close();
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/796 ... -streaming