WebActivator.PreApplicationStartMethod does not work
Asked Answered
H

5

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

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.

Hortense answered 2/1, 2013 at 0:12 Comment(0)
E
23

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!

Encapsulate answered 13/3, 2013 at 15:33 Comment(0)
A
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.

Andy answered 19/5, 2013 at 9:45 Comment(0)
D
0

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.

Dixie answered 4/1, 2013 at 6:1 Comment(0)
C
0

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?

Cressida answered 12/11, 2014 at 12:37 Comment(0)
B
0

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();
        }
    }
}
Broomrape answered 5/11, 2015 at 2:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.