AndrasCsanyi
AndrasCsanyi

Reputation: 4255

Why this webapi 2 setup with Autofac does not work?

I have been cracking my head for hours why the setup below does not work. It throws 404.

What I did so far:

What did I miss? What else should I check?

How it works, or what I want to work. WebApi controller gets only the businesslogic as parameter, Autofac should deal with it, and BusinessLogic library gets other tree component (Mapper, SoapClient, Repository). It should work, or at least I have a similar setup in my other application, but that one has Owin setup too.

public class WebApiApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            var builder = new ContainerBuilder();

            var config = GlobalConfiguration.Configuration;

            builder.RegisterApiControllers(Assembly.GetExecutingAssembly());

            builder.RegisterWebApiFilterProvider(config);


            builder.RegisterType<Repository.Repository.Repository>()
                .As<IRepository.IRepository.IRepository>();

            builder.RegisterType<BusinessLogic.BusinessLogic.BusinessLogic.BusinessLogic>()
                .As<IBusinessLogic>();

            builder.RegisterType<SoapClient>()
                .As<ISoapClient>();

            builder.RegisterType<Mapper.Mapper.Mapper.Mapper>()
                .As<IMapper>();

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

            //GlobalConfiguration.Configure(WebApiConfig.Register);
        }
    }

public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services

            // Web API routes
            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );

            var appXmlType = config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(t => t.MediaType == "application/xml");
            config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType);
        }
    }

[RoutePrefix("PipelineWorkflow")]
    public class PipelineWorkflowController : ApiController
    {
        #region Private variables
        private IBusinessLogic _businessLogic;

        #endregion

        #region Constructors
        public PipelineWorkflowController(IBusinessLogic businessLogic)
        {
            _businessLogic = businessLogic;
        }

        #endregion
        [HttpGet]
        [Route("GetPipelineWorkflows")]
        public string GetPipelineWorkflows()
        {
            return "asd";
        }

    }

Installed Nuget packages

PM> Get-Package -project GoNoGo.Services.webapi | Format-Table -Autosize

Id                              Versions ProjectName           
--                              -------- -----------           
Autofac                         {3.5.2}  GoNoGo.Services.WebApi
Autofac.WebApi2                 {3.4.0}  GoNoGo.Services.WebApi
EntityFramework                 {6.1.3}  GoNoGo.Services.WebApi
Microsoft.AspNet.WebApi         {5.2.3}  GoNoGo.Services.WebApi
Microsoft.AspNet.WebApi.Client  {5.2.3}  GoNoGo.Services.WebApi
Microsoft.AspNet.WebApi.Core    {5.2.3}  GoNoGo.Services.WebApi
Microsoft.AspNet.WebApi.WebHost {5.2.3}  GoNoGo.Services.WebApi
Newtonsoft.Json                 {8.0.2}  GoNoGo.Services.WebApi

Thanks for any help in advance!

Update:

Upvotes: 1

Views: 961

Answers (1)

Luca Ghersi
Luca Ghersi

Reputation: 3321

What about uncommenting this?

//GlobalConfiguration.Configure(WebApiConfig.Register);

Without that section there is no route activation, neither classical neither attribute routing, so I guess a 404 is a right answer. I doubt the problem is Autofac; btw, container issues almost everytime resolves as 500, not 404.

Hope it helps :)

Upvotes: 0

Related Questions