Error in Resolution of the dependency

Topics: Policy Injection Application Block
Jul 22, 2011 at 4:30 PM

Hi for all.

I have a problem in resolution of the dependency in only one interface in the language C#.

Above I post the error:

Resolution of the dependency failed, type = "CommonLibraries.Interfaces.IQueue`1[Comissoes.DAL.ApuracaoQueueInfo]", name = "". Exception message is: The current build operation (build key Build Key[CommonLibraries.Interfaces.IQueue`1[Comissoes.DAL.ApuracaoQueueInfo], null]) failed: The current type, CommonLibraries.Interfaces.IQueue`1[Comissoes.DAL.ApuracaoQueueInfo], is an interface and cannot be constructed. Are you missing a type mapping? (Strategy type BuildPlanStrategy, index 3)

And the code

 public static class DependencyInjection
    {
        private const string ERRO_UNITY_SECTION = "A SECTION 'unity' NÃO PODE SER VAZIA.";
        private const string ERRO_DEFAULT_SECTION = "O CONTAINER DEFAULT NÃO PODE SER VAZIO.";

        private static IUnityContainer UnityContainer { get; set; }

        private static void ConfigureContainer()
        {
            DependencyInjection.UnityContainer = new UnityContainer();

            UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");

            if (section == null)
                throw new Exception(ERRO_UNITY_SECTION);

            UnityContainerElement containerElement = section.Containers.Default;
            if (containerElement == null)
                throw new Exception(ERRO_DEFAULT_SECTION);

            containerElement.Configure(UnityContainer);
        }

        public static T Resolve<T>()
        {
            if (UnityContainer == null)
                DependencyInjection.ConfigureContainer();

            return UnityContainer.Resolve<T>();
        }
    }

 

        public int ObtemCodigoApuracaoIniciar()
        {
            IQueue<ApuracaoQueueInfo> dal = DependencyInjection.Resolve<IQueue<ApuracaoQueueInfo>>();
            ApuracaoQueueInfo mensagem = dal.GetMessage(KEY_QUEUE_APURACAO);
            if (mensagem != null)
            {
                return mensagem.CodigoApuracao;
            }
            return -1;
        }
    }

The typeof IQueue is CommonLibraries.Interfaces.IQueue

Jul 25, 2011 at 3:27 AM

Hi,

Can you post your config here so we can know how did you register your types?

 

Noel Angelo Bolasoc
Global Technologies and Solutions
Avanade, Inc.
Contact Us

 

Jul 25, 2011 at 2:08 PM

Follow

<configuration>

<configSections>

<section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration"/>

</configSections>

<unity>

<typeAliases>

<typeAlias alias="IValidator" type="CommonLibraries.ValidateHelper.IValidator, CommonLibraries.ValidateHelper"/>

<typeAlias alias="DefaultValidator" type="CommonLibraries.ValidateHelper.DefaultValidator, CommonLibraries.ValidateHelper"/>

<typeAlias alias="IQueue" type="CommonLibraries.Interfaces.IQueue`1, CommonLibraries.Interfaces"/>

<typeAlias alias="QueueHelper" type="CommonLibraries.AzureStorage.QueueHelper`1, CommonLibraries.AzureStorage"/>

</typeAliases>

<containers>

<container>

<types>

<type type="IValidator" mapTo="DefaultValidator"/>

<type type="IQueue" mapTo="QueueHelper"/>

<type type="Comissoes.IDAL.IApuracaoDal, Comissoes.IDAL" mapTo="Comissoes.SQLDal.ApuracaoDal, Comissoes.SQLDal">

<lifetime type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager, Microsoft.Practices.Unity" />

</type>

<type type="Comissoes.IDAL.IUsuarioDal, Comissoes.IDAL" mapTo="Comissoes.SQLDal.UsuarioDal, Comissoes.SQLDal">

<lifetime type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager, Microsoft.Practices.Unity" />

</type>

<type type="Comissoes.IDAL.IEmpreendimentoDal, Comissoes.IDAL" mapTo="Comissoes.SQLDal.EmpreendimentoDal, Comissoes.SQLDal">

<lifetime type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager, Microsoft.Practices.Unity" />

</type>

<type type="CommonLibraries.Interfaces.IFile, CommonLibraries.Interfaces" mapTo="CommonLibraries.AzureStorage.BlobStorage, CommonLibraries.AzureStorage">

<lifetime type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager, Microsoft.Practices.Unity" />

</type>

<type type="Comissoes.IDAL.IFilaApuracaoDal, Comissoes.IDAL" mapTo="Comissoes.SQLDal.FilaApuracaoDal, Comissoes.SQLDal">

<lifetime type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager, Microsoft.Practices.Unity" />

</type>

<type type="Comissoes.IDAL.IPoliticaDal, Comissoes.IDAL" mapTo="Comissoes.SQLDal.PoliticaDal, Comissoes.SQLDal">

<lifetime type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager, Microsoft.Practices.Unity" />

</type>

<type type="Comissoes.IDAL.IPreRequisitoDal, Comissoes.IDAL" mapTo="Comissoes.SQLDal.PreRequisitoDal, Comissoes.SQLDal">

<lifetime type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager, Microsoft.Practices.Unity" />

</type>

<type type="Comissoes.IDAL.IClienteDal, Comissoes.IDAL" mapTo="Comissoes.SQLDal.ClienteDal, Comissoes.SQLDal">

<lifetime type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager, Microsoft.Practices.Unity" />

</type>

<type type="Comissoes.IDAL.IVendaDal, Comissoes.IDAL" mapTo="Comissoes.SQLDal.VendaDal, Comissoes.SQLDal">

<lifetime type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager, Microsoft.Practices.Unity" />

</type>

<type type="Comissoes.IDAL.ICondicoesDal, Comissoes.IDAL" mapTo="Comissoes.SQLDal.CondicoesDal, Comissoes.SQLDal">

<lifetime type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager, Microsoft.Practices.Unity" />

</type>

<type type="Comissoes.IDAL.IImobiliariaDal, Comissoes.IDAL" mapTo="Comissoes.SQLDal.ImobiliariaDal, Comissoes.SQLDal">

<lifetime type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager, Microsoft.Practices.Unity" />

</type>

<type type="Comissoes.IDAL.IGVendasDal, Comissoes.IDAL" mapTo="Comissoes.GestaoVendasDal.GVendasDal, Comissoes.GestaoVendasDal">

<lifetime type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager, Microsoft.Practices.Unity" />

</type>

<type type="Comissoes.IDAL.IResultadoDal, Comissoes.IDAL" mapTo="Comissoes.SQLDal.ResultadoDal, Comissoes.SQLDal">

<lifetime type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager, Microsoft.Practices.Unity" />

</type>

<type type="Comissoes.IDAL.IImportacaoDal, Comissoes.IDAL" mapTo="Comissoes.TableDal.ImportacaoDal, Comissoes.TableDal">

<lifetime type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager, Microsoft.Practices.Unity" />

</type>

<type type="Comissoes.IDAL.IPedidoComissaoDal, Comissoes.IDAL" mapTo="Comissoes.SQLDal.PedidoComissaoDal, Comissoes.SQLDal">

<lifetime type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager, Microsoft.Practices.Unity" />

</type>

<type type="Comissoes.IDAL.IParametrosDal, Comissoes.IDAL" mapTo="Comissoes.SQLDal.ParametrosDal, Comissoes.SQLDal">

<lifetime type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager, Microsoft.Practices.Unity" />

</type>

<type type="CommonLibraries.Interfaces.IMail, CommonLibraries.Interfaces" mapTo="CommonLibraries.Mail.MailDal, CommonLibraries.Mail">

<lifetime type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager, Microsoft.Practices.Unity"/>

</type>

<type type="Comissoes.IDAL.IExtratoDal, Comissoes.IDAL" mapTo="Comissoes.SQLDal.ExtratoDal, Comissoes.SQLDal">

<lifetime type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager, Microsoft.Practices.Unity" />

</type>

<type type="Comissoes.IDAL.IProvisionamentoDal, Comissoes.IDAL" mapTo="Comissoes.SQLDal.ProvisionamentoDal, Comissoes.SQLDal">

<lifetime type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager, Microsoft.Practices.Unity" />

</type>

<type type="Comissoes.IDAL.IItemProvisionamentoDal, Comissoes.IDAL" mapTo="Comissoes.SQLDal.ItemProvisionamentoDal, Comissoes.SQLDal">

<lifetime type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager, Microsoft.Practices.Unity" />

</type>

<type type="Comissoes.IDAL.IModeloDal, Comissoes.IDAL" mapTo="Comissoes.SQLDal.ModelosDal, Comissoes.SQLDal">

<lifetime type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager, Microsoft.Practices.Unity" />

</type>

<type type="Comissoes.IDAL.IPerfilDal, Comissoes.IDAL" mapTo="Comissoes.SQLDal.PerfilDal, Comissoes.SQLDal">

<lifetime type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager, Microsoft.Practices.Unity" />

</type>

<type type="Comissoes.IDAL.ICoordenadorDal, Comissoes.IDAL" mapTo="Comissoes.SQLDal.CoordenadorDal, Comissoes.SQLDal">

<lifetime type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager, Microsoft.Practices.Unity" />

</type>

<type type="Comissoes.IDAL.ICoordenadorEmpreendimentoDal, Comissoes.IDAL" mapTo="Comissoes.SQLDal.CoordenadorEmpreendimentoDal, Comissoes.SQLDal">

<lifetime type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager, Microsoft.Practices.Unity" />

</type>

<type type="Comissoes.IDAL.IApuracaoCoordenadorDal, Comissoes.IDAL" mapTo="Comissoes.SQLDal.ApuracaoCoordenadorDal, Comissoes.SQLDal">

<lifetime type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager, Microsoft.Practices.Unity" />

</type>

</types>

</container>

</containers>

</unity>

Sérgio Rezende Júnior

MCOM Soluções e Tecnologia

(17) 3304-4440

De: AvanadeSupport [email removed]
Enviada em: domingo, 24 de julho de 2011 23:28
Para: Sérgio Rezende Jr.
Assunto: Re: Error in Resolution of the dependency [entlib:266228]

From: AvanadeSupport

Hi,

Can you post your config here so we can know how did you register your types?

Noel Angelo Bolasoc
Global Technologies and Solutions
Avanade, Inc.
Contact Us

Jul 25, 2011 at 2:16 PM
Edited Jul 25, 2011 at 2:17 PM

Hi.

I can to resolve the DI for same Interface (IQueue) to insert message in queue, but when i try read the error happens.

I use Enterprise Library 4.1 This interface is used for resolve DI.

public interface IQueue<T> where T: class

{

 void AddMessage(string  

queuename, T message);

T GetMessage(string

queueName);

}

 

Jul 26, 2011 at 3:56 AM

Your registration looks fine. Can you also post your QueueHelper and ApuracaoQueueInfo Class? 



Noel Angelo Bolasoc
Global Technologies and Solutions
Avanade, Inc.
Contact Us

Jul 26, 2011 at 3:36 PM

Hi follow the code of the Interface IQueue:

public interface IQueue<T> where T: class

{

void AddMessage(string queuename, T message);

T GetMessage(string queueName);

}

And code of the class QueueHelper

public class QueueHelper<T> : IQueue<T> where T : class

{

private readonly CloudQueueClient clientQueue;

public QueueHelper(string configurationSetting)

: this(CloudStorageAccount.FromConfigurationSetting(configurationSetting))

{

}

public QueueHelper(CloudStorageAccount account)

{

this.clientQueue = CloudStorageAccountStorageClientExtensions.CreateCloudQueueClient(account);

}

public void AddMessage(string queueName, T message)

{

if (!typeof(T).IsSerializable && !(typeof(T) is ISerializable))

throw new InvalidOperationException("A serializable type is required");

CloudQueue cloudQueue = this.clientQueue.GetQueueReference(queueName);

cloudQueue.CreateIfNotExist();

cloudQueue.AddMessage(new CloudQueueMessage(this.ObjectToByteArray(message)));

}

public T GetMessage(string queueName)

{

CloudQueue queue = this.clientQueue.GetQueueReference(queueName);

CloudQueueMessage cloudMessage = queue.GetMessage();

if (cloudMessage == null)

return null;

T message = (T)this.ByteArrayToObject(cloudMessage.AsBytes);

this.DeleteMessage(queueName, cloudMessage);

return message;

}

private void DeleteMessage(string queueName, CloudQueueMessage message)

{

CloudQueue queue = this.clientQueue.GetQueueReference(queueName);

queue.DeleteMessage(message);

}

private byte[] ObjectToByteArray(Object obj)

{

if (obj == null)

return null;

BinaryFormatter bf = new BinaryFormatter();

MemoryStream ms = new MemoryStream();

bf.Serialize(ms, obj);

return ms.ToArray();

}

private Object ByteArrayToObject(byte[] arrBytes)

{

MemoryStream memStream = new MemoryStream();

BinaryFormatter binForm = new BinaryFormatter();

memStream.Write(arrBytes, 0, arrBytes.Length);

memStream.Seek(0, SeekOrigin.Begin);

Object obj = (Object)binForm.Deserialize(memStream);

return obj;

}

}

And the calling in ImportacaoVendaDal

public class ImportacaoVendaDal

{

private string KEY_QUEUE_IMPORTACAO { get { return ConfigurationManager.AppSettings["FilaImportacao"].ToString(); } }

//private const string KEY_IMPORTACAO_PATH = "ImportacaoVendaPath";

public string FilePath { get { return ConfigurationManager.AppSettings["ImportacaoVendaPath"]; } }

public Guid SalvarArquivo(byte[] stream)

{

Guid id = System.Guid.NewGuid();

string filePath = String.Concat(this.FilePath, "/", id.ToString());

IFile dal = DependencyInjection.Resolve<IFile>();

dal.SaveFile(stream, filePath);

return id;

}

private byte[] ObterArquivo(Guid idFile)

{

IFile dal = DependencyInjection.Resolve<IFile>();

Stream file = dal.GetFileFromPath(String.Format("{0}/{1}", this.FilePath, idFile.ToString()));

byte[] conteudo = new byte[file.Length];

file.Read(conteudo, 0, (int)file.Length);

return conteudo;

}

public void InserirMensagem(Guid codigo)

{

IQueue<string> dal = DependencyInjection.Resolve<IQueue<string>>();

dal.AddMessage(KEY_QUEUE_IMPORTACAO, codigo.ToString());

}

public void RegistraImportacaoIniciar(ImportacaoInfo mensagem)

{

IQueue<ImportacaoInfo> dal = DependencyInjection.Resolve<IQueue<ImportacaoInfo>>();

dal.AddMessage(KEY_QUEUE_IMPORTACAO, mensagem);

}

public string ObtemProximaImportacao() //here the error happens

{

IQueue<string> dal = DependencyInjection.Resolve<IQueue<string>>();

return dal.GetMessage(KEY_QUEUE_IMPORTACAO);

}

public Guid Inserir(ImportacaoInfo importacao)

{

IImportacaoDal dal = DependencyInjection.Resolve<IImportacaoDal>();

return dal.Inserir(importacao);

}

public void Atualizar(ImportacaoInfo importacao)

{

IImportacaoDal dal = DependencyInjection.Resolve<IImportacaoDal>();

dal.Atualizar(importacao);

}

/// <summary>

/// Obtém uma importação do repositório com as planilhas preenchidas

/// </summary>

/// <param name="codigo">Código da importação</param>

public ImportacaoInfo ObtemImportacao(Guid codigo)

{

IImportacaoDal dal = DependencyInjection.Resolve<IImportacaoDal>();

/* Busca a importação no repositório */

ImportacaoInfo importacao = dal.ObterPorChave(codigo.ToString());

/* Preenche a planilha de importações */

importacao.ConteudoZ18 = this.ObterArquivo(importacao.GuidZ18);

importacao.ConteudoZ30 = this.ObterArquivo(importacao.GuidZ30);

return importacao;

}

/// <summary>

/// Obtém os últimos registros de importações, por cada empreendimento cadastrado.

/// </summary>

/// <returns>Lista com os últimos registros de importações para cada empreendimento</returns>

public IList<ImportacaoInfo> ObtemUltimosPorEmpreendimento()

{

/* Busca todos os empreendimentos cadastrados */

IEmpreendimentoDal empreendimentoDal = DependencyInjection.Resolve<IEmpreendimentoDal>();

IList<EmpreendimentoInfo> empreendimentos = empreendimentoDal.ListaEmpreendimento();

/* Obtém todos os últimos registros de importação, por cada empreendimento encontrado */

IImportacaoDal importacaoDal = DependencyInjection.Resolve<IImportacaoDal>();

IList<ImportacaoInfo> importacoes = new List<ImportacaoInfo>();

ImportacaoInfo impInfo = null;

foreach (EmpreendimentoInfo empreendimento in empreendimentos)

{

impInfo = importacaoDal.ObterUltimoPorEmpreendimento(empreendimento.Codigo);

if (impInfo != null)

{

impInfo.CodigoEmpreendimento = impInfo.Empreendimento;

impInfo.Empreendimento = string.Concat(impInfo.Empreendimento, " - ", empreendimento.Nome);

impInfo.NomeEmpreendimento = empreendimento.Nome;

importacoes.Add(impInfo);

}

}

return importacoes;

}

/// <summary>

/// Obtém os últimos registros de importações, pelos empreendimentos cada empreendimento cadastrado.

/// </summary>

/// <returns>Lista com os últimos registros de importações para cada empreendimento</returns>

public IList<ImportacaoInfo> ObtemUltimosPorEmpreendimento(List<string> empreendimentos)

{

/* Obtém todos os últimos registros de importação, por cada empreendimento encontrado */

IImportacaoDal importacaoDal = DependencyInjection.Resolve<IImportacaoDal>();

IEmpreendimentoDal empreendimentoDal = DependencyInjection.Resolve<IEmpreendimentoDal>();

IList<ImportacaoInfo> importacoes = new List<ImportacaoInfo>();

ImportacaoInfo impInfo = null;

foreach (string empreendimento in empreendimentos)

{

string empreendimentox = empreendimento.Replace("'", "");

impInfo = importacaoDal.ObterUltimoPorEmpreendimento(empreendimentox);

if (impInfo != null)

{

//obtém o empreendimento

EmpreendimentoInfo empreendimentoInfo = empreendimentoDal.Obter(empreendimentox);

if (empreendimentoInfo != null)

{

impInfo.CodigoEmpreendimento = impInfo.Empreendimento;

impInfo.Empreendimento = string.Concat(impInfo.Empreendimento, " - ", empreendimentoInfo.Nome);

impInfo.NomeEmpreendimento = empreendimentoInfo.Nome;

importacoes.Add(impInfo);

}

}

}

return importacoes;

}

}

Thanks for help.

Sérgio Rezende Júnior

MCOM Soluções e Tecnologia

(17) 3304-4440

De: AvanadeSupport [email removed]
Enviada em: segunda-feira, 25 de julho de 2011 23:57
Para: Sérgio Rezende Jr.
Assunto: Re: Error in Resolution of the dependency [entlib:266228]

From: AvanadeSupport

Your registration looks fine. Can you also post your QueueHelper and ApuracaoQueueInfo Class?

 
 

Noel Angelo Bolasoc
Global Technologies and Solutions
Avanade, Inc.
Contact Us

Jul 27, 2011 at 9:48 AM

Is it correct that the error is thrown on the GetMessage? If yes, what's the exact error are you getting?

 

Noel Angelo Bolasoc
Global Technologies and Solutions
Avanade, Inc.
Contact Us

Jul 27, 2011 at 12:47 PM

Hi.

Yes. The erro ris thrown on the GetMessage.

This Method is calling from ObtemProximIteracao after resolution dependency injection.

IQueue is the interface of the DLL Interfaces.dll which referenced with “using CommonLibraries.Interfaces;”

The error is:

Resolution of the dependency failed, type = "CommonLibraries.Interfaces.IQueue`1[System.String]", name = "". Exception message is: The current build operation (build key Build Key[CommonLibraries.Interfaces.IQueue`1[System.String], null]) failed: The current type, CommonLibraries.Interfaces.IQueue`1[System.String], is an interface and cannot be constructed. Are you missing a type mapping? (Strategy type BuildPlanStrategy, index 3)

Thanks for help.

Sérgio Rezende Júnior

MCOM Soluções e Tecnologia

(17) 3304-4440

De: AvanadeSupport [email removed]
Enviada em: quarta-feira, 27 de julho de 2011 05:49
Para: Sérgio Rezende Jr.
Assunto: Re: Error in Resolution of the dependency [entlib:266228]

From: AvanadeSupport

Is it correct that the error is thrown on the GetMessage? If yes, what's the exact error are you getting?

Noel Angelo Bolasoc
Global Technologies and Solutions
Avanade, Inc.
Contact Us

Jul 27, 2011 at 12:58 PM

Very importante.

I’m using DI on the Project for Windows Azure.

This GetMessage is resolved to get message in queue of the Windows Azure.

Sérgio Rezende Júnior

MCOM Soluções e Tecnologia

(17) 3304-4440

De: AvanadeSupport [email removed]
Enviada em: quarta-feira, 27 de julho de 2011 05:49
Para: Sérgio Rezende Jr.
Assunto: Re: Error in Resolution of the dependency [entlib:266228]

From: AvanadeSupport

Is it correct that the error is thrown on the GetMessage? If yes, what's the exact error are you getting?

Noel Angelo Bolasoc
Global Technologies and Solutions
Avanade, Inc.
Contact Us

Jul 28, 2011 at 11:50 AM

Hi,

Everything looks fine. We tried to reproduce your issue on our side but unfortunately, we didn't got any errors. I was just thinking this might have something to do with Azure but I'm not sure to that. If possible, can you send us a very simple sample repro project of your issue?

 

Noel Angelo Bolasoc
Global Technologies and Solutions
Avanade, Inc.
Contact Us

Aug 12, 2011 at 7:29 PM

Hi.

I sorry for the delay in returning an answer.

I tried any solution, but at the moment only the chance SDK Windows Azure from 1.4 to 1.2, in the words, a downgrade.

Thank you for your help.

Sérgio Rezende Júnior

MCOM Soluções e Tecnologia

(17) 3304-4440