13
[assembly:  WebActivator.PreApplicationStartMethod(typeof(MyApp.App_Start.StructureMapMvc), "Start")]

namespace MyApp.App_Start
{
    public static class StructureMapMvc
    {
        public static void Start()
        {
            var container = IoC.Initialize();
            DependencyResolver.SetResolver(new SmDependencyResolver(container));
        }
    }
}

这是我应该在 global.asax 中的 Application_start 之前运行的代码。我正在将我的 web 项目从 mvc 3 升级到 mvc 4。所以,在这个过程中,我在命名空间中犯了一个错误。在我更正我的命名空间之前,这是有效的。现在它不再起作用了。我重置了 iis/flushed dns/重建解决方案/删除了 C:\Windows\Microsoft.NET\Framework64\versionxxxxxx...\Temporary ASP.NET Files\root 中的临时 .net 文件。没有任何效果。我在这里错过了什么吗?Initialize() 方法有我所有的结构映射东西依赖解析的东西。所以,如果不弄清楚这一点,我就无法前进。试图诊断问题这么多小时,我需要帮助。

4

5 回答 5

22

如果您的代码在网站项目中(即,在 App_Code 文件夹下),您不能使用PreApplicationStartupMethod!您可以改用PostApplicationStartupMethod。“Pre”方法在 global.asax *Application_Start* 运行之前执行,而“Post”在之后执行。

在我弄清楚这一点之前,我浪费了一个或两个小时,所以希望这能帮助其他人避免这种情况!

于 2013-03-13T15:33:06.217 回答
2

My experience is that WebActivator will not work if your project settings (in .csproj.user or .vbproj.user) have the setting <StartAction>NoStartPage</StartAction> the fix is to set it to <StartAction>CurrentPage</StartAction> and it should then work next time you debug.

Also since it's in a .user file (which are typically not included in svn) it is difficult to determine why it works on some dev environments but not others.

于 2013-05-19T09:45:11.243 回答
0

对我来说,我遇到的问题是使用 NuGet.Server 下载创建私有 NuGet 存储库时(它使用 WebActivatorEx PreApplicationStartMethod 属性)。

我所做的是创建一个空网站项目。这是不正确的:它必须是一个空 Web应用程序项目。一旦我创建了空 Web 应用程序并重新安装了 NuGet.Server,一切正常。

所以:如果您使用了 Empty Web Site 项目,这可能就是您遇到问题的原因。请改用空 Web 应用程序项目。

我认为 Empty Web Site 项目缺少一些 ASP.NET “胶水”,它使 System.Web.PreApplicationStartMethod(由 WebActivatorEx 使用)能够工作。也许知道更多细节的人可以解释为什么会这样?

于 2014-11-12T12:37:52.943 回答
0

WebActivator1.5.3 版本中, MyClass.cs.pp文件不能只存在于App_Start文件夹中,而是必须存在于content\App_Start文件夹中,以便在目标项目nuget install中创建转换后的文件。App_Start

据我所知,这是无证的。

注意:只要原始文件.nupkgnuget pack使用传统文件系统方法构建的,此解决方案似乎就可以工作,但使用nuget pack以特定.csproj文件为目标时则不行。

于 2013-01-04T06:01:17.247 回答
0

我的问题是双重的。

1) 没有声明类型的完整路径

2)将属性放在命名空间内,而不是之前

一旦我解决了这两个问题,它就起作用了。

[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(MyApp.Api.Controllers.MyController), "AutoMapperStart")]


namespace MyApp.Api.Controllers
{

    public class MyController : ApiController
    {
        public static void AutoMapperStart()
        {
            MyMapperConfig.DefineMappings();
        }
    }
}
于 2015-11-05T02:16:02.123 回答