I have a windows service (using Topshelf
) which hosts a WCF
service (created using SimpleInjectorServiceHost
) and also spins up threads (using Task.Run
).
The idea is to:
WCF
service receives commands- post those commands to
BufferBlock<T>
. The service needs thisBufferBlock<T>
injected into its constructor - threads are supposed to be
TransformBlock<T>
's which will do the required action and return result - this result needs to be read back by the
WCF
service method and returned back to the caller
I am just starting with TPL Dataflow so maybe my understanding of action blocks is totally wrong
I spin the threads in the windows service OnStart method
for(var i = 0, i < Environment.ProcessorCount, ++i)
{
Task.Run(() =>
{
using (var scope = Bootstrapper.BeginScope())
{
var threadHost = Bootstrapper.GetInstance<IThreadHost>();
threadHost.DoWork(_cancellationTokenSource.Token);
}
}, _cancellationTokenSource.Token);
};
Bootstrapper.BeginScope
simply abstracts away SimpleInjector's _container.BeginLifetimeScope()
method
IThreadHost
/ThreadHost
is a simple interface/class that has methods for each command type. DoWork
is supposed to wait on the BufferBlock
and process each item once available.
The WCF
service is started using
if (_serviceHost != null)
{
_serviceHost.Close();
}
_serviceHost = Bootstrapper.CreateServiceHost(typeof(NodeService));
_serviceHost.Open();
Bootstrapper.CreateServiceHost
simply abstracts away new SimpleInjectorServiceHost(_container, serviceType)
.
Q: Should the BufferBlock<T>
be a singleton registered with SimpleInjector
?
Q: How to set DoWork
of each threadHost
as the TransformBlock<T>
and link to the BufferBlock<T>
?
Q: How do I get output of the TransformBlock<T>
in my WCF
service method? WCF
service methods writes the command to BufferBlock<T>
and then needs to wait for the output somehow?