<PackageReference Include="System.ClientModel" Version="1.1.0-beta.7" />

RequestOptions

public class RequestOptions
Options that can be used to control the behavior of a request sent by a client.
using System.ClientModel.Internal; using System.Collections.Generic; using System.Runtime.CompilerServices; using System.Threading; namespace System.ClientModel.Primitives { [System.Runtime.CompilerServices.NullableContext(1)] [System.Runtime.CompilerServices.Nullable(0)] public class RequestOptions { [System.Runtime.CompilerServices.Nullable(0)] private readonly struct HeadersUpdate { public HeaderOperation Operation { get; } public string HeaderName { get; } public string HeaderValue { get; } public HeadersUpdate(HeaderOperation operation, string name, string value) { Operation = operation; HeaderName = name; HeaderValue = value; } } [System.Runtime.CompilerServices.NullableContext(0)] private enum HeaderOperation { Add, Set } private bool _frozen; private CancellationToken _cancellationToken = CancellationToken.None; private ClientErrorBehaviors _errorOptions; private bool _bufferResponse = true; [System.Runtime.CompilerServices.Nullable(new byte[] { 2, 1 })] private PipelinePolicy[] _perCallPolicies; [System.Runtime.CompilerServices.Nullable(new byte[] { 2, 1 })] private PipelinePolicy[] _perTryPolicies; [System.Runtime.CompilerServices.Nullable(new byte[] { 2, 1 })] private PipelinePolicy[] _beforeTransportPolicies; [System.Runtime.CompilerServices.Nullable(2)] private List<HeadersUpdate> _headersUpdates; public CancellationToken CancellationToken { get { return _cancellationToken; } set { AssertNotFrozen(); _cancellationToken = value; } } public ClientErrorBehaviors ErrorOptions { get { return _errorOptions; } set { AssertNotFrozen(); _errorOptions = value; } } public bool BufferResponse { get { return _bufferResponse; } set { AssertNotFrozen(); _bufferResponse = value; } } [System.Runtime.CompilerServices.NullableContext(2)] internal static RequestOptions FromCancellationToken(CancellationToken cancellationToken) { if (cancellationToken == default(CancellationToken)) return null; return new RequestOptions { CancellationToken = cancellationToken }; } public void AddHeader(string name, string value) { Argument.AssertNotNull(name, "name"); Argument.AssertNotNull(value, "value"); AssertNotFrozen(); if (_headersUpdates == null) _headersUpdates = new List<HeadersUpdate>(); _headersUpdates.Add(new HeadersUpdate(HeaderOperation.Add, name, value)); } public void SetHeader(string name, string value) { Argument.AssertNotNull(name, "name"); Argument.AssertNotNull(value, "value"); AssertNotFrozen(); if (_headersUpdates == null) _headersUpdates = new List<HeadersUpdate>(); _headersUpdates.Add(new HeadersUpdate(HeaderOperation.Set, name, value)); } public void AddPolicy(PipelinePolicy policy, PipelinePosition position) { Argument.AssertNotNull(policy, "policy"); AssertNotFrozen(); switch (position) { case PipelinePosition.PerCall: _perCallPolicies = ClientPipelineOptions.AddPolicy(policy, _perCallPolicies); break; case PipelinePosition.PerTry: _perTryPolicies = ClientPipelineOptions.AddPolicy(policy, _perTryPolicies); break; case PipelinePosition.BeforeTransport: _beforeTransportPolicies = ClientPipelineOptions.AddPolicy(policy, _beforeTransportPolicies); break; default: throw new ArgumentException($"""{position}"""); } } protected internal virtual void Apply(PipelineMessage message) { Argument.AssertNotNull(message, "message"); Freeze(); message.CancellationToken = CancellationToken; message.PerCallPolicies = _perCallPolicies; message.PerTryPolicies = _perTryPolicies; message.BeforeTransportPolicies = _beforeTransportPolicies; message.BufferResponse = BufferResponse; if (_headersUpdates != null) { foreach (HeadersUpdate headersUpdate in _headersUpdates) { switch (headersUpdate.Operation) { case HeaderOperation.Add: message.Request.Headers.Add(headersUpdate.HeaderName, headersUpdate.HeaderValue); break; case HeaderOperation.Set: message.Request.Headers.Set(headersUpdate.HeaderName, headersUpdate.HeaderValue); break; default: throw new InvalidOperationException("Unrecognized header update operation value."); } } } } public virtual void Freeze() { _frozen = true; } protected void AssertNotFrozen() { if (_frozen) throw new InvalidOperationException("Cannot change a RequestOptions instance after it has been passed to a client method."); } } }