dror
dror

Reputation: 3946

Structuremap No parameterless constructor defined for this object

I'm a newbie to StructureMap, and I've been trying to fix that error for some time now. Just can't figure out how to fix it and where am I doing wrong. I even set up a template MVC4 site, with nothing in it and still getting that error.

Can someone please help me out ?

public static class IoC {
    public static IContainer Initialize() {
        ObjectFactory.Initialize(x =>
                    {
                        x.Scan(scan =>
                                {
                                    scan.TheCallingAssembly();
                                    scan.WithDefaultConventions();
                                });

                        x.For<IDbSession>().Use(() => MvcApplication.DbSession);
                        x.For<IDbService>().Use<DbService>();
                    });
        return ObjectFactory.Container;
    }
}

public class HomeController : Controller
{
    protected readonly IDbService _dbService;

    public HomeController(IDbService dbService)
    {
        _dbService = dbService;
    }

    ...
}

public interface IDbSession : IDisposable
{
    void Commit();
    void Rollback();
}

public interface IDbService
{
    StudentsService Students { get; }
    CoursesService Courses { get; }
    ...
}

public class DbService : IDbService
{
    private readonly IDbSession _dbSession;        

    public StudentsService Students { get; }
    public CoursesService Courses { get; }
    ...

    public DbService(IDbSession dbSession)
    {
        _dbSession = dbSession;
    }
}

public class MvcApplication : System.Web.HttpApplication
    {
        private static readonly string _connectionString;
        private static readonly IDbSessionFactory _dbSessionFactory;

        public static IDbSession DbSession
        {
            get { return (IDbSession)HttpContext.Current.Items["Current.DbSession"]; }
            private set { HttpContext.Current.Items["Current.DbSession"] = value; }
        }

        static MvcApplication()
        {
            _connectionString= ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            _dbSessionFactory = new DbSessionFactory(_connectionString);
        }

        protected MvcApplication()
        {
            BeginRequest += delegate
            {
                DbSession = _dbSessionFactory.Create();
            };

            EndRequest += delegate
            {
                if (DbSession != null)
                    DbSession.Dispose();
            };
        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            AuthConfig.RegisterAuth();
        }
    }

Upvotes: 4

Views: 7562

Answers (1)

Jeroen K
Jeroen K

Reputation: 10438

You must configure the dependency resolver to handle parameters in a Controller's constructor. You can find out here how: http://ardalis.com/How-Do-I-Use-StructureMap-with-ASP.NET-MVC-3

Upvotes: 5

Related Questions