加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – 在web api 2中使用autofac的无参数构造函数错误

发布时间:2020-12-16 07:13:16 所属栏目:asp.Net 来源:网络整理
导读:嘿伙计我正在使用Autofac作为IOC,这是我的结构: using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace AdTudent.Repo{ public interface IRepository { IAdvertisementRepository A
嘿伙计我正在使用Autofac作为IOC,这是我的结构:

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; }
    }
}

我的存储库类是:

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; }
    }
}

我的启动课是:

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);
    }
}

这是我的帐户管理员:

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;

    }
}

但我总是这个问题

“Message”: “An error has occurred.”,
“ExceptionMessage”: “An error occurred when trying to create a controller of type ‘AccountController’. Make sure that the controller has a parameterless public constructor.”,
“ExceptionType”: “System.InvalidOperationException”,
“StackTrace”: ” at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request,HttpControllerDescriptor controllerDescriptor,Type controllerType)rn at System.Web.Http.Controllers.HttpControllerDescriptor.CreateController(HttpRequestMessage request)rn at System.Web.Http.Dispatcher.HttpControllerDispatcher.d__1.MoveNext()

我在互联网上搜索,但我找不到任何可以帮助任何人指导我的东西?

解决方法

按 WebApi OWIN documentation:

A common error in OWIN integration is use of the GlobalConfiguration.Configuration. In OWIN you create the configuration from scratch. You should not reference GlobalConfiguration.Configuration anywhere when using the OWIN integration.

看起来你有几个问题:

>使用OWIN时需要重新启动HttpConfiguration.
>您缺少UseAutofacWebApi虚拟方法调用.

根据文档,您还需要Autofac WebApi OWIN package.

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,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,// and finally the standard Web API middleware.
        app.UseAutofacMiddleware(container);
        app.UseAutofacWebApi(config);
        app.UseWebApi(config);
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读