Adding Unity to Your Application
Unity is designed to support a range of common scenarios for resolving instances of objects that, themselves, depend on other objects or services. However, you must first prepare your application to use Unity. The following procedure describes how to include the necessary assemblies and elements in your code.
To prepare your application
Add a reference to the Unity assembly. In Microsoft Visual Studio, right-click your project node in Solution Explorer, and then click Manage NuGet Packages.
Click the Online button, and then in the Search Online box, type Unity.
Click the Install button for the Unity for .NET 4.5 and Windows Store Apps package. If you plan to use Unity interception, you should also add the Unity Interception Extension for .NET 4.5 package.
Read and accept the license terms for the packages listed.
After NuGet has finished installing the packages, click Close.
NuGet has now updated your project with all the necessary assemblies and references that you need to use Unity.
(Optional) If you intend to use the interception and policy injection features of Unity, use the same procedure to install the Unity Interception Extension NuGet package (search for Unity.Interception in the NuGet package manager).
(Optional) To use elements from Unity without fully qualifying the element reference, add the following using statements (C#) or Imports statements (Visual Basic) to the top of your source code file as required.
using Microsoft.Practices.Unity; using Microsoft.Practices.Unity.Configuration; using Microsoft.Practices.Unity.InterceptionExtension;
Imports Microsoft.Practices.Unity Imports Microsoft.Practices.Unity.Configuration Imports Microsoft.Practices.Unity.InterceptionExtension
(Optional) If you are using the IServiceLocator interface, use NuGet to add the CommonServiceLocator package to your project**.** Visual Studio may automatically copy this file to your bin directory when it compiles, but you do not need to include it unless you are explicitly using the UnityServiceLocatorAdapter class.
Add your application code. For more information about how you can use Unity in your own applications, see What Does Unity Do?
For Visual Basic projects, you can also use the References page of the Project Designer to manage references and imported namespaces. To access the References page, select a project node in Solution Explorer, and then click Properties on the Project menu. When the Project Designer appears, click the References tab.
Note
There are limitations when using Unity in a partial trust environment. For more information, see Using Unity in Partial Trust Environments.