Note that there are some explanatory texts on larger screens.

plurals
  1. POUnity injection into asp.net web api controllers and filters
    primarykey
    data
    text
    <p>I have WebForms project, and there I have WebApi controller. How I can inject to controller constructor and to action filter constructor?</p> <p>I have implemented <code>IDependencyResolver</code> and use it in <code>Global.asax</code> (<code>GlobalConfiguration.Configuration.DependencyResolver</code>), but it doesn't help: on request controller says that there is no default constructor and filter on application start says that it does not contain a constructor that takes 0 arguments.</p> <p>Moreover, i need singletone injection to action filter.</p> <p>Thanks.</p> <p><strong>UPD</strong></p> <p><code> </p> <pre><code>public class ScopeContainer : IDependencyScope { protected readonly IUnityContainer _container; public ScopeContainer(IUnityContainer container) { _container = container; } public object GetService(Type serviceType) { return _container.IsRegistered(serviceType) ? _container.Resolve(serviceType) : null; } public IEnumerable&lt;object&gt; GetServices(Type serviceType) { return _container.IsRegistered(serviceType) ? _container.ResolveAll(serviceType) : new List&lt;object&gt;(); } public void Dispose() { _container.Dispose(); } } public class IoCContainer : ScopeContainer, IDependencyResolver { public IoCContainer(IUnityContainer container) : base(container) { } public IDependencyScope BeginScope() { var child = _container.CreateChildContainer(); return new ScopeContainer(child); } } public class Global : HttpApplication { void Application_Start(object sender, EventArgs e) { RegisterRoutes(); ConfigureApi(GlobalConfiguration.Configuration); } void ConfigureApi(HttpConfiguration config) { var unity = new UnityContainer(); unity.RegisterInstance&lt;ILogger&gt;(new Logger()); unity.RegisterType&lt;IRepository, DbRepository&gt;(); config.DependencyResolver = new IoCContainer(unity); } private static void RegisterRoutes() { RouteTable.Routes.MapHttpRoute("ServiceApi", "api/{controller}/{action}"); } } </code></pre> <p></code></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload