简体   繁体   中英

autofac not working for WebAPI 2 on azure website

I'm trying to setup my web api on azure, Everything is working file locally but when i deploy to azure I get the following error message :

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

The website is a normal azure website, not a worker rule.

This is my Autofac bootstraper class:

var builder = new ContainerBuilder();
        builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
        builder.RegisterType<UnitOfWork>().As<IUnitOfWork>().InstancePerRequest();
        builder.RegisterType<DatabaseFactory>().As<IDatabaseFactory>().InstancePerRequest();
        builder.RegisterAssemblyTypes(typeof(DestinationRepository).Assembly).Where(t => t.Name.EndsWith("Repository")).AsImplementedInterfaces().InstancePerRequest();
        builder.RegisterAssemblyTypes(typeof(DestinationService).Assembly).Where(t => t.Name.EndsWith("Service")).AsImplementedInterfaces().InstancePerRequest();

        builder.RegisterWebApiFilterProvider(GlobalConfiguration.Configuration);
        IContainer container = builder.Build();

        // Create the depenedency resolver.
        var resolver = new AutofacWebApiDependencyResolver(container);

        // Configure Web API with the dependency resolver.
        GlobalConfiguration.Configuration.DependencyResolver = resolver;

And my Controller:

public class DestinationController : ApiController
{
    private readonly IDestinationService _destinationService;

    public DestinationController(IDestinationService destinationService)
    {
        this._destinationService = destinationService;
    }}

Thanks for your help.

After turning on the custom error messages, I found out if one of the injected dependencies throws an exception, you will get the error message above. But the original error message will be in the inner-exception, which you can see by turning custom errors off from web.config

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM