What does AsSelf do in autofac? [duplicate]

Typically you would want to inject interfaces, rather than implementations into your classes. But let’s assume you have: interface IFooService { } class FooService { } Registering builder.RegisterType<FooService>() allows you to inject FooService, but you can’t inject IFooService, even if FooService implements it. This is equivalent to builder.RegisterType<FooService>().AsSelf(). Registering builder.RegisterType<FooService>().As<IFooService>() allows you to inject IFooService, … Read more

SignalR + Autofac + OWIN: Why doesn’t GlobalHost.ConnectionManager.GetHubContext work?

If you use a custom dependency resolver with SignalR, you can no longer use GlobalHost unless you modify it: GlobalHost.DependencyResolver = new AutofacDependencyResolver(container); IHubContext hubContext = GlobalHost.ConnectionManager.GetHubContext<MyHub>(); // A custom HubConfiguration is now unnecessary, since MapSignalR will // use the resolver from GlobalHost by default. app.MapSignalR(); If you don’t want to modify GlobalHost, you will … Read more

.NET 4, AllowPartiallyTrustedCallers attribute, and security markings like SecurityCritical

You are correct: in .NET 4, leaving the APTCA on there makes the assembly SecurityTransparent, and that may be what’s causing you grief. The MSDN article Migrating an APTCA Assembly to the .NET Framework 4 has a good discussion and explanation of the changes to the AllowPartiallyTrustedCallersAttribute in .NET 4. Specifically: The AllowPartiallyTrustedCallers attribute has … Read more

How do I properly register AutoFac in a basic MVC5.1 website?

Here’s what I have – feedback welcome! Global.asax.cs: public class MvcApplication : System.Web.HttpApplication { protected void Application_Start() { // Register Inversion of Control dependencies IoCConfig.RegisterDependencies(); // Typical MVC setup // …. } } App_Start folder: public class IoCConfig { /// <summary> /// For more info see /// :https://code.google.com/p/autofac/wiki/MvcIntegration (mvc4 instructions) /// </summary> public static void … Read more

Register global filters in ASP.Net MVC 4 and Autofac

There’s a new way of registering MVC global filters in AutoFac. First, remove the filter registration from your RegisterGlobalFilters because we will have Autofac handle adding them to our controllers/actions instead of MVC. Then, register your container as follows: var builder = new ContainerBuilder(); builder.RegisterControllers(Assembly.GetExecutingAssembly()); builder.RegisterType<MyProperty>().As<IProperty>(); builder.Register(c => new CustomFilterAttribute(c.Resolve<IProperty>())) .AsActionFilterFor<Controller>().InstancePerHttpRequest(); builder.RegisterFilterProvider(); IContainer container = … Read more

How do I resolve Web API controllers using Autofac in a mixed Web API and MVC application?

I would suggest the following to make this work in your application with both MVC and WebApi. First your project will need to have references to the following Autofac Autofac.WebApi Autofac.Mvc5 (change the number at the end to match your aspnet mvc version) Then in your Autofac registration you would need the following which will … Read more

Autofac and Func factories

You are calling secVMFactory outside of your FirstViewModel constructor so by that time the ResolveOperation is disposed and in your factory method the c.Resolve will throw the exception. Luckily the exception message is very descriptive and telling you what to do: When registering components using lambdas, the IComponentContext ‘c’ parameter to the lambda cannot be … Read more

None of the constructors found with ‘Autofac.Core.Activators.Reflection.DefaultConstructorFinder’

You have regiestered your EmailService two times. Once in the web.config and once with builder.RegisterType<EmailService>().As<IEmailService>().SingleInstance(); If you have the line above in the Core.ModuleInstaller then it will override the web.config configuration. And because here you haven’t specified the parameter Autofac throws an exception. So to solve this just remove the EmailService registration from the Core.ModuleInstaller … Read more

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