title: Task Persistence C#
body: <p>I'm having a hard time trying to get my task to stay persistent and run indefinitely from a WCF service. I may be doing this the wrong way and am willing to take suggestions. </p>
  
  <p>I have a task that starts to process any incoming requests that are dropped into a BlockingCollection. From what I understand, the GetConsumingEnumerable() method is supposed to allow me to persistently pull data as it arrives. It works with no problem by itself. I was able to process dozens of requests without a single error or flaw using a windows form to fill out the request and submit them. Once I was confident in this process I wired it up to my site via an asmx web service and used jQuery ajax calls to submit request. </p>
  
  <p>The site submits request based on a url that is submitted, the Web Service downloads the html content from the url and looks for other urls within the content. It then proceeds to create a request for each url it finds and submits it to the BlockingCollection. Within the WCF service, if the application is Online (i.e. Task has started) - it pulls the request using the GetConsumingEnumerable via a Parallel.ForEach and Processes the request.</p>
  
  <p>This works for the first few submissions, but then the task just stops unexpectedly. Of course, this is doing 10x more request than I could simulate in testing - but I expected it to just throttle. I believe the issue is in my method that starts the task:</p>
  
  <pre><code> public void Start()
          {
              Online = true;
  
              Task.Factory.StartNew(() =&gt;
              {
                  tokenSource = new CancellationTokenSource();
                  CancellationToken token = tokenSource.Token;
                  ParallelOptions options = new ParallelOptions();
                  options.MaxDegreeOfParallelism = 20;
                  options.CancellationToken = token;
  
                  try
                  {
                      Parallel.ForEach(FixedWidthQueue.GetConsumingEnumerable(token), options, (request) =&gt;
                      {
                          Process(request);
                          options.CancellationToken.ThrowIfCancellationRequested();
  
                      });
                  }
                  catch (OperationCanceledException e)
                  {
                      Console.WriteLine(e.Message);
                      return;
                  }
  
              }, TaskCreationOptions.LongRunning);
  
          }
  </code></pre>
  
  <p>I've thought about moving this into a WF4 Service and just wire it up in a Workflow and use Workflow Persistence, but am not willing to learn WF4 unless necessary. Please let me know if more information is needed.</p>