문제

I just run this code in a quickly made console app with autofac assembly added:

builder.RegisterType<DbConnection>().As<IDbConnection>().WithParameter("connectionString", connectionString);

There was no exception although DbConnection is of abstract type.

Does autofac really create a concrete instance of DbConnection although its abstract?

How is that possible not to use:

builder.RegisterType().As().WithParameter("connectionString", connectionString);

Do I have to inherit from DbConnection and inject then MySqlConnection inherited from DbConnection?

도움이 되었습니까?

해결책

You would only get the exception on resolution. You do need to register concrete types since you can't instantiate abstract types. So, as you said, you'd need a non-abstract type with a public constructor derived from DbConnection and register that as IDbConnection.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top