How to configure fluent nHibernate with MySQL

Change MsSqlConfiguration.MsSql2005, to MySqlConfiguration.Standard, it was the one thing I contributed to the project. Example: Fluently.Configure().Database( MySqlConfiguration.Standard.ConnectionString( c => c.FromConnectionStringWithKey(“ConnectionString”) ) ) .Mappings(m => m.FluentMappings.AddFromAssemblyOf<MyAutofacModule>()) .BuildSessionFactory())

The requested service has not been registered ! AutoFac Dependency Injection

With the statement: builder.RegisterType<ProductService>().As<IProductService>(); Told Autofac whenever somebody tries to resolve an IProductService give them an ProductService So you need to resolve the IProductService and to the ProductService: using (var container = builder.Build()) { container.Resolve<IProductService>().DoSomething(); } Or if you want to keep the Resolve<ProductService> register it with AsSelf: builder.RegisterType<ProductService>().AsSelf();

Register partically closed generic type with Autofac

You cannot have partially opened classes (e.g. with UnitOfWork<Repository<>,> you have specified T but not O) inside a typeof, try it with: var builder = new ContainerBuilder(); builder .RegisterGeneric(typeof(UnitOfWork<,>)) .As(typeof(IUnitOfWork)) .InstancePerDependency(); The where T : Repository<O> generic constraint will take care of that the first argument should be an Repository<> But it won’t work with … Read more

How to deal with run-time parameters when using lifetime scoping?

Autofac now supports this out of the box with an extension to the lifetime scopes. The BeginLifetimeScope() method has an overload that takes an Action<ContainerBuilder> that allows for adding new registrations specific to only that lifetime scope. So for the given example it would look something like: var builder = new ContainerBuilder(); builder.RegisterType<MyService>().As<IMyService>().InstancePerLifetimeScope(); var container … Read more

AutoFac / .NET Core – Register DBcontext

I use Autofac to register both HttpContextAccessor and DbContext. builder .RegisterType<HttpContextAccessor>() .As<IHttpContextAccessor>() .SingleInstance(); builder .RegisterType<AppDbContext>() .WithParameter(“options”, DbContextOptionsFactory.Get()) .InstancePerLifetimeScope(); DbContextOptionsFactory public class DbContextOptionsFactory { public static DbContextOptions<AppDbContext> Get() { var configuration = AppConfigurations.Get( WebContentDirectoryFinder.CalculateContentRootFolder()); var builder = new DbContextOptionsBuilder<AppDbContext>(); DbContextConfigurer.Configure( builder, configuration.GetConnectionString( AppConsts.ConnectionStringName)); return builder.Options; } } DbContextConfigurer public class DbContextConfigurer { public static void Configure( … Read more

Is Dependency Injection possible with a WPF application?

It’s actually very easy to do. We have examples of this in Prism as jedidja mentioned. You can either have the ViewModel get injected with the View or the View get injected with the ViewModel. In the Prism StockTraderRI, you will see that we inject the View into the ViewModel. Essentially, what happens is that … Read more

Autofac with multiple implementations of the same interface

Autofac implicitly supports this by default via the use of IEnumerable<T>. Instead of having your depending class’s constructor take in a single instance of T, you make it take in an instance of IEnumerable<T> that will contain every T registered: public interface IMessageHandler { void HandleMessage(Message m); } public class MessageProcessor { private IEnumerable<IMessageHandler> _handlers; … Read more

Resolving Generic Interface with Autofac

Autofac supports open generics. You can use the following code if generics type is known at compile time: var builder = new ContainerBuilder(); builder.RegisterGeneric(typeof(SomeInstance1<>)) .As(typeof(IGenericInterface<>)); var container = builder.Build(); var instance1 = container.Resolve<IGenericInterface<SubClass1>>(); Assert.IsInstanceOfType(typeof(SomeInstance1<SubClass1>), instance1); If type parameter is not known until runtime (which is likely your case if you want to iterate through collection … Read more

Return same instance for multiple interfaces

builder.RegisterType<StandardConsole>() .As<IStartable>() .As<IConsumer<ConsoleCommand>>() .SingleInstance(); Very widely used feature of Autofac- any problems then there is a bug somewhere 🙂 Hth Nick Edit By the looks of it, you’re after the overload of As() that takes an IEnumerable<Type>() – check out all of the As() overloads using IntelliSense, something there should fit your scenario. As another … Read more

Adding Autofac to .NET core 6.0 using the new single file template

I found this Microsoft docs var builder = WebApplication.CreateBuilder(args); builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory()); // Register services directly with Autofac here. // Don’t call builder.Populate(), that happens in AutofacServiceProviderFactory. builder.Host.ConfigureContainer<ContainerBuilder>( builder => builder.RegisterModule(new MyApplicationModule())); var app = builder.Build();

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