我使用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
我按照你所說的編輯了代碼,但結果和以前一樣,你有什麼想法嗎? –