Created
January 4, 2014 17:36
-
-
Save ielcoro/8257862 to your computer and use it in GitHub Desktop.
MVC and Web Api Unity Integration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(mvc.App_Start.UnityWebActivator), "Start")] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DependencyResolver.SetResolver(new UnityDependencyResolver(container)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(mvc.App_Start.UnityWebActivator), "Start")] | |
namespace mvc.App_Start | |
{ | |
/// <summary>Provides the bootstrapping for integrating Unity with ASP.NET MVC.</summary> | |
public static class UnityWebActivator | |
{ | |
/// <summary>Integrates Unity when the application starts.</summary> | |
public static void Start() | |
{ | |
var container = UnityConfig.GetConfiguredContainer(); | |
//FilterProviders.Providers.Remove(FilterProviders.Providers.OfType<FilterAttributeFilterProvider>().First()); | |
//FilterProviders.Providers.Add(new UnityFilterAttributeFilterProvider(container)); | |
DependencyResolver.SetResolver(new UnityDependencyResolver(container)); | |
// TODO: Uncomment if you want to use PerRequestLifetimeManager | |
//Microsoft.Web.Infrastructure.DynamicModuleHelper.DynamicModuleUtility.RegisterModule(typeof(UnityPerRequestHttpModule)); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void RegisterTypes(IUnityContainer container) | |
{ | |
// NOTE: To load from web.config uncomment the line below. Make sure to add a Microsoft.Practices.Unity.Configuration to the using statements. | |
// container.LoadConfiguration(); | |
container.RegisterType<IUnitOfWork, UnitOfWork>(new PerRequestLifetimeManager()); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// TODO: Uncomment if you want to use PerRequestLifetimeManager | |
Microsoft.Web.Infrastructure.DynamicModuleHelper.DynamicModuleUtility.RegisterModule(typeof(UnityPerRequestHttpModule)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Use UnityHierarchicalDependencyResolver if you want to use a new child container for each IHttpController resolution. | |
var resolver = new UnityHierarchicalDependencyResolver(UnityConfig.GetConfiguredContainer()); | |
//var resolver = new UnityDependencyResolver(UnityConfig.GetConfiguredContainer()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void RegisterTypes(IUnityContainer container) | |
{ | |
// NOTE: To load from web.config uncomment the line below. Make sure to add a Microsoft.Practices.Unity.Configuration to the using statements. | |
// container.LoadConfiguration(); | |
container.RegisterType<IUnitOfWork, UnitOfWork>(new HierarchicalLifeTimeManager()); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary>Provides the bootstrapping for integrating Unity with WebApi when it is hosted in ASP.NET</summary> | |
public static class UnityWebApiActivator | |
{ | |
/// <summary>Integrates Unity when the application starts.</summary> | |
public static void Start() | |
{ | |
// Use UnityHierarchicalDependencyResolver if you want to use a new child container for each IHttpController resolution. | |
//var resolver = new UnityHierarchicalDependencyResolver(UnityConfig.GetConfiguredContainer()); | |
var resolver = new UnityDependencyResolver(UnityConfig.GetConfiguredContainer()); | |
GlobalConfiguration.Configuration.DependencyResolver = resolver; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment