本文整理汇总了C#中RequestOptions.Validate方法的典型用法代码示例。如果您正苦于以下问题:C# RequestOptions.Validate方法的具体用法?C# RequestOptions.Validate怎么用?C# RequestOptions.Validate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类RequestOptions
的用法示例。
在下文中一共展示了RequestOptions.Validate方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: IssueWebRequestAsync
/// <summary>
/// Issues the web request asynchronous.
/// </summary>
/// <param name="endpoint">The endpoint.</param>
/// <param name="method">The method.</param>
/// <param name="input">The input.</param>
/// <param name="options">request options</param>
/// <returns></returns>
public async Task<Response> IssueWebRequestAsync(
string endpoint, string query, string method, Stream input, RequestOptions options)
{
options.Validate();
Stopwatch watch = Stopwatch.StartNew();
UriBuilder builder = new UriBuilder(
_credentials.ClusterUri.Scheme,
_credentials.ClusterUri.Host,
options.Port,
options.AlternativeEndpoint + endpoint);
if (query != null)
{
builder.Query = query;
}
Debug.WriteLine("Issuing request {0} to endpoint {1}", Trace.CorrelationManager.ActivityId, builder.Uri);
HttpWebRequest httpWebRequest = WebRequest.CreateHttp(builder.Uri);
httpWebRequest.ServicePoint.ReceiveBufferSize = options.ReceiveBufferSize;
httpWebRequest.ServicePoint.UseNagleAlgorithm = options.UseNagle;
httpWebRequest.Timeout = options.TimeoutMillis;
httpWebRequest.KeepAlive = options.KeepAlive;
httpWebRequest.Credentials = _credentialCache;
httpWebRequest.PreAuthenticate = true;
httpWebRequest.Method = method;
httpWebRequest.Accept = _contentType;
httpWebRequest.ContentType = _contentType;
// This allows 304 (NotModified) requests to catch
//https://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.allowautoredirect(v=vs.110).aspx
httpWebRequest.AllowAutoRedirect = false;
if (options.AdditionalHeaders != null)
{
foreach (var kv in options.AdditionalHeaders)
{
httpWebRequest.Headers.Add(kv.Key, kv.Value);
}
}
if (input != null)
{
// expecting the caller to seek to the beginning or to the location where it needs to be copied from
using (Stream req = await httpWebRequest.GetRequestStreamAsync())
{
await input.CopyToAsync(req);
}
}
var response = (await httpWebRequest.GetResponseAsync()) as HttpWebResponse;
return new Response()
{
WebResponse = response,
RequestLatency = watch.Elapsed
};
}
示例2: IssueWebRequestAsync
/// <summary>
/// Issues the web request asynchronous.
/// </summary>
/// <param name="endpoint">The endpoint.</param>
/// <param name="method">The method.</param>
/// <param name="query">The query.</param>
/// <param name="input">The input.</param>
/// <param name="options">request options</param>
/// <returns></returns>
public async Task<Response> IssueWebRequestAsync(
string endpoint, string method, string query, Stream input, RequestOptions options)
{
options.Validate();
Stopwatch watch = Stopwatch.StartNew();
UriBuilder builder = new UriBuilder(
_credentials.ClusterUri.Scheme,
_credentials.ClusterUri.Host,
options.Port,
options.AlternativeEndpoint + endpoint);
if (query != null)
{
builder.Query = query;
}
Debug.WriteLine("Issuing request {0} to endpoint {1}", Trace.CorrelationManager.ActivityId, builder.Uri);
HttpWebRequest httpWebRequest = WebRequest.CreateHttp(builder.Uri);
httpWebRequest.ServicePoint.ReceiveBufferSize = options.ReceiveBufferSize;
httpWebRequest.ServicePoint.UseNagleAlgorithm = options.UseNagle;
httpWebRequest.Timeout = options.TimeoutMillis;
httpWebRequest.KeepAlive = options.KeepAlive;
httpWebRequest.Credentials = _credentialCache;
httpWebRequest.PreAuthenticate = true;
httpWebRequest.Method = method;
httpWebRequest.Accept = _contentType;
httpWebRequest.ContentType = _contentType;
if (options.AdditionalHeaders != null)
{
foreach (var kv in options.AdditionalHeaders)
{
httpWebRequest.Headers.Add(kv.Key, kv.Value);
}
}
if (input != null)
{
// seek to the beginning, so we copy everything in this buffer
input.Seek(0, SeekOrigin.Begin);
using (Stream req = httpWebRequest.GetRequestStream())
{
await input.CopyToAsync(req);
}
}
var response = (await httpWebRequest.GetResponseAsync()) as HttpWebResponse;
return new Response()
{
WebResponse = response,
RequestLatency = watch.Elapsed
};
}
示例3: IssueWebRequestAsync
/// <summary>
/// Issues the web request asynchronous.
/// </summary>
/// <param name="endpoint">The endpoint.</param>
/// <param name="method">The method.</param>
/// <param name="input">The input.</param>
/// <param name="options">request options</param>
/// <returns></returns>
public async Task<Response> IssueWebRequestAsync(string endpoint, string query, string method, Stream input, RequestOptions options)
{
options.Validate();
Stopwatch watch = Stopwatch.StartNew();
Trace.CorrelationManager.ActivityId = Guid.NewGuid();
var balancedEndpoint = _balancer.GetEndpoint();
// Grab the host. Use the alternative host if one is specified
string host = (options.AlternativeHost != null) ? options.AlternativeHost : balancedEndpoint.Host;
UriBuilder builder = new UriBuilder(
balancedEndpoint.Scheme,
host,
options.Port,
options.AlternativeEndpoint + endpoint);
if (query != null)
{
builder.Query = query;
}
var target = builder.Uri;
try
{
Debug.WriteLine("Issuing request {0} to endpoint {1}", Trace.CorrelationManager.ActivityId, target);
HttpWebRequest httpWebRequest = WebRequest.CreateHttp(target);
httpWebRequest.ServicePoint.ReceiveBufferSize = options.ReceiveBufferSize;
httpWebRequest.ServicePoint.UseNagleAlgorithm = options.UseNagle;
httpWebRequest.Timeout = options.TimeoutMillis; // This has no influence for calls that are made Async
httpWebRequest.KeepAlive = options.KeepAlive;
httpWebRequest.Credentials = _credentialCache;
httpWebRequest.PreAuthenticate = true;
httpWebRequest.Method = method;
httpWebRequest.Accept = _contentType;
httpWebRequest.ContentType = _contentType;
// This allows 304 (NotModified) requests to catch
//https://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.allowautoredirect(v=vs.110).aspx
httpWebRequest.AllowAutoRedirect = false;
if (options.AdditionalHeaders != null)
{
foreach (var kv in options.AdditionalHeaders)
{
httpWebRequest.Headers.Add(kv.Key, kv.Value);
}
}
long remainingTime = options.TimeoutMillis;
if (input != null)
{
// expecting the caller to seek to the beginning or to the location where it needs to be copied from
Stream req = null;
try
{
req = await httpWebRequest.GetRequestStreamAsync().WithTimeout(
TimeSpan.FromMilliseconds(remainingTime),
"Waiting for RequestStream");
remainingTime = options.TimeoutMillis - watch.ElapsedMilliseconds;
if (remainingTime <= 0)
{
remainingTime = 0;
}
await input.CopyToAsync(req).WithTimeout(
TimeSpan.FromMilliseconds(remainingTime),
"Waiting for CopyToAsync",
CancellationToken.None);
}
catch (TimeoutException)
{
httpWebRequest.Abort();
throw;
}
finally
{
req?.Close();
}
}
try
{
remainingTime = options.TimeoutMillis - watch.ElapsedMilliseconds;
if (remainingTime <= 0)
{
remainingTime = 0;
}
Debug.WriteLine("Waiting for response for request {0} to endpoint {1}", Trace.CorrelationManager.ActivityId, target);
//.........这里部分代码省略.........
示例4: IssueWebRequestAsync
/// <summary>
/// Issues the web request asynchronous.
/// </summary>
/// <param name="method">The method.</param>
/// <param name="input">The input.</param>
/// <param name="options">request options</param>
/// <returns></returns>
public async Task<Response> IssueWebRequestAsync(
string method, Stream input, RequestOptions options)
{
options.Validate();
Stopwatch watch = Stopwatch.StartNew();
UriBuilder builder = new UriBuilder(
"http",
options.AlternativeHost,
options.Port,
options.AlternativeEndpoint);
Debug.WriteLine("Issuing request to endpoint {0}", builder.Uri);
HttpWebRequest httpWebRequest = WebRequest.CreateHttp(builder.Uri);
//httpWebRequest.ServicePoint.ReceiveBufferSize = options.ReceiveBufferSize;
httpWebRequest.ServicePoint.UseNagleAlgorithm = options.UseNagle;
httpWebRequest.Timeout = options.TimeoutMillis;
httpWebRequest.KeepAlive = options.KeepAlive;
httpWebRequest.Credentials = null;
httpWebRequest.PreAuthenticate = true;
httpWebRequest.Method = method;
httpWebRequest.Accept = _contentType;
httpWebRequest.ContentType = _contentType;
if (options.AdditionalHeaders != null)
{
foreach (var kv in options.AdditionalHeaders)
{
httpWebRequest.Headers.Add(kv.Key, kv.Value);
}
}
long remainingTime = options.TimeoutMillis;
if (input != null)
{
// expecting the caller to seek to the beginning or to the location where it needs to be copied from
Stream req = null;
try
{
req = await httpWebRequest.GetRequestStreamAsync().WithTimeout(
TimeSpan.FromMilliseconds(remainingTime),
"Waiting for RequestStream");
remainingTime = options.TimeoutMillis - watch.ElapsedMilliseconds;
if (remainingTime <= 0)
{
remainingTime = 0;
}
await input.CopyToAsync(req).WithTimeout(
TimeSpan.FromMilliseconds(remainingTime),
"Waiting for CopyToAsync",
CancellationToken.None);
}
catch (TimeoutException)
{
httpWebRequest.Abort();
throw;
}
finally
{
if (req != null)
{
req.Close();
}
}
}
try
{
remainingTime = options.TimeoutMillis - watch.ElapsedMilliseconds;
if (remainingTime <= 0)
{
remainingTime = 0;
}
HttpWebResponse response = (HttpWebResponse)await httpWebRequest.GetResponseAsync().WithTimeout(
TimeSpan.FromMilliseconds(remainingTime),
"Waiting for GetResponseAsync");
return new Response()
{
WebResponse = response,
RequestLatency = watch.Elapsed
};
}
catch (Exception ex)
{
if (ex is WebException)
{
WebException we = (WebException)ex;
var resp = we.Response as HttpWebResponse;
return new Response()
{
WebResponse = resp,
RequestLatency = watch.Elapsed
//.........这里部分代码省略.........
示例5: IssueWebRequestAsync
/// <summary>
/// Issues the web request asynchronous.
/// </summary>
/// <param name="endpoint">The endpoint.</param>
/// <param name="method">The method.</param>
/// <param name="input">The input.</param>
/// <param name="options">request options</param>
/// <returns></returns>
public async Task<Response> IssueWebRequestAsync(string endpoint, string method, Stream input, RequestOptions options)
{
options.Validate();
Stopwatch watch = Stopwatch.StartNew();
Trace.CorrelationManager.ActivityId = Guid.NewGuid();
var balancedEndpoint = _balancer.GetEndpoint();
UriBuilder builder = new UriBuilder(
balancedEndpoint.Scheme,
balancedEndpoint.Host,
options.Port,
options.AlternativeEndpoint + endpoint);
var target = builder.Uri;
try
{
Debug.WriteLine("Issuing request {0} to endpoint {1}", Trace.CorrelationManager.ActivityId, target);
HttpWebRequest httpWebRequest = WebRequest.CreateHttp(target);
httpWebRequest.ServicePoint.ReceiveBufferSize = options.ReceiveBufferSize;
httpWebRequest.ServicePoint.UseNagleAlgorithm = options.UseNagle;
httpWebRequest.Timeout = options.TimeoutMillis;
httpWebRequest.KeepAlive = options.KeepAlive;
httpWebRequest.Credentials = _credentialCache;
httpWebRequest.PreAuthenticate = true;
httpWebRequest.Method = method;
httpWebRequest.Accept = _contentType;
httpWebRequest.ContentType = _contentType;
if (options.AdditionalHeaders != null)
{
foreach (var kv in options.AdditionalHeaders)
{
httpWebRequest.Headers.Add(kv.Key, kv.Value);
}
}
if (input != null)
{
// seek to the beginning, so we copy everything in this buffer
input.Seek(0, SeekOrigin.Begin);
using (Stream req = httpWebRequest.GetRequestStream())
{
await input.CopyToAsync(req);
}
}
Debug.WriteLine("Waiting for response for request {0} to endpoint {1}", Trace.CorrelationManager.ActivityId, target);
var response = (await httpWebRequest.GetResponseAsync()) as HttpWebResponse;
Debug.WriteLine("Web request {0} to endpoint {1} successful!", Trace.CorrelationManager.ActivityId, target);
return new Response()
{
WebResponse = response,
RequestLatency = watch.Elapsed,
PostRequestAction = (r) =>
{
if (r.WebResponse.StatusCode == HttpStatusCode.OK || r.WebResponse.StatusCode == HttpStatusCode.Created)
{
_balancer.RecordSuccess(balancedEndpoint);
}
else
{
_balancer.RecordFailure(balancedEndpoint);
}
}
};
}
catch (Exception e)
{
_balancer.RecordFailure(balancedEndpoint);
Debug.WriteLine("Web request {0} to endpoint {1} failed!", Trace.CorrelationManager.ActivityId, target);
throw e;
}
}
示例6: IssueWebRequestAsync
/// <summary>
/// Issues the web request asynchronous.
/// </summary>
/// <param name="endpoint">The endpoint.</param>
/// <param name="query">The query to append.</param>
/// <param name="method">The method.</param>
/// <param name="input">The input.</param>
/// <param name="options">request options</param>
/// <returns></returns>
public async Task<Response> IssueWebRequestAsync(
string endpoint, string query, string method, Stream input, RequestOptions options)
{
options.Validate();
Stopwatch watch = Stopwatch.StartNew();
UriBuilder builder = new UriBuilder(
_credentials.ClusterUri.Scheme,
_credentials.ClusterUri.Host,
options.Port,
options.AlternativeEndpoint + endpoint);
if (query != null)
{
builder.Query = query;
}
Debug.WriteLine("Issuing request {0} to endpoint {1}", Trace.CorrelationManager.ActivityId, builder.Uri);
HttpWebRequest httpWebRequest = WebRequest.CreateHttp(builder.Uri);
httpWebRequest.ServicePoint.ReceiveBufferSize = options.ReceiveBufferSize;
httpWebRequest.ServicePoint.UseNagleAlgorithm = options.UseNagle;
httpWebRequest.Timeout = options.TimeoutMillis; // This has no influence for calls that are made Async
httpWebRequest.KeepAlive = options.KeepAlive;
httpWebRequest.Credentials = _credentialCache;
httpWebRequest.PreAuthenticate = true;
httpWebRequest.Method = method;
httpWebRequest.Accept = _contentType;
httpWebRequest.ContentType = _contentType;
// This allows 304 (NotModified) requests to catch
//https://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.allowautoredirect(v=vs.110).aspx
httpWebRequest.AllowAutoRedirect = false;
if (options.AdditionalHeaders != null)
{
foreach (var kv in options.AdditionalHeaders)
{
httpWebRequest.Headers.Add(kv.Key, kv.Value);
}
}
long remainingTime = options.TimeoutMillis;
if (input != null)
{
// expecting the caller to seek to the beginning or to the location where it needs to be copied from
Stream req = null;
try
{
req = await httpWebRequest.GetRequestStreamAsync().WithTimeout(
TimeSpan.FromMilliseconds(remainingTime),
"Waiting for RequestStream");
remainingTime = options.TimeoutMillis - watch.ElapsedMilliseconds;
if(remainingTime <= 0)
{
remainingTime = 0;
}
await input.CopyToAsync(req).WithTimeout(
TimeSpan.FromMilliseconds(remainingTime),
"Waiting for CopyToAsync",
CancellationToken.None);
}
catch (TimeoutException)
{
httpWebRequest.Abort();
throw;
}
finally
{
req?.Close();
}
}
try
{
remainingTime = options.TimeoutMillis - watch.ElapsedMilliseconds;
if (remainingTime <= 0)
{
remainingTime = 0;
}
HttpWebResponse response = (HttpWebResponse) await httpWebRequest.GetResponseAsync().WithTimeout(
TimeSpan.FromMilliseconds(remainingTime),
"Waiting for GetResponseAsync");
return new Response()
{
WebResponse = response,
RequestLatency = watch.Elapsed
};
}
catch (TimeoutException)
//.........这里部分代码省略.........