Frans Verhoeven
Frans Verhoeven

Reputation: 1

unity xml configuration generic interfaces

I'm using Unity for constructor injection. Constructor injection via the runtime API succeeds with the following code:

{
using ContractImplementations;
using Contracts;
using DataAccess;
using DataModel.Entities;
using DataModel.Interfaces;

using Microsoft.Practices.Unity;

using Unity.Wcf;

/// <summary>
/// The wcf service factory.
/// </summary>
public class WcfServiceFactory : UnityServiceHostFactory
{
    #region Methods
    /// <summary>
    /// Configure container.
    /// </summary>
    /// <param name="container">
    /// The container.
    /// </param>
    protected override void ConfigureContainer(IUnityContainer container)
    {
        container
           .RegisterType<IGaugeModelbaseService, GaugeModelbaseService>()
           .RegisterType<IContractMapper, ContractMapper>(new HierarchicalLifetimeManager())
           .RegisterType<IGenericRepository<GaugeModel>, GenericSqlRepository<GaugeModel>>(new HierarchicalLifetimeManager());
    }

    #endregion
}

}

Because of integration with AppFabric and EntLib however I have to configure the container in XML config. Problem: The unity documentation is not clear on the subject of registering generic types. According to the documentation, I have to do something like this:

<?xml version="1.0" encoding="utf-8"?>

<namespace name="Design.ModelbaseSvc" />
<assembly name="Design.ModelbaseSvc" />
<namespace name="Design.ContractImplementations" />
<assembly name="Design.ContractImplementations" />
<namespace name="Design.DataModel" />
<assembly name="Design.DataModel" />
<namespace name="Design.DataAccess" />
<assembly name="Design.DataAcces" />


<container>
  <register type="IGaugeModelbaseService" mapTo="GaugeModelbaseService">
    <interceptor type="InterfaceInterceptor" />
  </register>
  <register type="IContractMapper" mapTo="ContractMapper">
    <lifetime type="hierarchical" />
  </register>
  <register type="IGenericRepository'1[Design.DataModel.Entities.GaugeModel, Design.DataModel]" mapTo="GenericSqlRepository'1[Design.DataModel.Entities.GaugeModel, Design.DataModel]">       
    <lifetime type="hierarchical" />
  </register>
</container>

I don't understand what I'm doing wrong, but this doesn't work: - the XML-editor gives errors on using parenthesis "[]" - Browsing the svc-file give the folowing error:

The type name or alias IGenericRepository'1[Design.DataModel.Entities.GaugeModel, Design.DataModel] could not be resolved. Please check your configuration file and verify this type name.

I tried several other and it finally leads to brain death. Please help.

Thanks

Frans Verhoeven

Upvotes: 0

Views: 2159

Answers (1)

onof
onof

Reputation: 17367

For each type you must specify the assembly and the full namespace, or you can use aliases.

With generic types, if you don't use aliases you have to use double square brackets:

<register 
      type="MyNameSpace.IGenericRepository'1[[Design.DataModel.Entities.GaugeModel, Design.DataModel]], MyAssembly"       
     mapTo="MyNameSpace.GenericSqlRepository'1[[Design.DataModel.Entities.GaugeModel, Design.DataModel]], MyAssembly">       
    <lifetime type="hierarchical" />
</register>

Upvotes: 1

Related Questions