UPDATED WITH LATEST RESULTS AND QUESTION
I've converted everything to pipeline and integrated Simple Injector. As I mentioned, I've never used SI before, and I'm having trouble with the controller using EntityService. I can't seem to get the SI configuration for that controller to work. I get the following error: "Make sure the controller has a parameterless public constructor. All of the other registrations are working fine with SI. I debug and see those being populated.
Here is the controller:
namespace ProjectA.Controllers
{
[EnableCors(origins: "*", headers: "*", methods: "*")]
[ServicesController]
public class ProductServiceController : EntityService<Product>
{
public ProductServiceController(IRepository<Product> repository) : base(repository)
{
}
}
}
And here is the registration with the working ones:
public void Process(PipelineArgs args)
{
// Create the container
var container = Container.CreateContainer();
container.Options.DefaultScopedLifestyle = new WebRequestLifestyle();
var assemblies = AppDomain.CurrentDomain.GetAssemblies().Where(a => a.FullName.StartsWith("ProjectA.") || a.FullName.StartsWith("ProjectB."));
container.RegisterWebApiControllers(GlobalConfiguration.Configuration);
container.RegisterMvcControllers(assemblies.ToArray());
// Register Mvc filter providers
container.RegisterMvcIntegratedFilterProvider();
//these all work
container.Register<IProductsReader, ProductsReader>();
container.Register<IProductsUpdater, ProductsUpdater>();
container.Register<IProductsCreator, ProductsCreator>();
container.Register<IProductMapper, ProductMapper>();
container.Register<IFieldUpdater, FieldUpdater>();
container.Register(typeof(IRepository<Product>), typeof(ProductRepository));
//tried this for EntityService Controller, but no good
//container.Register<IRepository<Product>>(() => new ProductRepository(container.GetInstance<IProductsReader>(), container.GetInstance<IProductsUpdater>(), container.GetInstance<IProductsCreator>()));
container.Verify();
GlobalConfiguration.Configuration.DependencyResolver = new SimpleInjectorWebApiDependencyResolver(container);
// Set the ASP.NET dependency resolver
DependencyResolver.SetResolver(new SimpleInjectorDependencyResolver(container));
}
I've used a lot of Ninject in the past, but was hoping to get SI working here. I'm sure it will end up being a missing registration line ;) Thanks for the help!
ORIGINAL QUESTION
Hoping someone can help. I am using EntityService and have implemented Simple Injector. I know Simple Injector defaults to allow a single constructor and that is the recommended approach. With that in place, I'm getting the following error:
"An error occurred when trying to create a controller of type 'ProductServiceController'. Make sure that the controller has a parameterless public constructor."
Should I add a default constructor to the ProductRepository and if so, what do I add to global.asax for Simple Injector? Below is my related code.
Sample from Global.asax
var container = new Container();
container.Options.DefaultScopedLifestyle = new WebRequestLifestyle();
// Register your types
container.Register<IProductsReader, ProductsReader>();
container.Register<IProductsUpdater, ProductsUpdater>();
container.Register<IProductsCreator, ProductsCreator>();
container.Register<IProductMapper, ProductMapper>();
container.Register<IFieldUpdater, FieldUpdater>();
container.Register(typeof(IRepository<Product>), typeof(ProductRepository));
container.RegisterMvcControllers(Assembly.GetExecutingAssembly());
container.RegisterMvcIntegratedFilterProvider();
container.Verify();
DependencyResolver.SetResolver(new SimpleInjectorDependencyResolver(container));
Controller
[EnableCors(origins: "*", headers: "*", methods: "*")]
[ServicesController]
public class ProductServiceController : EntityService<Product>
{
public ProductServiceController(IRepository<Product> repository) : base(repository)
{
}
}
Sample from Repository
public class ProductRepository : Sitecore.Services.Core.IRepository<Product>
{
private IProductsReader productsReader;
private IProductsUpdater productsUpdater;
private IProductsCreator productsCreator;
public ProductRepository(IProductsReader productsReader, IProductsUpdater productsUpdater, IProductsCreator productsCreator)
{
this.productsReader = productsReader;
this.productsUpdater = productsUpdater;
this.productsCreator = productsCreator;
}
Thanks! Dan