我已经按照本教程进行了很好的工作,直到我修改了我DbContext有一个额外的构造函数.我现在遇到了解决方案的问题,不知道如何解决这个问题.是否有一种简单的方法可以强制它抓住无参数构造函数,或者我接近这个错误?
DbContext 有两个构造函数:
public class DashboardDbContext : DbContext
{
public DashboardDbContext() : base("DefaultConnection") { }
public DashboardDbContext(DbConnection dbConnection, bool owns)
: base(dbConnection, owns) { }
}
Run Code Online (Sandbox Code Playgroud)
SiteController 构造函数:
private readonly IDashboardRepository _repo;
public SiteController(IDashboardRepository repo)
{
_repo = repo;
}
Run Code Online (Sandbox Code Playgroud)
库:
DashboardDbContext _context;
public DashboardRepository(DashboardDbContext context)
{
_context = context;
}
Run Code Online (Sandbox Code Playgroud)
UnityResolver 码:
public class UnityResolver : IDependencyResolver
{
private readonly IUnityContainer _container;
public UnityResolver(IUnityContainer container)
{
_container = container;
}
public object GetService(Type serviceType)
{ …Run Code Online (Sandbox Code Playgroud) 我试图用Simple Injector做一些基本的构造函数DI,似乎它无法解析Web API控制器的依赖关系.
SimpleInjectorWebApiDependencyResolver文档的基础,这也是在这里找到的.这似乎不是问题,但我仍然收到以下错误:
类型"MyProject.API.ArticleController"没有默认构造函数
System.ArgumentException at
System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator的System.Web.Http.Internal.TypeActivator.Create [TBase](Type instanceType)中的System.Linq.Expressions.Expression.New(Type type)(HttpRequestMessage请求,类型为controllerType) System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request,HttpControllerDescriptor controllerDescriptor,Type controllerType),Func`1&activator)
如果有人可以提供一些关于是否应该从其当前状态/呼叫顺序修改任何内容的建议,将不胜感激.
ArticleController(基本结构):
public class ArticleController : ApiController
{
private readonly IArticleRepository articleRepository;
private readonly IUserRepository userRepository;
private readonly IReleaseRepository releaseRepository;
public ArticleController(IArticleRepository articleRepository, IUserRepository userRepository, IReleaseRepository releaseRepository)
{
this.articleRepository = articleRepository;
this.userRepository = userRepository;
this.releaseRepository = releaseRepository;
}
// GET api/Article
public IEnumerable<Article> GetArticles(){ // …Run Code Online (Sandbox Code Playgroud) 我是使用Unity的新手,但我的问题是每当我调用我的Web服务时,我都会得到一个例外情况
"确保控制器具有无参数的公共构造函数"
我已经遵循了多个教程,我仍然遇到同样的问题.
在我的WebApiConfig类的Register函数中,我有
var container = new UnityContainer();
container.RegisterType<IValidator, Validator>(new HierarchicalLifetimeManager());
config.DependencyResolver = new UnityResolver(container);
Run Code Online (Sandbox Code Playgroud)
这是我的UnityResolver类
using Microsoft.Practices.Unity;
using System;
using System.Collections.Generic;
using System.Web.Http.Dependencies;
public class UnityResolver : IDependencyResolver
{
protected IUnityContainer container;
public UnityResolver(IUnityContainer container)
{
if (container == null)
{
throw new ArgumentNullException("container");
}
this.container = container;
}
public object GetService(Type serviceType)
{
try
{
return container.Resolve(serviceType);
}
catch (ResolutionFailedException)
{
return null;
}
}
public IEnumerable<object> GetServices(Type serviceType)
{
try
{
return container.ResolveAll(serviceType);
}
catch (ResolutionFailedException) …Run Code Online (Sandbox Code Playgroud) 我知道它之前已经被问过并回答了 - 我问的原因是因为(我认为)我尝试了所有建议的解决方案来解决这个问题,但仍然无法解决它.
我有一个ASP.NET Web API 2.0项目.我有Autofac,Autofac.Mvc5并Autofac.WebApi2安装了依赖项.当我尝试调用API控制器时,我收到以下错误:
尝试创建"MyController"类型的控制器时发生错误.确保控制器具有无参数的公共构造函数.
在我Global.asax的电话中IocConfig.Config(),我已将其置于其中App_Start:
public static class IocConfig
{
public static void Config()
{
var builder = new ContainerBuilder();
builder.RegisterType<MyLogger>().As<IMyLogger>();
builder.RegisterApiControllers(Assembly.GetCallingAssembly());
builder.RegisterWebApiFilterProvider(GlobalConfiguration.Configuration);
WebApiApplication.Container = builder.Build();
DependencyResolver.SetResolver(
new AutofacDependencyResolver(WebApiApplication.Container));
GlobalConfiguration.Configuration.DependencyResolver =
new AutofacWebApiDependencyResolver(WebApiApplication.Container);
}
}
Run Code Online (Sandbox Code Playgroud)
这是以下构造函数MyController:
public MyController(IMyLogger logger)
Run Code Online (Sandbox Code Playgroud)
当我尝试调用它时,我得到有关构造函数的指定错误.我错过了什么?
这是手头的问题:
在调用我CustomerController的时候URL,我得到以下异常:
ExceptionMessage:
尝试创建"CustomerController"类型的控制器时发生错误.确保控制器具有无参数的公共构造函数.
我使用以下网址:
请注意:
/api/Customer/在我将逻辑重构为业务类并实现依赖注入之前,调用正在进行.
我的研究表明,我没有正确地注册我的界面和课程Ninject,但不确定我错过了哪一步.
研究链接:
这是我的问题导致此异常的原因是什么?我正在注册我的接口/类
Ninject,但它似乎没有正确识别映射.有什么想法吗?
客户控制员
public class CustomerController : ApiController
{
private readonly ICustomerBusiness _customerBusiness;
public CustomerController(ICustomerBusiness customerBusiness)
{
_customerBusiness = customerBusiness;
}
// GET api/Customer
[HttpGet]
public IEnumerable<Customer> GetCustomers()
{
return _customerBusiness.GetCustomers();
}
// GET api/Customer/Id
[HttpGet]
public IEnumerable<Customer> GetCustomersById(int customerId)
{
return _customerBusiness.GetCustomerById(customerId);
}
}
Run Code Online (Sandbox Code Playgroud)
客户业务
public class CustomerBusiness : ICustomerBusiness
{
private readonly DatabaseContext …Run Code Online (Sandbox Code Playgroud)