Unity Container in SharePoint Service Locator

Oct 28, 2010 at 1:46 PM

Hi,

I'm trying to use Unity Container to replace ActivatingServiceLocator's container. I have created a new service locator factory (SharePointUnityServiceLocatorFactory) and service locator (SharePointUnityServiceLocator). To test if it works, I then modified code in SharePointServiceLocator's GetServiceLocatorFactory method and returned my service locator factory (though this is not the best place but I tried to make it work first). Then I tried to register type mappings in feature receiver. However when I try to resolve the IServiceLocatorConfig it generates error saying

Resolution of the dependency failed, type = "Microsoft.Practices.SharePoint.Common.ServiceLocation.IServiceLocatorConfig", name = "(none)".
Exception occurred while: while resolving.
Exception is: InvalidOperationException - The current type, Microsoft.Practices.SharePoint.Common.Configuration.IPropertyBagHierarchy, is an interface and cannot be constructed. Are you missing a type mapping?
-----------------------------------------------
At the time of the exception, the container was:

  Resolving Microsoft.Practices.SharePoint.Common.ServiceLocation.ServiceLocatorConfig,(none) (mapped from Microsoft.Practices.SharePoint.Common.ServiceLocation.IServiceLocatorConfig, (none))
  Resolving parameter "manager" of constructor Microsoft.Practices.SharePoint.Common.ServiceLocation.ServiceLocatorConfig(Microsoft.Practices.SharePoint.Common.Configuration.IConfigManager manager)
    Resolving Microsoft.Practices.SharePoint.Common.Configuration.ConfigManager,(none) (mapped from Microsoft.Practices.SharePoint.Common.Configuration.IConfigManager, (none))
    Resolving parameter "hierarchy" of constructor Microsoft.Practices.SharePoint.Common.Configuration.ConfigManager(Microsoft.Practices.SharePoint.Common.Configuration.IPropertyBagHierarchy hierarchy, Microsoft.Practices.SharePoint.Common.Configuration.IConfigSettingSerializer configSettingSerializer)
      Resolving Microsoft.Practices.SharePoint.Common.Configuration.IPropertyBagHierarchy,(none)

 

I have checked the types are registered in container (inside SharePointUnityServiceLocator) and I can resolve the types registered by me. It seems the IPropertyBagHierarchy can't be resolved. Is there any known issue using Unity in SharePointSeriveLocator or am I missing something? Is there any help in incorporating Unity with SharePoint guidance?

Thanks

Sohel Rana

 

Nov 1, 2010 at 12:54 PM

Hi,

I''m not sure exactly why unity is trying to resolve that constructor rather than using the default constructor, and I have not tried to replace the service locator with unity.  There are two different ways to construct HierarchicalConfig which is used by the service locator config.  One way to construct it is to pass in the hierarchy, and we use this for unit testing.  However there is also  a default constructor for ConfigManager.  When that constructor is used, ConfigManger will internally manage construction of the IPropertyBagHeirarchy.  This is the way that we construct when in the SharePoint environment.  The PropertyBagHierarchy needs to be built by HierarchyBuilder, which takes a look at the current SharePoint context to determine which property bag hierarchy is appropriate.  Unity should not construct this iinstance since it doesn't know how to interpret the SharePoint context.

Chris