Updating old nuget packages can sometimes be a problem. I took me a while to update the Unity package.

Here I’m talking about the dependency injection package.

The project I had to do this update on was using version 2.1.505 from 2011. We’re in version 5.11.

So, there are a lot of breaking changes between the two versions.

Here are the important points for the update between these two versions.

Updating the package

We start with the update of the Unity package in the Nuget packages. The operation is not complex in itself, it’s a simple package update.

Troubleshooting

Namespace

With this new version we move from the namespace Microsoft.Practices.Unity to Unity.

Change rather simple.

I would also like to take the opportunity to remove any references that might remain in the assemblies.

  • Microsoft.Practices.Unity
  • Microsoft.Practices.Unity.Configuration

The type ‘IUnityContainer’ is defined in an assembly that is not referenced.

The update may sometimes not work properly and the updated Unity package will need to be re-imported.

The type or namespace name ‘UnityConfigurationSection’ could not be found

With the new version of Unity many packages have been created to dispatch the features.

For UnityConfigurationSection, if you use it, you’ll need to add the package [Unity.Configuration] (https://www.nuget.org/packages/Unity.Configuration/) to projects in need.

The namespace Microsoft.Practices.Unity.Configuration remains unchanged.

The type or namespace name ‘UnityServiceLocator’ could not be found

For UnityServiceLocator, you need to add the package [Unity.ServiceLocation] (https://www.nuget.org/packages/Unity.ServiceLocation/).

You’ll need to replace the namespace used from Microsoft.Practices.ServiceLocation to Unity.ServiceLocation.

The type or namespace name ‘IServiceLocator’ could not be found

No additional packages if you’ve added the previous one: Unity.ServiceLocation.

The package you need is part of its dependencies: CommonServiceLocator.

UnityDependencyResolver

In the project I updated there is a use of the class UnityDependencyResolver. You can find this class in the Unity.Mvc package.

Its namespace: Unity.AspNet.Mvc.

Configuration

The Unity configuration in the *.config files also need to be changed.

If you have the message below:

An unhandled exception of type ‘System.Configuration.ConfigurationErrorsException’ occurred in System.Configuration.dll
Additional information: An error occurred creating the configuration section handler for unity: Could not load file or assembly ‘Microsoft.Practices.Unity.Configuration’ or one of its dependencies. The system cannot find the file specified.

No worries the UnityConfigurationSection class is no longer in Microsoft.Practices.Unity.Configuration but in Unity.Configuration.

Example of Web.config for Unity 2:

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

Example of Web.config for Unity 5:

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

The configuration of the interface with its concrete class is also evolving.

Unity 2 :

<unity>
  <typeAliases>
    <typeAlias alias="ILogger" type="Microsoft.Extensions.Logging.ILogger, Microsoft.Extensions.Logging.Abstractions" />
    <typeAlias alias="Logger" type="Sample.Logger, Sample" />
  </typeAliases>
  <containers>
    <container name="RepositoryContainer">
      <types>
        <type type="ILogger" mapTo="Logger" />
      </types>
    </container>
  </containers>
</unity>

Unity 5 :

<unity>

  <alias alias="ILogger" type="Microsoft.Extensions.Logging.ILogger, Microsoft.Extensions.Logging.Abstractions" />
  <alias alias="Logger" type="Sample.Logger, Sample" />

  <containers>
    <container name="RepositoryContainer">

      <register type="ILogger" mapTo="Logger" />

    </container>
  </containers>
</unity>

Conclusion

Lots of things to do to update to Unity 5. It took me several hours to find all the missing information. Changing the configuration was the most complicated to find.

Thanks to Sandeepkumar Gupta and his article Configure Unity Container from config file that allowed me to finalize my migration.