0

I setup dryIoc with MVC because it's an mvc application but i recently just added an API controller to the project. I get error when i call the API controller. The MVC controllers are working fine

An error occurred when trying to create a controller of type 'xxxController'. Make sure that the controller has a parameterless public constructor."

2 Answers2

0

This error is not from DryIoc but from the ASP.NET, which tries to instantiate the controller after DryIoc has failed.

To find why DryIoc is failed, add the optional parameter to container.WithMvc() method (if you're using DryIoc.Mvc extension):

var mvcContainer = container.WithMvc(
    throwIfUnresolved: type => true); // throws DryIoc exc. for any unresolved type

Hope that actual exception will help with problem fix.

dadhi
  • 4,807
  • 19
  • 25
  • I am getting this error now "Unable to resolve IControllerFactory Where no service registrations found and no dynamic registrations found in 0 Rules.DynamicServiceProviders and nothing in 0 Rules.UnknownServiceResolvers" – Kemi Awosile Apr 15 '18 at 12:33
0

This is what i found out. In my StartUp.cs, i have below code

public partial class Startup
{
    public void Configuration(IAppBuilder app)
    {

            var config = new HttpConfiguration();

            IContainer container = new Container(rules => rules.WithTrackingDisposableTransients());

            RegisterServices(container);

            container = container.WithMvc();

            ConfigureAuth(app);
    }

    public static void RegisterServices(IRegistrator registrator)
    {

        registrator.Register(typeof(IxxxRepository), typeof(xxxRepository), Reuse.Singleton);

    }
}

In my WebApiConfig.cs

            var c = new Container()
            .WithWebApi(config, throwIfUnresolved: type => type.IsController());
        c.Register(typeof(IxxxRepository), typeof(xxxRepository), Reuse.Singleton);

Both controllers (MVC and API) are working fine now. But i am not sure if this is the right way.Because now i will need to register repositories for each instance of both containers.