Constructor error without autofac parameters in web api 2

Hey guys, I am using Autofac as IOC, and here is my structure:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AdTudent.Repo
{
    public interface IRepository
    {
        IAdvertisementRepository Advertisements { get; set; }
        IProfileRepository Profiles { get; set; }
    }
}

and my repository class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AdTudent.Repo
{
    public class Repositories : IRepository
    {
        public Repositories(IAdvertisementRepository advertisementRepository,
                            IProfileRepository profileRepository)
        {
            Advertisements = advertisementRepository;
            Profiles = profileRepository;
        }
        public IAdvertisementRepository Advertisements { get; set; }
        public IProfileRepository Profiles { get; set; }
    }
}

and my launch class:

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

        builder.RegisterType<Repositories>().As<IRepository>();
        builder.Register<IGraphClient>(context =>
        {
            var graphClient = new GraphClient(new Uri("http://localhost:7474/db/data"));
            graphClient.Connect();
            return graphClient;
        }).SingleInstance();
        // STANDARD WEB API SETUP:

        // Get your HttpConfiguration. In OWIN, you'll create one
        // rather than using GlobalConfiguration.
        var config = new HttpConfiguration();

        // Register your Web API controllers.
        //builder.RegisterApiControllers(Assembly.GetExecutingAssembly());

        // Run other optional steps, like registering filters,
        // per-controller-type services, etc., then set the dependency resolver
        // to be Autofac.

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

        app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);

        // OWIN WEB API SETUP:

        // Register the Autofac middleware FIRST, then the Autofac Web API middleware,
        // and finally the standard Web API middleware.
        app.UseAutofacMiddleware(container);
        app.UseAutofacWebApi(config);
        app.UseWebApi(config);

        ConfigureAuth(app);
    }
}

and here is my account controller:

public class AccountController : ApiController
{
    private  IRepository _repository;
    private const string LocalLoginProvider = "Local";
    private ApplicationUserManager _userManager;
    private IGraphClient _graphClient;


    public ISecureDataFormat<AuthenticationTicket> AccessTokenFormat { get; private set; }       

    public AccountController(IRepository repository, IGraphClient graphClient,
         ISecureDataFormat<AuthenticationTicket> accessTokenFormat)
    {

        AccessTokenFormat = accessTokenFormat;
        _repository = repository;
        _graphClient = graphClient;

    }
}

but I always have this problem

"": " ".      "ExceptionMessage": " AccountController. , ".      "ExceptionType": "System.InvalidOperationException",      "StackTrace": " System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create( HttpRequestMessage, HttpControllerDescriptor controllerDescriptor, controllerType)\r\n System.Web.Http.Controllers.HttpControllerDescriptor.CreateController( HttpRequestMessage)\r\n System.Web.Http.Dispatcher.HttpControllerDispatcher.d__1.MoveNext()

, , , - ?

+4
3

WebApi OWIN:

​​ OWIN GlobalConfiguration.Configuration. OWIN . GlobalConfiguration.Configuration , OWIN.

, :

  • OWIN HttpConfiguration.
  • UseAutofacWebApi.

Autofac WebApi OWIN .

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

        // STANDARD WEB API SETUP:

        // Get your HttpConfiguration. In OWIN, you'll create one
        // rather than using GlobalConfiguration.
        var config = new HttpConfiguration();

        // Register your Web API controllers.
        builder.RegisterApiControllers(Assembly.GetExecutingAssembly());

        // Run other optional steps, like registering filters,
        // per-controller-type services, etc., then set the dependency resolver
        // to be Autofac.
        var container = builder.Build();
        config.DependencyResolver = new AutofacWebApiDependencyResolver(container);

        app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);

        // OWIN WEB API SETUP:

        // Register the Autofac middleware FIRST, then the Autofac Web API middleware,
        // and finally the standard Web API middleware.
        app.UseAutofacMiddleware(container);
        app.UseAutofacWebApi(config);
        app.UseWebApi(config);
    }
}
+5

Autofac, ISecureDataFormat<AuthenticationTicket>.

builder.Build().

builder.RegisterType<ISecureDataFormat<AuthenticationTicket>>().As<TicketDataFormat>();
0

Check your Global.asax.cs. You should delete this line if you have it:

GlobalConfiguration.Configure(WebApiConfig.Register);

You can transfer this to the Startup.cs configuration as follows:

WebApiConfig.Register(config);
0
source

Source: https://habr.com/ru/post/1606116/


All Articles