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.
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 a service that is registered in my container as a single instance
public class MyModule : Module
{
protected override void Load(ContainerBuilder builder)
{
builder.RegisterType<MyService>()
.As<IMyService>()
.SingleInstance();
}
}
The container is created as below
public static IContainer SetupContainer()
{
var builder = new ContainerBuilder();
var moduleTypes = TypeCache.GetTypes(x => x.IsClassEx() && !x.IsAbstractEx() && x.ImplementsInterfaceEx<IModule>());
foreach (var moduleType in moduleTypes)
{
if (Activator.CreateInstance(moduleType) is IModule module)
builder.RegisterModule(module);
}
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
builder.RegisterAssemblyModules(assemblies);
var result = builder.Build();
return result;
}
This all works perfectly within normal code - I can inject my service and its resolved as I expect
However, when I try to inject my service into a web api controller, the service is again resolved, but Autofac gives me a NEW instance of my service
How can I prevent this behaviour so that the originally created instance is injected?
I think that what are you missing here is that you are not actually resolving your container in the right place and so depending on what type of integration you are using you could do one of the following.
//For OWIN you could do something like the following.
public class Startup
{
public void Configuration(IAppBuilder app)
{
var container = YourObject.SetupContainer();
// Register the Autofac middleware FIRST. This also adds
// Autofac-injected middleware registered with the container.
app.UseAutofacMiddleware(container);
// ...then register your other middleware not registered
// with Autofac.
}
}
//In your Global.asax.cs
protected void Application_Start()
{
var container = YourObject.SetupContainer();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
}
MVC Example
OWIN Implementation
I've been reading several posts but nothing seems to fit to my problem.
I'm developing a console app with .Net Core 2.1 and EF Core trying to follow Microsoft's advices but I'm facing with the next problem.
I've a project named myproject.data which contains all the interfaces and services. This one, for example
ILeagueService.cs
public interface ILeaguesService
{
List<Leagues> GetAllLeaguesValids();
}
LeagueService.cs
private statsContext _context;
public LeaguesService(statsContext context)
{
_context = context;
}
public List<Leagues> GetAllLeaguesValids()
{
return _context.Leagues.Where(x => (x.DateFirstMatch != null || x.CurrentLastSeason == true) && x.Active == true).OrderBy(x => x.Id).ToList();
}
Then, I have all the methods of my app separated and all of them inherit from the same class. In this Base.cs class I setup the ServiceProvider
Base.cs
public ServiceProvider _serviceProvider;
public Base()
{
ConfigureServices();
_config = new HelperConfig(CONFIG_FILE);
_html = GetHelperHtml();
_context = GetContext();
}
private void ConfigureServices()
{
_serviceProvider = new ServiceCollection()
.AddScoped<ILeaguesService, LeaguesService>()
.BuildServiceProvider();
}
When I try to use the LeagueService in one of the methods I get the 'Unable to resolve service for type myproject.stats.statsContext' error
GetNextMatches.cs
private ILeaguesService _leagueService;
public GetNextMatches()
{
_config.GetSection(AppsettingsModel.BetExplorerUrlsSection).Bind(betExplorerSectionKeys);
_leagueService = _serviceProvider.GetService<ILeaguesService>(); <-- In this line I get the error
}
When using the ServiceProvider DI you have to register all of the classes in the hierarchy. The DI container is trying to create your LeagueService class but to call its constructor it needs to create an instance of statsContext. However it cannot find that in its registry so it throws the exception.
The solution is to add statsContext to your services collection.
private void ConfigureServices()
{
_serviceProvider = new ServiceCollection()
.AddScoped<ILeaguesService, LeaguesService>()
.AddScoped<statsContext>()
.BuildServiceProvider();
}
I am going to assume your _context variable is the statsContext you want to inject so you can use your GetContext() method to create the context for you:
private void ConfigureServices()
{
_serviceProvider = new ServiceCollection()
.AddScoped<ILeaguesService, LeaguesService>()
.AddSingleton<statsContext>(GetContext())
.BuildServiceProvider();
}
This will call your GetContext() once to create your single instance of statsContext. Now, whenever you call
_leagueService = _serviceProvider.GetService<ILeaguesService>();
DI will inject the singleton instance of statsContext when it creates your LeageService class.
I am trying to inject the API dependency and MVC dependency. But when I try to inject the MVC Dependency I get the error:
The configuration is invalid. The following diagnostic warnings were
reported:
-[Lifestyle Mismatch] FeedbackDbRepository (Web Request) depends on ChatBotDbContext (Transient).
-[Disposable Transient Component] ChatBotDbContext is registered as transient, but implements IDisposable. See the Error property for
detailed information about the warnings. Please see
https://simpleinjector.org/diagnostics how to fix problems and how to
suppress individual warnings.
Which happens in the RegisterMvcDependencies() in here:
public static void RegisterWebApiDependencies()
{
//TODO: setup dependency injection for Web Api
var container = new Container();
container.Options.DefaultScopedLifestyle = new WebRequestLifestyle();
container.Register<IAnswerGenerator, PxlAnswerGenerator>(Lifestyle.Singleton);
container.Verify();
GlobalConfiguration.Configuration.DependencyResolver = new SimpleInjectorWebApiDependencyResolver(container);
}
public static void RegisterMvcDependencies()
{
//TODO: setup dependency injection for MVC
var container = new Container();
container.Options.DefaultScopedLifestyle = new WebRequestLifestyle();
container.Register<IFeedbackRepository, FeedbackDbRepository>(Lifestyle.Scoped);
container.RegisterMvcControllers(Assembly.GetExecutingAssembly());
container.Verify();
DependencyResolver.SetResolver(new SimpleInjectorDependencyResolver(container));
}
This is my application_start():
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
DependencyConfig.RegisterWebApiDependencies();
DependencyConfig.RegisterMvcDependencies();
}
It does work when I comment out RegisterMvcDependencies() how can I solve this?
EDIT ChatBotDbContext is registered here:
public class FeedbackDbRepository : IFeedbackRepository//TODO: implement IFeedbackRepository
{
private readonly ChatBotDbContext _context;
public FeedbackDbRepository(ChatBotDbContext context)
{
_context = context;
}
//Tip1: use async await
//Tip2: use SaveChangesAsync() instead of SaveChanges()
public async Task AddAsync(Feedback newFeedback)
{
_context.Feedbacks.Add(newFeedback);
await _context.SaveChangesAsync();
}
}
ChatBoxDBContext:
public class ChatBotDbContext : DbContext //TODO: inherit from some other class
{
public ChatBotDbContext()
: base("DefaultConnection")
{
}
public DbSet<Feedback> Feedbacks { get; set; }
public DbSet<User> Users { get; set; }
public static ChatBotDbContext Create()
{
return new ChatBotDbContext();
}
public static void SetInitializer()
{
//TODO: make sure Entity Framework creates the database if it does not exists and migrates an existing database to the latest version
Database.SetInitializer(new MigrateDatabaseToLatestVersion<ChatBotDbContext, Migrations.Configuration>());
}
}
Change it to be a scoped service. From the docs:
Warning: Transient instances are not tracked by the container. This means that Simple Injector will not dispose transient instances. Simple Injector will detect disposable instances that are registered as transient when calling container.Verify(). Please view Diagnostic Warning - Disposable Transient Components for more information.
You can read more on the error here: https://simpleinjector.readthedocs.io/en/latest/disposabletransientcomponent.html
This question already has an answer here:
How to inject webapi AccountController in WebApi
(1 answer)
Closed 5 years ago.
My controller is like :
public class AccountController : ApiController
{
private const string LocalLoginProvider = "Local";
private ApplicationUserManager _userManager;
private readonly ApplicationRoleManager _roleManager;
public AccountController()
{
}
public AccountController(ApplicationUserManager userManager,
ISecureDataFormat<AuthenticationTicket> accessTokenFormat,
ApplicationRoleManager roleManager)
{
UserManager = userManager;
AccessTokenFormat = accessTokenFormat;
_roleManager = roleManager;
}
}
In UnityConfig.cs I try to configure like this way :
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();
// TODO: Register your types here
container.RegisterType<AccountController>(new InjectionConstructor());
container.RegisterType<ApplicationUserManager>();
container.RegisterType<ApplicationRoleManager>();
container.RegisterType<ISecureDataFormat<AuthenticationTicket>, SecureDataFormat<AuthenticationTicket>>();
}
In WebApiConfig.cs :
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
var container = new UnityContainer();
UnityConfig.RegisterTypes(container);
//Set the unity container as the default dependency resolver
config.DependencyResolver = new UnityHierarchicalDependencyResolver(container);
}
}
And in Global.asax.cs :
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
GlobalConfiguration.Configure(WebApiConfig.Register); // At the beginning, register with HttpConfiguration
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
}
But when i try to retrieve role using roleManager (var role = await _roleManager.FindByIdAsync(model.RoleId);) it says :
Object reference not set to an instance of an object.
I see there is a similar question here, but it can't solve my problem.
Based on how the code in the question looks I believe you are using Unity bootstrapper for ASP.NET Web API which should wire up a UnityDependencyResolver.
container.RegisterType<AccountController>(new InjectionConstructor());
This registers the AccountController and instructs Unity to use the paramterless constructor. This is why all of your dependencies are null. If you want to use the other constructor remove the AccountController registration and Unity will use the constructor with the most parameters. However if you do this then you will get a runtime error attempting to resolve the AccountController because ISecureDataFormat<> is not registered and Unity will not know how to map that interface to a concrete type.
If you register a mapping to SecureDataFormat<> then there are some additional dependencies that will need to be registered.
container.RegisterType(typeof(ISecureDataFormat<>), typeof(SecureDataFormat<>));
container.RegisterType<ITextEncoder, Base64UrlTextEncoder>()
container.RegisterType<IDataSerializer<AuthenticationTicket>, TicketSerializer>()
container.RegisterType<IDataProtector>(new ContainerControlledLifetimeManager(),
new InjectionFactory(c => new DpapiDataProtectionProvider().Create("App Name")));
Note that the above registrations are not tested. Not sure if you should configure OWIN with data protection (and perhaps get the protection provider from the OWIN config).
The following two registrations are not strictly required since Unity knows how to resolve a concrete type without a registration and no additional InjectionMembers are being provided (e.g. lifetime, parameters overrides etc.).
container.RegisterType<ApplicationUserManager>();
container.RegisterType<ApplicationRoleManager>();