In the context of your question, I assume that a lengthy survey is some kind of operation that periodically issues an HTTP request to a third-party resource until the desired response is returned or until the timeout expires.
, .NET 4.5, TAP async/.
():
[ServiceContract]
public interface IService
{
[OperationContract]
Task<bool> DoLongPollingAsync(string url, int delay, int timeout);
}
public class Service : IService
{
public async Task<bool> DoLongPollingAsync(
string url, int delay, int timeout)
{
using (var cts = new CancellationTokenSource(timeout))
using (var httpClient = new System.Net.Http.HttpClient())
using (cts.Token.Register(() => httpClient.CancelPendingRequests()))
{
try
{
while (true)
{
var data = await httpClient.GetStringAsync(url).ConfigureAwait(false);
if (data == "END POLLING")
return true;
await Task.Delay(delay, cts.Token);
}
}
catch (OperationCanceledException)
{
if (!cts.IsCancellationRequested)
throw;
}
throw new TimeoutException();
}
}
}
, DoLongPolling , HttpClient.GetStringAsync Task.Delay. , ThreadPool, WCF DoLongPolling . "No Thread" Stephen Cleary , .
WCF . " " - WCF " ".
.NET 4.0, " WCF" Jaliya Udagedara.