I have an API controller that fires a service that uses constructor dependency injection. I would like to use the Windsor container to inject those dependencies, but I'm not sure the best practice.
Here's part of the API controller factory:
private readonly IWindsorContainer _container;
private readonly ServiceFactory _serviceFactory;
...
public IHttpController Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)
{
Arguments args = new Arguments(new { serviceFactory = _serviceFactory });
IHttpController controller = (IHttpController)_container.Resolve(controllerType, args);
request.RegisterForDispose(new Release(() => _container.Release(controller)));
return controller;
}
...
Here's part of the Service Factory:
private readonly IWindsorContainer _container;
...
public IService Create(Type serviceType, ApiRequestModel apiRequest)
{
Arguments args = new Arguments(new { request = apiRequest });
return (IService)_container.Resolve(serviceType, args);
}
...
Here's part of the API controller:
private ServiceFactory _serviceFactory { get; set; }
...
public object Post([FromBody]ApiRequestModel request)
{
...
Type serviceType = Assembly.Load("TestProject").GetType("TestClass");
IService instance = _serviceFactory.Create(serviceType, request);
...
_serviceFactory.Release(instance);
}
...
The Service Factory contains an instance of the Windsor container, so is it a bad idea to expose it to the API controller? If so, what is the best alternative? Thanks.
My solution was to use a generic typed factory facility (e.g. http://thesenilecoder.blogspot.com/2012/04/ioc-windsor-typedfactoryfacility-and.html)
The factory only consists of the following interface:
public interface IServiceFactory
{
T Create<T>(ApiRequestModel request) where T : IService;
void Release(IService service);
}
And here's how it gets installed to the container:
container.AddFacility<TypedFactoryFacility>();
container.Register(
Component.For<IServiceFactory>().AsFactory(),
Classes.FromThisAssembly().BasedOn<IService>().LifestylePerWebRequest());
That gets injected into the API controller and allows me to create generic types. I used reflection to resolve the generic type before I went to the factory.
Type serviceType = Assembly.Load("TestProject").GetType("TestClass");
MethodInfo factoryMethod = _serviceFactory.GetType().GetMethod("Create").MakeGenericMethod(serviceType);
IService instance = (IService)factoryMethod.Invoke(_serviceFactory, new object[] { request });
Related
I am trying to inject my logger as dependency in a .Net Framework 4.7.2 web api project by following these instructions:
https://scottdorman.blog/2016/03/17/integrating-asp-net-core-dependency-injection-in-mvc-4/
This works great for MVC web application but fails on the webapi project with the "parameterless constructor missing" error.
How do I successfully inject using just the default assembly: Microsoft.Extensions.DependencyInjection in framework?
public class Startup
{
public void Configuration(IAppBuilder app)
{
var services = new ServiceCollection();
ConfigureServices(services);
var resolver = new DefaultDependencyResolver(services.BuildServiceProvider());
DependencyResolver.SetResolver(resolver);
}
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersAsServices(typeof(Startup).Assembly.GetExportedTypes()
.Where(t => !t.IsAbstract && !t.IsGenericTypeDefinition)
.Where(t => typeof(IController).IsAssignableFrom(t)
|| t.Name.EndsWith("Controller", StringComparison.OrdinalIgnoreCase)));
services.AddSingleton<IMyInterface , MyClass>();
}
}
public class DefaultDependencyResolver : IDependencyResolver
{
protected IServiceProvider serviceProvider;
public DefaultDependencyResolver(IServiceProvider serviceProvider)
{
this.serviceProvider = serviceProvider;
}
public object GetService(Type serviceType)
{
return this.serviceProvider.GetService(serviceType);
}
public IEnumerable<object> GetServices(Type serviceType)
{
return this.serviceProvider.GetServices(serviceType);
}
}
public static class ServiceProviderExtensions
{
public static IServiceCollection AddControllersAsServices(this IServiceCollection services,
IEnumerable<Type> controllerTypes)
{
foreach (var type in controllerTypes)
{
services.AddTransient(type);
}
return services;
}
}
Getting "Parameterless constructor is missing" error with an injection like this:
private IMyInterface _my;
public HomeController(IMyInterface my)
{
_my= my;
}
Registration Explanation
One issue is that you are registering your DependencyResolver with the MVC resolver registration API which unfortunately is different from the WebAPI resolver registration API. What you want to do instead is:
public void Configuration(IAppBuilder app)
{
var services = new ServiceCollection();
ConfigureServices(services);
var resolver = new DefaultDependencyResolver(services.BuildServiceProvider());
HttpConfiguration config = new HttpConfiguration();
config.DependencyResolver = resolver;
app.UseWebApi(config);
}
Also note that the IDependencyResolver interface is defined separately in System.Web.Http, so your DefaultDependencyResolver class needs to be updated to derive from it instead.
One thing that has changed on that interface is that there is a BeginScope API. To implement that, call the CreateScope Extension Method exposed on IServiceProvider in Microsoft.Extensions.DependencyInjection to get a new scope. Then pass the provider from that scope to a new instance of your DefaultDependencyResolver.
public IDependencyScope BeginScope()
{
return new DefaultDependencyResolver(this.serviceProvider.CreateScope().ServiceProvider);
}
Full Example
The blog example you were following for MVC was using OWIN. When I set out to make a full example, I hit the same error as you, and it was because I hadn't correctly configured OWIN so Startup was never being called. Here is a full working example of Microsoft.Extensions.DependencyInjection being used to inject into both MVC and WebAPI Controllers:
https://github.com/ryandle/MVC_WebApi_DI_Example
Are you using [Serializable] on your HomeController? If so, when using it you need a constructor without parameters.
Try add this: public HomeController() { } and run again.
More info: parameter less constructor error
I have the following asp.net c# code
{
public class Test
{
ISomeService _someService;
public Test()
{
}
public void DoSomething()
{
_someService.Do();
}
}
I need to provide ISomeService to Test class, and I dont know how to do it. I am not allowed to add additional construction which would make entire problem go away, for example
public Test(ISomeService someService)
{
_someService = someService;
}
I tried using setter injection or method injection but that didnt do the trick.
Implementation of ISomeService in SomeService class also uses constructor injection, such as
public SomeService(IService1 service1, Iservice2 service2)
Not sure what to do here.
HERE IS A COMPLETE CODE
public class Startup
{
private IService _service;
public Startup()
{
}
public Startup(IService service)
{
_service = service;
}
public void Configuration(IAppBuilder app)
{
HttpConfiguration config = new HttpConfiguration();
var container = new UnityContainer();
container.RegisterType<IService, Service>();
config.DependencyResolver = new UnityDependencyResolver(container);
app.UseWebApi(config);
_service.DoSomething());
}
}
_service is null
I would suggest you use a factory to create your object. That would have an instance of ISomeService injected on the constructor.
Then in a CreateTest() method on your factory set the ISomeService property directly.
public class Factory
{
private readonly ISomeService someService;
public Factory(ISomeService someService)
{
this.someService = someService ?? throw new ArgumentNullException(nameof(someService));
}
public TestClass CreateTestClass()
{
var instance = new TestClass();
instance.SomeService = this.someService;
return instance;
}
}
You should note that most DI providers have built in functionality to allow factory semantics without the need to create your own factories.
What I ended up doing is this
var serviceCollection = new ServiceCollection();
serviceCollection.AddTransient<IService, Service>();
// create service provider
var serviceProvider = serviceCollection.BuildServiceProvider();
_service = ActivatorUtilities.CreateInstance<Service>(serviceProvider);
_service.DoSomething();
Thanks to this answer Dependency Injection with classes other than a Controller class
I am using ASP.Net Core Dependency Injection in an MVC App (not .Net Core app, but classic ASP.NET MVC Applicatio) I am using DI by adding the Microsoft.Extensions.DependencyInjection Nuget package. I am trying to create scoped life time for my controllers so I have a new scope whenever I create my controllers but I am getting the same instance always for my requests and there is an error as below
"A single instance of controller 'X.Controllers.HomeController' cannot be used to handle multiple requests. If a custom controller factory is in use, make sure that it creates a new instance of the controller for each request"
I have used a custom factory to create my controllers
and used new scope to create the controllers .
and the scope is disposed in the ReleaseController method
public class MyServiceFactory : DefaultControllerFactory
{
private readonly IServiceContainer _dependencyManager;
public MyServiceFactory (IServiceContainer dependencyManager)
{
this._dependencyManager = dependencyManager;
}
public override void ReleaseController(IController controller)
{
_dependencyManager.Release(((ServiceEndPoint)controller).Context.RuntimeContext.Scope);
}
protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
{
if (controllerType == null)
{
throw new HttpException(404, string.Format("The controller for path '{0}' could not be found.", requestContext.HttpContext.Request.Path));
}
var scope = _dependencyManager.GetNewScope();
var service=(ServiceEndPoint)_dependencyManager.Resolve(scope, controllerType);
service.Context.RuntimeContext.SetScope(scope);
return service;
}
}
The ServiceEndpoint is just a base class derived from Controller and I am using it as the base for all my controllers which contains some common logic.
I am setting a Context for my controllers which also contain the newly created scope and I am disposing my scope in Releasecontroller by getting it from the Context.
_dependencyManager.GetNewScope() create a New scope as below
return _container.CreateScope();
where _container is an Instance of IServiceProvider
The code _dependencyManager.Resolve(scope, type) is as below
public object Resolve(IServiceScope scope,Type type)
{
return scope.ServiceProvider.GetService(type);
}
You are doing something wrong, but as you hid the use of the Microsoft.Extensions.DependencyInjection (MS.DI) container behind your own abstraction, it is impossible to see what is going on.
However, the following is an example of a working sample application that integrates ASP.NET MVC with MS.DI.
MS.DI-specific controller factory:
public class MsDiControllerFactory : DefaultControllerFactory
{
private readonly ServiceProvider container;
public MsDiControllerFactory(ServiceProvider container) => this.container = container;
protected override IController GetControllerInstance(RequestContext c, Type type) =>
(IController)this.GetScope().ServiceProvider.GetRequiredService(type);
public override void ReleaseController(IController c) => this.GetScope().Dispose();
private IServiceScope GetScope() =>
(IServiceScope)HttpContext.Current.Items["scope"] ??
(IServiceScope)(HttpContext.Current.Items["scope"] = this.container.CreateScope());
}
MVC application configuring the container and replacing the default controller factory:
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
// Default MVC stuff
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
// create container builder to register dependencies in
var services = new ServiceCollection();
// register controller in the controller
services.AddScoped<HomeController>();
// Build the container while ensuring scopes are validated
ServiceProvider container = services.BuildServiceProvider(true);
// Replace default controller factory
ControllerBuilder.Current.SetControllerFactory(
new MsDiControllerFactory(container));
}
}
When you apply the above code to an MVC application created using the default MVC template for Visual Studio, you'll get a working MVC application that uses MS.DI as its application container.
(MVC 5, .NET Framework 4.8, not .NET Core or ASP.NET Core)
I was able to get Singleton / Scoped / Transient service lifecycles by creating an HttpContext-bound scope from within the dependency resolver, and did not need to modify the default controller factory:
private void ConfigureDependencyInjection(IAppBuilder app)
{
var services = new ServiceCollection();
AddControllersAsServices(services);
var provider = services.BuildServiceProvider();
var resolver = new DefaultDependencyResolver(provider);
DependencyResolver.SetResolver(resolver);
}
private void AddControllersAsServices(IServiceCollection services)
{
var controllers = typeof(Startup).Assembly.GetExportedTypes()
.Where(t => !t.IsAbstract && !t.IsGenericTypeDefinition && typeof(IController).IsAssignableFrom(t));
foreach(var controller in controllers)
{
services.AddTransient(controller);
}
}
class DefaultDependencyResolver : IDependencyResolver
{
private readonly IServiceProvider serviceProvider;
public DefaultDependencyResolver(IServiceProvider serviceProvider)
{
this.serviceProvider = serviceProvider;
}
public object GetService(Type serviceType)
{
var scope = this.GetHttpContextScope();
if (scope == null)
{
return this.serviceProvider.GetService(serviceType);
}
else
{
return scope.ServiceProvider.GetService(serviceType);
}
}
public IEnumerable<object> GetServices(Type serviceType)
{
return this.serviceProvider.GetServices(serviceType);
}
private IServiceScope GetHttpContextScope()
{
var context = HttpContext.Current;
if (context == null)
{
return null;
}
var scope = context.Items[this] as IServiceScope;
if (scope != null)
{
return scope;
}
scope = this.serviceProvider.CreateScope();
context.Items[this] = scope;
context.AddOnRequestCompleted(_ => scope.Dispose());
return scope;
}
}
The code works as follows: if a service is requested and there is a current HttpContext check if there is an IServiceScope associated with it, otherwise create a new Scope instance and bind it to the context. When the request is completed, dispose of the scope instance.
If there is no HttpContext simply resolve the service from the root ServiceProvider instance. I am not sure what that means for scoped services, but I assume they will behave like singletons in that case.
I'm injecting one service to my controller and I'm resolving the same service manually, but this two services are not the same. Manually created service is disposing but auto resolved service is not disposing. Let me show by code.
Here is controller, there is a IFirmService auto resolved and I have an other IFirmService in test function manually resolved. Here occurs the problem, there is two IFirmService resolved by unity but they are not the same.
public class TestmeController : ApiController
{
IFirmService FirmServiceHeader;
public TestmeController(IFirmService _FirmServiceHeader)
{
//here is successfully resolved FirmService by unity resolver.
this.FirmServiceHeader = _FirmServiceHeader;
}
[Route("api/test")]
public IHttpActionResult test() {
//Here is I'm resolving an other service manually.
IFirmService FirmServiceLocal = this.Configuration.DependencyResolver.GetService(typeof(IFirmService)) as IFirmService;
if (Object.ReferenceEquals(FirmServiceLocal,FirmServiceHeader)) {
//Here is the problem, FirmServiceLocal and firmServiceHeader is not resolved as the same
}
}
}
WebApiConfig.cs
I'm setting the default DependencyResolver as Unity and setting registered types.
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
.....
UnityContainr container = new UnityContainer();
container.RegisterType<IFirmService, FirmService>(new HierarchicalLifetimeManager());
//setting the dependency resolver.
config.DependencyResolver = new UnityResolver(container);
.....
}
}
I'm not writing here IFirmService and FirmService class, it is a disposable class.
Summary; I want to resolve the same Firmservice in test() function. And I want to it automaticly disposed after each web api request.
What is the best way for this?
if you want same object, you should get from same container(UnityContainer).
msdn says about HierarchicalLifetimeManager:
"The distinction is that when there are child containers, each child resolves its own instance of the object and does not share one with the parent. When resolving in the parent, the behavior is like a container controlled lifetime; when resolving the parent and the child you have different instances with each acting as a container-controlled lifetime. If you have multiple children, each will resolve its own instance."
Configuration.DependencyResolvers container is root container. but controller constructere resolvers container is child container of root container.
IService _service;
IUnityContainer _container;
public DefaultController(IService service,IUnityContainer container)
{
_service = service;
_container = container;
}
so.. you can get same object from injected container..
IService _service;
IUnityContainer _container;
public DefaultController(IService service,IUnityContainer container)
{
_service = service;
_container = container;
}
[HttpGet]
public async Task<ResponseObject> Test()
{
var localService = _container.Resolve<IService>();
if (Object.ReferenceEquals(_service, localService))
Debug.WriteLine("Equal");
else
Debug.WriteLine("Not Equal");
}
I am using Castle Windsor 3.0 and it worked perfectly for me until I tried to register controllers (I used WCF facility and IoC for repository/service layer). Here is my controllers installer class:
public class ControllersInstaller : IWindsorInstaller
{
public void Install(IWindsorContainer container, IConfigurationStore store)
{
RegisterAllBasedOnWithCustomComponentRegistration(container, typeof(IController),
typeof(HomeController).Assembly,
cr => cr.LifeStyle.Transient);
}
private void RegisterAllBasedOnWithCustomComponentRegistration(IWindsorContainer container, Type baseType,
Assembly assemblyWithImplementations, Func<ComponentRegistration, ComponentRegistration<object>> customComponentRegistrationCb)
{
container.Register(
AllTypes.FromAssembly(assemblyWithImplementations)
.BasedOn(baseType)
.If(t => t.Name.EndsWith("Controller"))
.Configure(c => customComponentRegistrationCb(c)));
}
}
And here is my controller factory:
public class WindsorControllerFactory : DefaultControllerFactory
{
private readonly IKernel _kernel;
public WindsorControllerFactory(IKernel kernel)
{
_kernel = kernel;
}
public override void ReleaseController(IController controller)
{
_kernel.ReleaseComponent(controller);
}
public override IController CreateController(System.Web.Routing.RequestContext requestContext, string controllerName)
{
var controllerComponentName = controllerName + "Controller";
return _kernel.Resolve<IController>(controllerComponentName);
}
}
From my global.asax I call the next method:
InversionOfControl.InstallControllers(FromAssembly.This());
which lies in an another project. And in there I do call the installation code:
public static void InstallControllers(IWindsorInstaller install)
{
_container.Install(install);
}
it seems like I am doing something wrong and I hope I am because it could be a "never use awny beta again" moment for me.
I get the next exception : No component for supporting the service System.Web.Mvc.IController was found altough I can see the controller in the debug mode being registered in the container
In your ControllerFactory, you don't shouldn't Resolve IController but rather the concrete controller type. Here's a typical Windsor-base ControllerFactory I always use:
public class WindsorControllerFactory : DefaultControllerFactory
{
private readonly IWindsorContainer _container;
public WindsorControllerFactory(IWindsorContainer container)
{
_container = container;
}
protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
{
return (IController)_container.Resolve(controllerType);
}
public override void ReleaseController(IController controller)
{
_container.Release(controller);
}
}
In this case add .WithServices(typeof(IController)) and name all components.
cr => cr.LifeStyle.Transient.Named(cr.Implementation.Name)
and your registration should look like:
.Register(
AllTypes.FromAssembly(assemblyWithImplementations)
.BasedOn(baseType)
.WithServices(typeof(IController))
.If(t => t.Name.EndsWith("Controller"))...)