You can affect the order of services using an IHandlersFilter
implementation
public class ServiceHandlersFilter : IHandlersFilter
{
private readonly List<Type> sortOrder = new List<Type>
{
{ typeof(Service1) },
{ typeof(Service2) },
{ typeof(Service3) },
};
public bool HasOpinionAbout(Type service)
{
return service == typeof(IService);
}
public IHandler[] SelectHandlers(Type service, IHandler[] handlers)
{
return handlers
.OrderBy(h => sortOrder.IndexOf(h.ComponentModel.Implementation))
.ToArray();
}
}
You may want the sort order to be determined more elegantly, for example, by the components themselves. If that is the case, check out this RespectOrderDirectivesHandlersFilter
implementation. As per mookid's blog post on the filter, you can attribute your IService
implementations with the ExecutesBeforeAttribute
and ExecutesAfterAttribute
attributes to imply order of execution relative to each other.