.NET, Architecture, ALM, Cloud
Le Framework DataFlow mixés avec WCF vont être un excellent moyen pour vous permettre de gréer des services asynchrone devant effectuer des tâches longues à mettre en file d’attente.
Considérons le contrat de service suivant :
[ServiceContract]
public interface IDemoService
{
[OperationContract(IsOneWay=true)]
void PostRequest(MyRequest request);
}
[ServiceBehavior(ConcurrencyMode=ConcurrencyMode.Multiple, InstanceContextMode=InstanceContextMode.Single)]
public class DemoService : IDemoService
{
private static ActionBlock<MyRequest> _queue;
static DemoService()
{
ExecutionDataflowBlockOptions options = new ExecutionDataflowBlockOptions()
{
MaxDegreeOfParallelism = 10
};
_queue = new ActionBlock<MyRequest>((R) =>
{
Thread.Sleep(5000);
File.WriteAllText(Path.Combine(@"C:\Demo",
tring.Format("{0}.txt", Guid.NewGuid())), String.Empty);
}, options);
}
public void PostRequest(MyRequest request)
{
_queue.Post(request);
}
}