2

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(Repository.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?

Dr.
  • 167
  • 3
  • 11
  • is `ISessionFactory` registered in autofac? – Eranga Jul 17 '11 at 15:42
  • I tried adding `builder.RegisterType(typeof(ISessionFactory)).InstancePerLifetimeScope();` but it still gets the same error – Dr. Jul 17 '11 at 16:07
  • but you need to register the concrete class that implement `ISessionFactory` – Eranga Jul 17 '11 at 16:15
  • I changed it to `builder.RegisterInstance(new NhibernateFactory()).As(typeof(INhibernateFactory)).SingleInstance();` but I still get one of the constructors found with 'Public binding flags' on type 'Repository.Data.NHibernateSession' can be invoked with the available services and parameters: Cannot resolve parameter 'NHibernate.ISessionFactory sessionFactory' of constructor 'Void .ctor(NHibernate.ISessionFactory)'. – Dr. Jul 17 '11 at 16:31

2 Answers2

3

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

Community
  • 1
  • 1
Peter Lillevold
  • 33,668
  • 7
  • 97
  • 131
  • Ok, Thanks, I'm confused though. I've implemented the concrete type via `builder.RegisterInstance(new NhibernateFactory()).As(typeof(INhibernateFactory)).SingleInstance();` but I still get **No constructors on type 'NHibernate.ISessionFactory' can be found with 'Public binding flags'.** How do I resolve a type thats part of nHibernate. – Dr. Jul 17 '11 at 17:18
  • @Dr. - did you figure it out or is there anything you think needs elaboration? – Peter Lillevold Jul 25 '11 at 21:54
0

I am not familiar with AutoFac; but does your NHibernateSession class have to implement your INHibernateSession interface to resolve correctly?

Matt

Matt Griffiths
  • 1,142
  • 8
  • 26