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
RegisterTypeto map an open generic type, but there is no easy way to do batch registration of generic types in Unity. - Steven