Answered by:
Unity.AspNet.Webapi v5.11.1 global.asax question

Question
-
User-1104215994 posted
Hi,
I am trying to use Unity.AspNet.Web API v5.11.1 as IOC container. Couldn't figure it out if I should add something about unity on Application Start.
Here is my Unity config:
public static void RegisterTypes(IUnityContainer container) { // NOTE: To load from web.config uncomment the line below. // Make sure to add a Unity.Configuration to the using statements. // container.LoadConfiguration(); // TODO: Register your type's mappings here. // container.RegisterType<IProductRepository, ProductRepository>(); container.RegisterType<IGameServices, GameServices>().RegisterType<UnitOfWork>(new Unity.Lifetime.HierarchicalLifetimeManager()); }
Thursday, June 27, 2019 8:06 AM
Answers
-
User71929859 posted
Couldn't figure it out if I should add something about unity on Application Start.No you don't have to specify anything specifically. When you install Unity, you should get a class called UnityWebApiActivator. It should be responsible for registering unity.
If you check that class, you should see the line below
[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(WebApi.UnityWebApiActivator), nameof(WebApi.UnityWebApiActivator.Start))]
That is basically saying fire the Start method in UnityWebApiActivator class when Application Starts.
Registering your mappings under RegisterTypes method should be enough to get you going.
What's happening? I don't understand what's the problem here.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Sunday, June 30, 2019 5:22 AM
All replies
-
User1120430333 posted
Are you not following some article on how to implement Unity in an ASP.NET WebAPI solution?
Thursday, June 27, 2019 9:20 AM -
User-1104215994 posted
Are you not following some article on how to implement Unity in an ASP.NET WebAPI solution?seems like <g class="gr_ gr_6 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins replaceWithoutSep" id="6" data-gr-id="6">following</g>:
https://blog.nicolaayan.com/2017/12/dependency-injection-with-unity-in-an-asp-net-application/
Thursday, June 27, 2019 11:03 AM -
User1120430333 posted
My usage if Unity in ASP.NET WebAPI using VB.NET.
Global.asax.vb
Imports System.IO Imports System.Web.Http Imports System.Web.Optimization Public Class WebApiApplication Inherits System.Web.HttpApplication Sub Application_Start() AreaRegistration.RegisterAllAreas() GlobalConfiguration.Configure(AddressOf WebApiConfig.Register) FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters) RouteConfig.RegisterRoutes(RouteTable.Routes) BundleConfig.RegisterBundles(BundleTable.Bundles) log4net.Config.XmlConfigurator.Configure(new FileInfo(Server.MapPath("~/Web.config"))) End Sub End Class
Static class in App_Start folder
Imports System.Data.Entity Imports System.Web.Http Imports Unity Imports DAL Imports Unity.Lifetime Public Module WebApiConfig Public Sub Register(ByVal config As HttpConfiguration) ' Web API configuration and services dim container = new UnityContainer() container.RegisterType(Of IDaoProject, DaoProject) () container.RegisterType(Of IDaoTask, DaoTask) () container.RegisterType(Of IDaoCache, DaoCache) () container.RegisterType(Of DbContext, ProjectManagementEntities)(new HierarchicalLifetimeManager()) config.DependencyResolver = new UnityResolver(container) ' Web API routes config.MapHttpAttributeRoutes() config.Routes.MapHttpRoute( name:="DefaultApi", routeTemplate:="api/{controller}/{action}/{id}", defaults:=New With {.controller = "Home", .action = "Index", .id = UrlParameter.Optional} ) End Sub End Module
Thursday, June 27, 2019 7:50 PM -
User-1104215994 posted
Please show the unity config.
Thursday, June 27, 2019 8:10 PM -
User1120430333 posted
The Unity config is the WebApiConfig that is being used globally and has the Unity container in the Register static class that is doing the Unity registrations. Anything in the Modules.vb in VB.NET is the means to make a class static. You could remove the route code out of the Register class and rename the WebApiConfig.vb to UnityConfig.vb.
Thursday, June 27, 2019 8:59 PM -
User71929859 posted
Couldn't figure it out if I should add something about unity on Application Start.No you don't have to specify anything specifically. When you install Unity, you should get a class called UnityWebApiActivator. It should be responsible for registering unity.
If you check that class, you should see the line below
[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(WebApi.UnityWebApiActivator), nameof(WebApi.UnityWebApiActivator.Start))]
That is basically saying fire the Start method in UnityWebApiActivator class when Application Starts.
Registering your mappings under RegisterTypes method should be enough to get you going.
What's happening? I don't understand what's the problem here.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Sunday, June 30, 2019 5:22 AM -
User-1104215994 posted
thank you for the info Ruchira.
Sunday, June 30, 2019 8:11 AM