WebActivator.PreApplicationStartMethod does not wo

2019-01-23 15:58发布

[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));
        }
    }
}

Here is my code that is supposed to run before Application_start in global.asax. I was upgrading my web project from mvc 3 to mvc 4. So, In that process, I made a mistake in namespace. This was working before i corrected my namespace. It no longer works now. I reset iis/flushed dns/ rebuilt solution/removed the temporary .net files in C:\Windows\Microsoft.NET\Framework64\versionxxxxxx...\Temporary ASP.NET Files\root. Nothing worked. Am i missing something here? The Initialize() method has all my structure map stuff dependency resolution stuff. So, I can't move forward without figuring this out. Tried to diagnose the problem for so many hours and i need help.

5条回答
地球回转人心会变
2楼-- · 2019-01-23 16:30

For me, the problem I had was when creating a private NuGet repository using NuGet.Server download (it makes use of WebActivatorEx PreApplicationStartMethod attribute).

What I did was create an Empty Web Site project. This is incorrect: it needs to be an Empty Web Application project. Once I created the Empty Web Application and re-installed NuGet.Server everything worked fine.

So: If you've used an Empty Web Site project, that might be why you're having the problem. Use an Empty Web Application project instead.

I'm thinking the Empty Web Site project is missing some of the ASP.NET "glue" which enables the System.Web.PreApplicationStartMethod (as used by WebActivatorEx) to work. Maybe someone who knows a little more of the details could explain why this is?

查看更多
ら.Afraid
3楼-- · 2019-01-23 16:32

My problem was twofold.

1) not declaring full path to the type

2) placing attribute inside the namespace, not before

Once I fixed both of these it worked.

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


namespace MyApp.Api.Controllers
{

    public class MyController : ApiController
    {
        public static void AutoMapperStart()
        {
            MyMapperConfig.DefineMappings();
        }
    }
}
查看更多
smile是对你的礼貌
4楼-- · 2019-01-23 16:41

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.

查看更多
霸刀☆藐视天下
5楼-- · 2019-01-23 16:49

With WebActivator version 1.5.3, the MyClass.cs.pp file cannot just live in the App_Start folder, but must live in the content\App_Start folder in order for nuget install to create the transformed file in the App_Start of the target project.

This is undocumented, as far as I can tell.

NOTE: This solution seems to work so long as the originating .nupkg was built using nuget pack using a conventional file system approach, but NOT when using nuget pack that targets a specific .csproj file.

查看更多
啃猪蹄的小仙女
6楼-- · 2019-01-23 16:55

If your code is in a Web Site Project (ie, under the App_Code folder) you cannot use PreApplicationStartupMethod! You can use PostApplicationStartupMethod instead. The "Pre" method executes before global.asax *Application_Start* runs, while "Post" executes after.

I wasted a good hour or two before I figured this out, so hopefully this will help someone else avoid that!

查看更多
登录 后发表回答