ERRO FLEX e CAIRNGORM: C0001E: Apenas uma instância do ServiceLocator pode ser instanciada

StackOverflow https://stackoverflow.com/questions/832445

Pergunta

Eu sou novo no Flex e CairnGorm. Enquanto estou usando o ServiceLocator, encontro o problema: Erro: C0001E: Apenas uma instância do ServiceLocator pode ser instanciada.

Meu código é assim:

Em sorves.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"/>

Em delegate.a, tenho trechos:

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

Em main.xml, trechos como:

<business:Service id="service" />

Esta maravilhosa mensagem de erro aparece no minuto em que carrego uma segunda instância de algum módulo que requer HttpService.

Existe alguma maneira de resolver esse problema sem mudar para outra estrutura?

Muitas felicidades,

Shuo da China

Foi útil?

Solução

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top