3

I have the following Startup file (Startup.cs):

public void Configuration(IAppBuilder app)
{


    AutofacConfig.RegisterIoc();
    HttpConfiguration config = new HttpConfiguration();

    ConfigureOAuth(app); //This must come first before we load the WebApiConfig below.

    WebApiConfig.Register(config);
    app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
    app.UseWebApi(config);

}

As you can see, I register the autofac config. The code for my autofac config looks like this:(AutofacConfig.cs)

 public class AutofacConfig
    {
        public static void RegisterIoc()
        {
            var builder = GetBuilder();
            var container = builder.Build();

            // Set dependency resolver for MVC
            //DependencyResolver.SetResolver(new AutofacWebApiDependencyResolver(container));

            // Set dependency resolver for Web API
            GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container);
        }

        public static ContainerBuilder GetBuilder()
        {
            var builder = new ContainerBuilder();
            builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
            //builder.RegisterFilterProvider();


            var assemblies = BuildManager.GetReferencedAssemblies().Cast<Assembly>().Where(x => x.FullName.Contains("API")).ToArray();
            builder.RegisterAssemblyTypes(assemblies)
                .AsImplementedInterfaces()
                .InstancePerRequest();

            return builder;
        }
    }

But when I run my application and tries to access a method in my controller, I get the following error:

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

I have tried to debug my startup.cs either to see if it's executed, and it does.

Before I used to register autofac in Global.asax, but because I use OAuth, I moved away from Global.asax and created a Startup-file instead. I guess it has something to do with this?

My controller look like this:

private readonly IItemModelService _itemModelService;

public DataController(IItemModelService itemModelService)
{
    this._itemModelService = itemModelService;
}

Stacktrace:

at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType) at System.Web.Http.Controllers.HttpControllerDescriptor.CreateController(HttpRequestMessage request) at System.Web.Http.Dispatcher.HttpControllerDispatcher.d__15.MoveNext() at System.Linq.Expressions.Expression.New(Type type) at System.Web.Http.Internal.TypeActivator.Create[TBase](Type instanceType) at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage request, Type controllerType, Func`1& activator) at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)

9
  • It says you need a parameterless public constructor. You need public DataController() { ... }. Am I missing something? Commented Aug 27, 2018 at 9:22
  • 1
    You have to register service at container container.RegisterType<IItemModelService , ItemModelService >(); Commented Aug 27, 2018 at 9:25
  • Where are your IOC registrations? Commented Aug 27, 2018 at 9:25
  • @Div: No. Instead of doing that, Im just writing var assemblies = BuildManager.GetReferencedAssemblies().Cast<Assembly>().Where(x => x.FullName.Contains("API")).ToArray(); builder.RegisterAssemblyTypes(assemblies) .AsImplementedInterfaces() .InstancePerRequest(); Commented Aug 27, 2018 at 9:36
  • @Progressive: They are in my autfacconfig.cs Commented Aug 27, 2018 at 9:41

1 Answer 1

1

I'm using Oauth with OWIN, and I did not know that It was a special implementation of autofac for owin based application.

My startup.cs looks like this and it works:

public void Configuration(IAppBuilder app)
    {
        var builder = new ContainerBuilder();


        //AutofacConfig.RegisterIoc();
        HttpConfiguration config = new HttpConfiguration();

        builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
        var assemblies = BuildManager.GetReferencedAssemblies().Cast<Assembly>().Where(x => x.FullName.Contains("API")).ToArray();
        builder.RegisterAssemblyTypes(assemblies)
            .AsImplementedInterfaces()
            .InstancePerRequest();

        var container = builder.Build();
        config.DependencyResolver = new AutofacWebApiDependencyResolver(container);

        ConfigureOAuth(app); //This must come first before we load the WebApiConfig below.

        WebApiConfig.Register(config);
        app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
        app.UseAutofacMiddleware(container);
        app.UseAutofacWebApi(config);
        app.UseWebApi(config);

    }

I followed this links:

https://autofaccn.readthedocs.io/en/latest/integration/webapi.html#owin-integration

https://autofaccn.readthedocs.io/en/latest/integration/owin.html

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.