Thursday, May 5, 2011

Flex and Cairngorm Error: C0001E: Only one ServiceLocator instance can be instantiated.

Hey guys,

I’m new to Flex and Cairngorm.While I’m using ServiceLocator,I do run into the problem: Error: C0001E: Only one ServiceLocator instance can be instantiated.

My Code is like this:

In Serives.mxml:

<cairngorm:ServiceLocator xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:cairngorm="com.adobe.cairngorm.business.*">
<mx:HTTPService id="statistServ"
    url="rooms.xml"
    showBusyCursor="true"
    method="POST"
    resultFormat="array"/>

In Delegate.as,I have snippets:

this.service = ServiceLocator.getInstance().getHTTPService(”statistServ”);

In Main.xml,snippets like:

<business:Service id="service" />

this wonderful little Error message pops up the minute I load a second instance of some module which requires httpservice.

Is there any way to resolve this problem without switching to another Framework?

Best Wishes,

Shuo from China

From stackoverflow
  • The error you're seeing is from Cairngorm's implementation of the Singleton pattern. It's preventing you from creating a second instance of ServiceLocator because the framework requires that there be only one. When loading the second instance of your module, your code is also trying to create a second instance of ServiceLocator when it should be using getInstance() (or not getting an instance at all).

    By declaring your Services tag in Main.mxml, you've created your lone instance of ServiceLocator and it cannot be declared or newed anywhere else. If Main.mxml is part of the module that you're trying to instantiate more than once, that's your problem right there. If not, I can't tell where in your code the problem is based on the limited sample above, but it should be somewhere in the module.

    In reply to comment:

    No problem. You can easily solve your problem by declaring your Service in ActionScript instead of MXML. By declaring a tag in MXML, you're always creating an instance of the corresponding AS class, whereas you instead want to retrieve the lone instance of the singleton. To do this, include the following AS code inside of your module MXML:

    var service:Service = Service(ServiceLocator.getInstance());
    

    That is essentially equivalent of what you're trying to do with the MXML tag, with the exception of not creating a new Service instance.

    However, you still need to instantiate the Service somewhere. To do that, put the Service MXML tag inside of the main MXML of the application which is loading the modules.

    Shuo : Thanks,Stiggler! You're right!In my module,I have in Main.mxml. Each time I open the module,I'm actually instantiating a serviceLocator.As we know,ServiceLocator validates a static variable to see if an instance has already been created in its constructor.But I think the constructor ServiceLocator should be private but current AS3.0 does not allow it. So,is there any way to solve this?As the module is used quite ofen. Much Thanks! Regards,Shuo
    Shuo : Thanks again,Stiggler! I've solved my problems by placing inside of the main MXML of the application instead of the module MXML. You're very helpful! Good luck! Shuo from China
    Stiggler : I'm glad I could help. If you feel this question has been fully answered, please go ahead and mark the answer as accepted.
    Shuo : Hey Stiggler!I'm not sure how to mark the answer as accepted.I'll be glad to mark it if I know the way.
    Stiggler : Just click the check mark icon that appears to the left hand side of this answer :)

0 comments:

Post a Comment