Autofac - None of the constructors found with 'Public binding flags'
I keep on getting an error when trying to resolve my repositories.
None of the constructors found with 'Public binding flags' on type can be invoked with the available services and parameters: Cannot resolve parameter 'Repository.Data.INHibernateSession nHibernateSession' of constructor 'Void .ctor(Reposit开发者_StackOverflowory.Data.INHibernateSession)'.
Global.asax
builder.RegisterAssemblyTypes(assembly).Where(t => typeof(IDependency).IsAssignableFrom(t)).
AsImplementedInterfaces().InstancePerLifetimeScope();
builder.RegisterAssemblyTypes(assembly).Where(t => typeof(ISingletonDependency).IsAssignableFrom(t)).
AsImplementedInterfaces().SingleInstance();
builder.RegisterAssemblyTypes(assembly).Where(t => typeof(ITransientDependency).IsAssignableFrom(t)).
AsImplementedInterfaces().InstancePerDependency();
builder.RegisterGeneric(typeof(Repository<>)).As(typeof(IRepository<>)).InstancePerDependency();
then i have my singleton factory
public interface INhibernateFactory : ISingletonDependency
{
ISessionFactory SessionFactory { get; }
}
then my instance per lifetime
public interface INHibernateSession : IDependency
{
ISession Session { get; }
}
public class NHibernateSession : IDependency
{
private ISession _session;
private readonly ISessionFactory _sessionFactory;
public NHibernateSession(ISessionFactory sessionFactory)
{
_sessionFactory = sessionFactory;
}
then in my Generic repository
public class Repository<T> : IRepository<T> where T : class
{
private readonly INHibernateSession _nHibernateSession;
public Repository(INHibernateSession nHibernateSession)
{
_nHibernateSession = nHibernateSession;
}
public ISession Session
{
get { return _nHibernateSession.Session; }
}
It seems all i'm doing is creating a singleton, inject that to the session, then inject into repository. (all my other dependencies work fine)
Appreciate if someone could point me in the right direction why this wont resolve, I'm quite stumped?
The errors you get speaks for them selves, they state some type that cannot be resolved. After your latest change, this is the the 'ISessionFactory' interface which is missing.
To further elaborate: the current problem is that Autofac, when asked to build NHibernateSession
instances, doesn't know how to provide an ISessionFactory
instance. To "learn" the container how to build session factories it is not enough to provide the NHibernateFactory
instance alone. Consider this registration:
builder.RegisterType<NhibernateFactory>().As<INhibernateFactory>().SingleInstance();
builder.Register(c => c.Resolve<INhibernateFactory>().SessionFactory);
The last line there registers a lambda that knows how to provide the session factory instance.
You'll need to provide the container with every type involved. Use the "no constructors..." error to see which types are missing.
To dig further into Autofac and NHibernate you'll have to search around the net. E.g. this question discusses NHibernate sessions: Managing NHibernate ISession with Autofac
I am not familiar with AutoFac; but does your NHibernateSession class have to implement your INHibernateSession interface to resolve correctly?
Matt
精彩评论