Service and ServiceDependency?

Topics: CAB & Smart Client Software Factory
Jun 27, 2006 at 5:18 AM
originally posted by: shahramjam

Hi
I have created a module and a service in the module and done everything exact as in docs is said.
but when I run application I get immediatly this error:
InnerExceptionService Invoice.Module1.IMyService is not available in the current context.
System.Exception {Microsoft.Practices.CompositeUI.Services.ServiceMissingException}
{
My Service:
Service(typeof(IMyService))
public class MyService: IMyService
{}

My Module:

InjectionConstructor
public Module(ServiceDependency WorkItem rootWorkItem,
ServiceDependency IMyService myService)
{
_rootWorkItem = rootWorkItem;
_myService = myService;
}

Any Konowlage what is about?

Regards
Jun 27, 2006 at 6:23 AM
originally posted by: ksunair

Where is your service? Is it in the same dll or someother dll? If it is in like common.dll add the common.dll in the profile catalog?

My second thought is you need to add the service in the root workitem so that everyone can use it.
Jun 27, 2006 at 6:41 AM
originally posted by: shahramjam

My Service is in samme module and In workitemconstructer in module file I use it so it is in root workitem for my module:

InjectionConstructor
public Module(ServiceDependency WorkItem rootWorkItem,
ServiceDependency IMyService myService)
{
_rootWorkItem = rootWorkItem;
_myService = myService;
}

so what is the problem?
Jun 27, 2006 at 6:58 AM
originally posted by: ksunair

In your module controller, do you have add services method? Try adding the service in their and see if you can use it.
If still doesn't work, I can send you a sample that I am using.
Jun 27, 2006 at 7:10 AM
originally posted by: shahramjam

Thanx for the answer, I will try that way by using add method but in the docs for SCSF they say
if you use service attribute in your service class like this:

Service(typeof(IMyService))
public class MyService: IMyService
{}

objectbuilder take care of it! Am I wrong or there is a bug or I have done it wrong way?
Regards
Jun 27, 2006 at 7:41 AM
originally posted by: ksunair

This is how I did it. Following displays message box. One more thing, I have this on the presenter rather than the view. Since preseter is build up during initial build up.

private IMessageBoxService _messageBox;
ServiceDependency
public IMessageBoxService MessageBoxService
{
set { _messageBox = value; }
}

I use it in the presenter itself as
_messageBox.Show(MessageReference.TaxReturnInRS);

I did not add the service in my shell or view. Hope this helps.
Jun 27, 2006 at 9:46 PM
originally posted by: shahramjam

I do exact as you but still I get this error:
"Service InvoiceServices.SC.ClientManagement.Module.Services.IMyService is not available in the current context."
!?
Jun 28, 2006 at 4:00 AM
originally posted by: ksunair

Sorry, Is it a compile time error or run time error?

Also yesterday I wrote a new service and here is what I have done, it is little different from the messagebox service, This is to log the events using elib logging block.

1. Added both LoggingService and its interface in the common.dll
2. Added logging elib in the reference
3. In my presenter I added

private ILoggingService _log = null;
ServiceDependency
public ILoggingService LoggingService
{
set { _log = value; }
}

in my method I have
_log.WriteError("Offline log");

and I do have 'using' to include the services.

One think I noticed, 'you need not add the services anywhere' in the module like it mentioned in the docs.

Thanks.
Jun 28, 2006 at 4:50 AM
originally posted by: shahramjam

Hi!
thanks for your answer.
It is a runtime error.
I have my service interface and service class in my module dll.
You use your service in your presenter class but I use it in my workitem(module class):
I mean this is in my module class:
ServiceDependency
public ILoggingService LoggingService
{
set { _log = value; }
}
and in my modulecontroller in addservices method I do this:
workitem.services.addnew<Myaervices,Imysevice>() and it works fine
The issue is :
ServiceDependency
public IMyService MyService
{
set { _log = value; }
}
in my module class that results in a exception and it says
"Service InvoiceServices.SC.ClientManagement.Module.Services.IMyService is not available in the current context."

If I am correct ServiceDependency means objectbuilder must inject the service and it throws the exception.
Any idea?
Regards
Jun 28, 2006 at 5:20 AM
originally posted by: ksunair

I am new as you so sorry I couldn't answer stright to you. But, I have couple of questions though

1. Why are you creating the instance in a module rather than in the place where you will be using it, like view/presenter?
2. Can you try removing the addnew and move the servicedepency to the place where you would like to use and see if it still fails?

Thanks.
Jun 28, 2006 at 5:47 AM
originally posted by: matiaswoloski

This is a problem on the order of events. Let me explain how this will be executed by CAB.

1. Get a list of modules from the Profile Catalg
2. Load the assemblies (Assembly.LoadFrom)
3. foreach assembly
a. Look for ModuleInit derived classes in each assembly
b. Execute the AddServices method of ModuleInit classes
c. Look for classes with Service attribute in the assembly and add them as services to the RootWorkItem
d. Look for ServiceDependency attribute in the classes that are being added to the workitem (like ModuleInit derived class, and any class added to the workitem during the initialization of the module. Inject the service.
e. Execute the Load method of ModuleInit derived classes
4. In this case in the Load method you are adding a child workitem (ControlledWorkItem<ModuleController>)
5. Execute the Run method of the Workitem controller (ModuleController)

If you realize the order, you will notice that you are trying to inject the ILogginService in step 3.d, however you are adding the service instance in the ModuleController, and this guy has not run yet (step 5).

So either you:
- decorate the LoggingService class with Service(typeof(ILoggingService) and the service will be added on 3.c or
- add the service in the AddServices method of ModuleInit derived class (Module.cs)

Matias
Smart Client Software Factory Team
http://staff.southworks.net/blogs/matiaswoloski
Jun 28, 2006 at 7:44 AM
originally posted by: ksunair

I was going to ask the question on the order of execution. You read my mind thanks for the explanation.