相关疑难解决方法(0)

确保控制器具有无参数的公共构造函数错误

我已经按照本教程进行了很好的工作,直到我修改了我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)

c# asp.net-mvc unity-container asp.net-web-api

93
推荐指数
5
解决办法
16万
查看次数

使用Ninject在构造函数中使用其他参数创建实例

我决定开始使用Ninject并面临一个问题.说我有以下场景.我有一个IService接口和2个实现此接口的类.而且我还有一个类,它有一个构造函数获取IService和一个int.如何使用Ninject创建此类的实例(我不想硬连接这个int,我想在每次获取实例时都传递它)?

以下是一些说明情况的代码:

interface IService
{
    void Func();
}

class StandardService : IService
{
    public void Func()
    {
        Console.WriteLine("Standard");
    }
}

class AlternativeService : IService
{
    public void Func()
    {
        Console.WriteLine("Alternative");
    }
}


class MyClass
{
    public MyClass(IService service, int i)
    {
        this.service = service;
    }

    public void Func()
    {
        service.Func();
    }

    IService service = null;
}
class Program
{
    static void Main(string[] args)
    {
        IKernel kernel = new StandardKernel(new InlineModule(
            x => x.Bind<IService>().To<AlternativeService>(),
            x => x.Bind<MyClass>().ToSelf())); …
Run Code Online (Sandbox Code Playgroud)

c# ninject ioc-container

63
推荐指数
1
解决办法
5万
查看次数

确保控制器在Unity中具有无参数的公共构造函数

我在控制器中遇到了这个问题:
尝试创建类型为' * .WebMvc.Controllers.HomeController' 的控制器时发生错误.确保控制器具有无参数的公共构造函数.

找到ApiController的解决方案,但没有找到任何关于普通Controller的信息.

从头开始创建新的MVC 4项目.

HomeController.cs:

public class HomeController : Controller
{
    private IAccountingUow _uow;
    public HomeController(IAccountingUow uow)
    {
        _uow = uow;
    }
Run Code Online (Sandbox Code Playgroud)

UnityDependencyResoler.cs:

public class UnityDependencyResolver : IDependencyResolver
{
    private IUnityContainer _container;
    public UnityDependencyResolver(IUnityContainer container)
    {
        _container = container;
        RegisterTypes();
    }
    public object GetService(Type serviceType)
    {
        try
        {
            return _container.Resolve(serviceType);
        }catch
        {
            return null;
        }
    }

    public IEnumerable<object> GetServices(Type serviceType)
    {
        try
        {
            return _container.ResolveAll(serviceType);
        }catch
        {
            return null;
        }
    }

    private void RegisterTypes()
    {
        _container.RegisterType<IAccountingUow, …
Run Code Online (Sandbox Code Playgroud)

c# dependency-injection unity-container asp.net-mvc-4

7
推荐指数
1
解决办法
2万
查看次数

Ninject"没有为此对象定义无参数构造函数."

我正在测试Ninject,但是按照操作方法,我发现无法使其工作.网络上的信息非常混乱甚至是矛盾的.我正在使用Visual Studio 2012在MVC 4中开发一个网站,我确实使用Nuget安装了Ninject.

所以我得到一个错误:"没有为这个对象定义无参数构造函数." 一进入我的控制器.

我做了必要的步骤:

  • Nuget安装
  • 在NinjectWebCommon.cs中,我确实在RegisterServices方法中注册了我的接口.
  • 在我的家庭控制器中,我设置了这样的对象:

    public  ISurvey _survey { get; set; }
    
    [Inject]
    public HomeController(ISurvey s)
    {
        _survey = s;
    }
    
    Run Code Online (Sandbox Code Playgroud)

我收到以下错误消息:

Server Error in '/' Application.

No parameterless constructor defined for this object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.MissingMethodException: No parameterless constructor defined for this object. …

ninject ninject.web.mvc

5
推荐指数
1
解决办法
2769
查看次数

确保控制器在web api中有一个无参数的公共构造函数?

我正在努力解决使用untiy的依赖注入问题.我已根据此链接实施https://www.asp.net/web-api/overview/advanced/dependency-injection

但得到了这个错误:

{
  "Message": "An error has occurred.",
  "ExceptionMessage": "An error occurred when trying to create a controller of type 'WebAPIController'. 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)\r\n   at System.Web.Http.Controllers.HttpControllerDescriptor.CreateController(HttpRequestMessage request)\r\n   at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()",
  "InnerException": {
    "Message": "An error has occurred.",
    "ExceptionMessage": "Type 'Fairmoves.Controllers.WebAPIController' does not have a default constructor",
    "ExceptionType": "System.ArgumentException",
    "StackTrace": "   at System.Linq.Expressions.Expression.New(Type type)\r\n   at System.Web.Http.Internal.TypeActivator.Create[TBase](Type instanceType)\r\n   at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage request, Type …
Run Code Online (Sandbox Code Playgroud)

asp.net-mvc dependency-injection inversion-of-control unity-container asp.net-web-api

1
推荐指数
1
解决办法
6447
查看次数