我有Visual Studio 2012 Express for Web.我的步骤 - 创建一个新的ASP .NET MVC 4应用程序 - 选择基本模板-go到Package Manager Console -try来安装WebActivator
PM> Install-Package WebActivator
Run Code Online (Sandbox Code Playgroud)
我得到以下错误

我如何解决这个问题并安装WebActivator包?
我发现了大量关于如何使用Ninject在ASP.NET MVC3中对ActionFilter进行属性注入的不确定文章和问题.
有人能给我一个明确的例子吗?
这是我的自定义身份验证属性.
public class CustomAuthorizeAttribute : AuthorizeAttribute
{
[Inject]
public IService Service { get; set; }
[Inject]
public IAuthenticationHelper AuthenticationHelper { get; set; }
public override void OnAuthorization(AuthorizationContext filterContext)
{
//My custom code
}
}
Run Code Online (Sandbox Code Playgroud)
我正在使用WebActivator来设置Ninject
[assembly: WebActivator.PreApplicationStartMethod(typeof(MyProject.Web.AppStart_NinjectMvc3), "Start")]
namespace MyProject.Web {
public static class AppStart_NinjectMvc3 {
public static void RegisterServices(IKernel kernel) {
//Binding things
}
public static void Start() {
// Create Ninject DI Kernel
IKernel kernel = new StandardKernel();
// Register services with our Ninject DI …Run Code Online (Sandbox Code Playgroud) 我想设置Ninject进行一个简单的测试,并使用Nuget演示易于设置.我想解决一个示例服务.
public interface ITestService
{
string GetMessage();
}
public class TestService : ITestService
{
public string GetMessage() { return "hello world"; }
}
Run Code Online (Sandbox Code Playgroud)
我运行NuGet安装包NinjectMVC3 ....它很好地将NinjectMVC3.cs放入我的App_Start文件夹中,装饰有一些WebActivator属性以使其全部加载.
接下来,我在NinjectMVC3.RegisterServices方法中添加我的绑定:
private static void RegisterServices(IKernel kernel)
{
kernel.Bind<ITestService>().To<TestServiceOne>();
}
Run Code Online (Sandbox Code Playgroud)
现在我想"使用"Ninjet解析我的ITestService.
public ActionResult Index()
{
//[Ninject, give me the service I need]
ITestService service = ???
ViewBag.Message = service.GetMessage();
return View();
}
Run Code Online (Sandbox Code Playgroud)
设置Ninject还有另一部分吗?我需要提供一个解析器吗?
解决我的ITestService需要什么代码.
谢谢你的帮助»
*************更新:******************
感谢关于'控制器构造器'注入的响应.只需添加一个带有ITestServcice的构造函数作为参数.... BAMM !!!
private ITestService _service;
public HomeController(ITestService service)
{
_service = service
}
public ActionResult Index()
{
ViewBag.Message = …Run Code Online (Sandbox Code Playgroud) 我正在将ASP.NET MVC3项目转换为MVC4.我试图找到使用StructureMap和MVC4的最佳方法.我找到了一些可行的解决方案,但尚未尝试过.
第一种解决方案非常简单轻便.第二个(Structuremap.MVC4)依赖于WebActivator进行启动.
什么是更好,最简单的方法?我是否仍需要引导所有内容并使用WebActivator设置DependencyResolver?
谢谢你的帮助.
我已经阅读了几篇关于Ninject for MVC3项目设置的文章.
有人说你的Global.asax.cs应该继承自NinjectHttpApplication,其他人则在通过Application_Start()构造StandardKernel时注册模块.
我个人使用NuGet,它以不同的方式创建AppStart_NinjectMVC3类,并使用它启动它
[assembly: WebActivator.PreApplicationStartMethod(typeof(MyApp.AppStart_NinjectMVC3), "Start")]
Run Code Online (Sandbox Code Playgroud)
每个都有任何优点/缺点吗?
我有几个Web应用程序使用WebActivator来使用包.在我使用IIS 7.5 Express的本地计算机上,无论我在发布版本还是调试配置中进行测试,一切正常.但是,在我的IIS 7.5生产机器上,WebActivator实际上并没有运行,因此所有模块都无法加载,我必须将代码添加回Global.asax.cs文件中.
我很困惑,甚至开始寻找的地方 - 用Google搜索和搜索StackOverflow,但没有遇到任何有类似问题的人.有没有明确需要配置才能运行?
编辑 - 添加了记录到Windows的激活器的快速示例.添加到Global.asax.cs文件时,函数内容在生产服务器上运行正常,但从不从激活器中记录.
using System.Web.Mvc;
using System;
[assembly: WebActivator.PreApplicationStartMethod(typeof(Admin.App_Start.WebActivatorTestStart), "Start")]
namespace Admin.App_Start
{
public static class WebActivatorTestStart {
public static void Start() {
System.Diagnostics.EventLog log = new System.Diagnostics.EventLog();
log.Source = ".NET Runtime";
log.WriteEntry("WebActivator Start", System.Diagnostics.EventLogEntryType.Information);
}
}
}
Run Code Online (Sandbox Code Playgroud) 我刚刚在公司机器上更新了我的NuGet包管理器.自更新以来,我无法添加任何包.我试图安装WebActivator例如,并收到以下错误:
PM> Install-Package WebActivator
Install-Package : The ServicePointManager does not support proxies with the https scheme.
At line:1 char:16
+ Install-Package <<<< WebActivator
+ CategoryInfo : NotSpecified: (:) [Install-Package], NotSupportedException
+ FullyQualifiedErrorId : NuGetCmdletUnhandledException,NuGet.PowerShell.Commands.InstallPackageCommand
Run Code Online (Sandbox Code Playgroud)
我怎么能这样做?
我有几个WebActivator.PreApplicationStartMethod修饰类.
一个用于Ninject,另一个用于AwesomeMVC,第三个用于后台任务调度程序.
问题是调度程序类需要利用由IoC容器解决的依赖性.
我的问题是:
WebActivator.PreApplicationStartMethod在我的Web应用程序中使用NinjectMVC3的属性时,
[assembly: WebActivator.PreApplicationStartMethod(typeof(MyProject.App_Start.NinjectMVC3), "Start")]
Run Code Online (Sandbox Code Playgroud)
我在每个视图源编辑器文件中看到以下警告:
xxx.cshtml:ASP.NET运行时错误:预应用程序启动初始化方法运行类型WebActivator.ActivationManager引发异常,并显示以下错误消息:调用目标引发异常.. xxx.cshtml 1 1
它在我的每个视图文件的第一行(例如a @using或@model声明)中表现为蓝色波浪形.请注意,应用程序运行得很好,问题仅在于编辑我的视图源文件时(visual studio实时错误/警告分析).
我可以WebActivator.PreApplicationStartMethod通过评论来验证这是原因.
我怎样才能解决这个问题?
我WebActivator.PreApplicationStartMethod在我目前的项目中使用但似乎OwinStartupAttribute可以做同样的工作?这是正确使用的OwinStartupAttribute吗?
webactivator ×10
c# ×4
ninject ×3
nuget ×3
asp.net-mvc ×2
.net ×1
asp.net ×1
owin ×1
razor ×1
structuremap ×1