我使用Autofac作为国际奥委会和这里嗨是我的结构:在网页API autofac参数的构造函数错误2
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;
}
}
但我总是这个问题
“消息”:“发生了错误。” “ExceptionMessage”:“试图创建类型的控制装置‘的AccountController’时发生错误。 “, ”ExceptionType“:”System.InvalidOperationException“, ”StackTrace“:”at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request,HttpControllerDescriptor controllerDescriptor,Type controllerType个)\ r \ n在System.Web.Http.Controllers.HttpControllerDescriptor.CreateController(HttpRequestMessage请求个)\ r \ n在System.Web.Http.Dispatcher.HttpControllerDispatcher.d__1.MoveNext()
我搜索在互联网上,但我找不到任何可以帮助任何人可以引导我?
我不需要MVC控制器我只是需要web API控制器,因为我上面提到,你有什么想法吗? –
您还没有按照正确的顺序注册您的全局过滤器。在调用builder.Build();'之前,您需要调用'builder.RegisterWebApiFilterProvider(GlobalConfiguration.Configuration);'。看我上面的例子。 – NightOwl888
我按照你所说的编辑了代码,但结果和以前一样,你有什么想法吗? –