Intra-Module [Service][ServiceDependency] solver

Topics: CAB & Smart Client Software Factory
Dec 2, 2005 at 1:22 PM
originally posted by: jdressel

I have run into an issue (more of an interesting future concept) in resolving intra-module services declared with the Service attribute that also declare service dependencies via the ServiceDependency attribute. This is because the Servicediscovery/loading and ServiceDependencydependency resolution is a 2 pass mechanism. You cannot guarantee the reflection discovery and creation order of the Service services who may also innerly declare ServiceDependencyies to services in the same assembly. The Services are reflected upon, instances created and added to the container. Upon adding to the container, the ServiceDependency are attempted to be resolved. You may get lucky the dependent service has already been contained and the injection may appear to work.

Service
public class ServiceFooBar
{
ServiceFoo foo = null;
ServiceBar bar = null;

ServiceDependency
public ServiceFoo Foo
{
get { return foo; }
set { foo = value; }
}

ServiceDependency
public ServiceBar Bar
{
get { return bar; }
set { bar = value; }
}
}

Service
public class ServiceBar {}

Service
public class ServiceFoo {}

public class Program : CabApplication<WorkItem>
{
STAThread
static void Main()
{
new Program().Run();
}

protected override void Start(){}
}

The work around is easy, just override the AddServices() method in your ModuleInit class to control the load order. But more interestingly, is it intended in the future that the module loading mechanism will create an intra-module service dependency graph that considers Service-ServiceDependency dependencies? Currently a given ModuleMetadata only has an unsorted list of ServiceMetadata.
Dec 2, 2005 at 1:32 PM
originally posted by: BradWilsonMSFT

Yeah, there's nothing you can do about this, except not use ServiceDependency for the service. As a work-around, you can use ServiceDependency for your WorkItem, and then delay-retrieve the service as needed using WorkItem.Services.