Flex and Cairngorm error: C0001E: Only one instance of ServiceLocator can be created

Im new to Flex and Cairngorm.While Im using ServiceLocator I ran into problem: Error: C0001E: Only one instance of ServiceLocator can be created.

My code looks 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 fragments:

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

      

The Main.xml has snippets like:

<business:Service id="service" />

      

this wonderful little error message appears the moment I load a second instance of some module that requires an httpservice.

Is there a way to solve this problem without switching to another Framework?

Best regards,

Shuo from China

0


a source to share


1 answer


The error you are seeing is Cairngorm's implementation of the Singleton pattern. This prevents you from creating a second instance of the ServiceLocator because only one is required within the framework. When loading a second instance of your module, your code also tries to create a second instance of the ServiceLocator when it should use getInstance () (or not get an instance at all).

By declaring your Services tag in Main.mxml, you have created your single instance of ServiceLocator and it cannot be declared or newer anywhere. If Main.mxml is part of a module that you are trying to create multiple times, that is your problem right there. If not, I can't tell where in your code the problem is based on the limited example above, but it must be in a module somewhere.

In response to a comment:

No problems. You can easily solve your problem by declaring your service in ActionScript instead of MXML. When declaring a tag in MXML, you always instantiate the corresponding AS class, whereas instead you want a single instance of a singleton. To do this, include the following AS code inside the MXML module:



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

      

This is essentially equivalent to what you are trying to do with the MXML tag, except that you don't create a new instance of the service.

However, you still need to create an instance of the service. To do this, place the Service MXML tag inside the main MXML application that loads the modules.

+2


a source







All Articles