MVC2& Ninject2 - 控制器未解决依赖性

发布于 2024-10-26 13:19:01 字数 2346 浏览 2 评论 0原文

我愚蠢地决定在周五的工作中尝试一些新的东西!

因此,我使用 NuGet 将 Ninject.Web.Mvc 2.2.xx 添加到我的 .Net MVC2 项目中。

我已经改变了我的 Global.asax.cs

using System.Web.Mvc;
using System.Web.Routing;
using IntegraRecipients;
using Mailer;
using Ninject;
using Ninject.Web.Mvc;
using Ninject.Modules;

namespace WebMailer
{

public class MvcApplication : NinjectHttpApplication
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.IgnoreRoute("favicon.ico");

        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Mail", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );

    }

    protected override void OnApplicationStarted()
    {
        AreaRegistration.RegisterAllAreas();

        RegisterRoutes(RouteTable.Routes);
    }

    protected override IKernel CreateKernel()
    {
        return new StandardKernel(new INinjectModule[] { new MailModule()});
    }

    internal class MailModule : NinjectModule
    {
        public override void Load()
        {
            Bind<IMailing>().To<Mailing>();
            Bind<IMailingContext>().To<MailingContext>();
            Bind<IRecipientContext>().To<RecipientContext>();
        }
    }
}

}

并且我创建了一个像这样的控制器...

using System.Linq;
using System.Web.Mvc;
using WebMailer.Models;

namespace WebMailer.Controllers
{
    [ValidateInput(false)]
    public class MailController : Controller
    {
        private readonly IMailingContext _mailContext;
        private readonly IRecipientContext _integraContext;

        public MailController(IMailingContext mail,IRecipientContext integra)
        {
            _mailContext = mail;
            _integraContext = integra;
        }

        public ActionResult Index()
        {
            return View(_mailContext.GetAllMailings().Select(mailing => new MailingViewModel(mailing)).ToList());
        }
    }
}

但是控制器仍然坚持

找不到类型或命名空间名称“IRecipientContext”(您是否缺少 using 指令或程序集引用?)

并且

找不到类型或命名空间名称“IMailingContext”(您是否缺少 using 指令或程序集引用?)

我的 google-fu 失败了,我真的希望这只是一个愚蠢的拼写错误/丢失线的事情

提前谢谢

P

I foolishly decided to try something new on a Friday job!

So I have used NuGet to add Ninject.Web.Mvc 2.2.x.x to my .Net MVC2 project.

I've altered my Global.asax.cs

using System.Web.Mvc;
using System.Web.Routing;
using IntegraRecipients;
using Mailer;
using Ninject;
using Ninject.Web.Mvc;
using Ninject.Modules;

namespace WebMailer
{

public class MvcApplication : NinjectHttpApplication
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.IgnoreRoute("favicon.ico");

        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Mail", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );

    }

    protected override void OnApplicationStarted()
    {
        AreaRegistration.RegisterAllAreas();

        RegisterRoutes(RouteTable.Routes);
    }

    protected override IKernel CreateKernel()
    {
        return new StandardKernel(new INinjectModule[] { new MailModule()});
    }

    internal class MailModule : NinjectModule
    {
        public override void Load()
        {
            Bind<IMailing>().To<Mailing>();
            Bind<IMailingContext>().To<MailingContext>();
            Bind<IRecipientContext>().To<RecipientContext>();
        }
    }
}

}

and I've created a controller like so...

using System.Linq;
using System.Web.Mvc;
using WebMailer.Models;

namespace WebMailer.Controllers
{
    [ValidateInput(false)]
    public class MailController : Controller
    {
        private readonly IMailingContext _mailContext;
        private readonly IRecipientContext _integraContext;

        public MailController(IMailingContext mail,IRecipientContext integra)
        {
            _mailContext = mail;
            _integraContext = integra;
        }

        public ActionResult Index()
        {
            return View(_mailContext.GetAllMailings().Select(mailing => new MailingViewModel(mailing)).ToList());
        }
    }
}

But the controller is still insisting that

The type or namespace name 'IRecipientContext' could not be found (are you missing a using directive or an assembly reference?)

and

The type or namespace name 'IMailingContext' could not be found (are you missing a using directive or an assembly reference?)

My google-fu has failed me and I really hope this is just a silly typo/missing line thing

Thanks in advance

P

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迷鸟归林 2024-11-02 13:19:01

Ninject 不会改变程序集的编译方式!它不会神奇地添加对其他程序集的引用或添加 using 指令。如果您使用其他程序集的接口,则必须添加 using 指令和对此程序集的引用。

Ninject 的全部工作就是在运行时连接您的应用程序。

Ninject does not change the way assemblies are compiled! It deos not magically add references to other assemblies or add using directives. If you are using interfaces from other assemblies you have to add a using directive and a reference to this assembly.

All Ninject is about is to wire up your application at runtime.

薯片软お妹 2024-11-02 13:19:01

我遇到了类似的问题。

我有一个简单的 WPF Window 项目,其中链接了已编译的 Ninject.dll。但是,以下内容给了我错误...

using Ninject;
namespace CatalogueManager
{
  public class ServiceLocator
  {
    public IMainWindowViewModel GetMainWindowViewModel()
    {
      return Kernel.Get<IMainWindowViewModel>();
    }

    static IKernel Kernel;
    static ServiceLocator()
    {
      Kernel = new StandardKernel(new NinjectConfiguration());
    }
  }
}

特别是,“Ninject”命名空间和 IKernel 提示编译时消息“类型或命名空间 'X'未找到...”

I am have what appears to be a similar problem.

I have a simple WPF Window project with the compiled Ninject.dll linked in. However, the following is giving me errors...

using Ninject;
namespace CatalogueManager
{
  public class ServiceLocator
  {
    public IMainWindowViewModel GetMainWindowViewModel()
    {
      return Kernel.Get<IMainWindowViewModel>();
    }

    static IKernel Kernel;
    static ServiceLocator()
    {
      Kernel = new StandardKernel(new NinjectConfiguration());
    }
  }
}

In particular, "Ninject" namespace and IKernel are prompting the compile time message "type or name space 'X' not found..."

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文