title: How to integrate TPL Dataflow in ASP NET Core
body: <p>Hello have looked with intense interest lately to <code>TPL Dataflow</code>  and i want to integrate it in my <code>ASP .NET Core</code> application.
  I want to use it as a pipeline where multiple methods from different parts of application can post data to this DataFlow chain.
  What i do not know is where do you store your link of blocks  in case you want them to be called from multiple places? </p>
  
  <p><strong>Producer</strong></p>
  
  <pre><code>public class Producer
  {
      private BufferBlock&lt;int&gt; startBlock{get;}
      private ActionBlock&lt;int&gt; ioBlock{get;}
      private IOService service;
  
      private void InitializeChain()
      {
         this.startBlock=new BufferBlock&lt;int&gt;();
         var transformLink=new TransformBlock&lt;int,string&gt;([something]);
         // some chain of blocks here 
         this.ioBlock=new ActionBlock&lt;int&gt;(async(x)=&gt;await this.service.WriteAsync(x));
         this.startBlock.LinkTo([someBlock]).LinkTo([someOtherBlock])......LinkTo(ioBlock);
      }
      public async Task AddAsync(int data)
      {
          this.BufferBlock.Post(data);
      }
      public Producer(IOService service)
      {
          this.service=service;
          this.InitializeChain();
      }
  }
  </code></pre>
  
  <p><strong>API Producers</strong><br>
  I am envisioning this <code>Producer</code> getting called from multiple parts of my application , well use <code>Controller</code>-s for brevity:</p>
  
  <pre><code>public class C1:Controller
  {
      private Producer producer;
      [HttpPost]
      [Route([someroute])
      public async Task SomeRoute(int data)
      {
          await  this.producer.AddAsync(data);
      }
      [HttpGet]
      [Route([someotherroute])
      public async Task SomeOtherRoute(int data)
      {
          await  this.producer.AddAsync(data);
      }
      public C1(Producer producer)
      {
        this.producer=producer;
      }
  }
  </code></pre>
  
  <p><strong>Startup</strong></p>
  
  <pre><code>  public void ConfigureServices(IServiceCollection services) {
             services.AddSingleton&lt;Producer&gt;();
    }
  </code></pre>
  
  <p>This can be extended to a multiple <code>Controller</code> scenario or deeper in the hierarchy.</p>
  
  <p>Now my question would be:<br>
  How should the <code>Producer</code> that keeps the <code>Dataflow</code> chain be injected ? Should it be transient ? Should the <code>Blocks</code> be instantiated on every call ? </p>
  
  <p>I do not know if this design is ok.I know TPL Dataflow is threadsafe , but can  it be used this way? </p>
  
  <p><strong>P.S</strong> I basically do not know in what form to keep my <code>Dataflow</code> pipeline and its lifetime , if i want it to be available per the entire scope of my <code>ASP NET Core</code> application.
  I want to fetch data from multiple endpoints (directly or deeper in the call hierarchy) , batch them ,transform them , and control the way they are in the end written to an external source (<code>async</code> operation).
  Does this play nice with the already existing <code>ThreadPool</code> of <code>ASP NET Core</code> ?</p>
  
  <p><strong>P.S 2</strong>: This question also haunts me for an <code>Rx</code> equivalent.</p>