Reputation:
I am receiving this error message from Autofac;:
None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'MyService`1[MyContext]' can be invoked with the available services and parameters: Cannot resolve parameter 'MyContext context' of constructor 'Void .ctor(MyContext)'.
The error occurs on this line of code (also shown in the code below):
IMyService myService = container.Resolve<IMyService>(); // error here
I am interested to note that when I include this line in my registrations, everything works:
builder.RegisterSource(new AnyConcreteTypeNotAlreadyRegisteredSource());
The fact that it all works when I register AnyConcreteType... leads me to believe I am not not registering something. My question is what am I not registering? The error message appears to name MyContext as the culprit but clearly I am registering it as shown below.
I really dont want to use the AnyConcreteType... catch all because I want to explicity register only the the classes I need.
My service is constructed like this:
public class MyService<T> : BaseService<T>, IMyService where T:DbContext,IMyContext
{
public MyService(T context) : base(context)
{
}
}
MyService derives from BaseService:
public abstract class BaseService<T> : IDisposable where T:DbContext, IMyContext
{
internal T db;
public BaseService(T context)
{
db = context;
}
}
MyContext is passed to MyService and is constructed like this:
public partial class MyContext : DbContext, IMyContext
{
public MyContext(INamedConnectionString conn)
: base(conn.ConnectionString)
{
Configuration.ProxyCreationEnabled = false;
}
}
Here is NamedConnectionString
public class NamedConnectionString : INamedConnectionString
{
public string ConnectionString { get; set; }
}
Here is how I register the above:
builder.RegisterType<MyService<MyContext>>().As<IMyService>();
builder.RegisterType<NamedConnectionString>().As<INamedConnectionString>().SingleInstance();
builder.RegisterType<MyContext>().As<IMyContext>().InstancePerLifetimeScope();
builder.RegisterType<DbContext>(); // is this necessary??
And here is how I call it:
var container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
INamedConnectionString namedConnectionString = container.Resolve<INamedConnectionString>();
namedConnectionString.ConnectionString = myConnectionString;
IMyService myService = container.Resolve<IMyService>(); // error here
Upvotes: 5
Views: 2832
Reputation: 742
Got this working by using builder.RegisterType<InventoryContext>().InstancePerLifetimeScope();
.
Context:
I have a DBContext class, I have services that injects the Context and then have Controllers to call the Services (Interface).
And then on global.asax, called the builder.RegisterType<InventoryContext>().InstancePerLifetimeScope();
. This worked well, been a while since I worked with an MVC project again.
If there's anyone having issues with this you can refer to this again.
Upvotes: 0
Reputation: 3207
This worked for me
builder.RegisterType().As().InstancePerLifetimeScope();
Upvotes: 1
Reputation:
Autofac and ASP .Net MVC 4 Web API
The above thread is related. It does not answer the question but it helped me start troubleshooting in the right direction. The solution is here:
I removed these two lines:
builder.RegisterType<MyContext>().As<IMyContext>().InstancePerLifetimeScope();
builder.RegisterType<DbContext>(); // is this necessary??
and replaced them with this one:
builder.RegisterType<MyContext>().InstancePerLifetimeScope();
Upvotes: 7