Resolve IContainer

You’ll need IComponentContext or ILifetimeScope for most purposes. These are automatically provided by Autofac, so all you need to do is take a (constructor) dependency on one of them and the current instance will be injected. Both interfaces provide the standard Resolve() operations, while ILifetimeScope extends IComponentContext by adding methods for starting new nested lifetimes. … Read more

Autofac: Resolve all instances of a Type

For current versons of Autofac: ( 2.0+, so anything you should be using today) You register multiple ILoggers (for example): var builder = new ContainerBuilder(); builder.Register<ConsoleLogger>() .As<ILogger>(); builder.Register<EmailLogger>() .As<ILogger>() .PreserveExistingDefaults(); //keeps console logger as the default Then get all ILoggers: var loggers = container.Resolve<IEnumerable<ILogger>>(); You don’t need to do anything special, just ask for an … Read more

How to set ViewBag properties for all Views without using a base class for Controllers?

The best way is using the ActionFilterAttribute. I’ll show you how to use it in .Net Core and .Net Framework. .Net Core 2.1 & 3.1 public class ViewBagActionFilter : ActionFilterAttribute { public ViewBagActionFilter(IOptions<Settings> settings){ //DI will inject what you need here } public override void OnResultExecuting(ResultExecutingContext context) { // for razor pages if (context.Controller is … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)