Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Order of Execution of Startup Codes located on Different Assemblies #10

Open
csoon1 opened this issue Feb 8, 2013 · 12 comments
Open

Order of Execution of Startup Codes located on Different Assemblies #10

csoon1 opened this issue Feb 8, 2013 · 12 comments

Comments

@csoon1
Copy link

csoon1 commented Feb 8, 2013

Okay so let me describe what I'm doing. I have a solution with multiple projects. Let's say I have the projects: Project1 (which is the startup project), Project2, and so on. On some of these projects I have a folder: App_Start where my desired startup codes are located. On the webactivator attribute I specify the desired "Order" for the execution on each code.

The issue is that the sequence of the execution (on multiple assemblies) isn't exactly followed because as soon as an attribute on an assembly is found, the corresponding code is then executed. For example: if Project1 assembly is found first... it will start running the code... even if the Order = 2 (just an example).... but not the code with Order = 1 because it is on another assembly.

I ended up writing my own activator where I scan all assemblies first for any of the startup attributes, put the result on a list, sorting them based on the order, ... then executing the codes.

Is there a chance for webactivator to behave like this? I can place all startup codes in one place and everything would be good... but for some reasons I just like placing each startup code on different assemblies (where they are related... example I place my DI registration startup code on the project that is about DependencyResolution.)

@davidebbo
Copy link
Owner

The slightly strange part is that when dealing with unrelated assemblies, the Order attribute is not necessarily meaningful across assemblies (though I know in your case it is).

Still, I guess it's probably harmless, as long as the ordering within one assembly is honored.

Feel free to send a PR if you have a clean change to do this. Make sure it is efficient in scenarios where there could be a lot of assemblies in bin. Thanks!

@PleasantD
Copy link

I've encountered this as well when using RazorGenerator. Each RazorGenerator project has it's own RazorGeneratorMvcStart.cs file in the App_Start folder so that it can register its view engine in the list. I need to be able to specify the order of these so that the main project happens first, followed by each engine as needed. Sort of a reverse dependency ordering. If this doesn't happen, I wind up with view hiding because the ViewEngines are looking things up in the wrong order.

@davidebbo
Copy link
Owner

I see. So it sounds like you would want something where you can specify the order in which WebActivator processes entire assembly, and not so much control it globally at the level of each PreApplicationStartMethod attribute.

Question: do the assemblies that you want to order have dependencies on each other? If so, we could conceivably walk the dependency graph to automatically order them with children before parents.

@PleasantD
Copy link

I think that having WebActivator walk the dependency graph would work in most cases. Under normal circumstances I would expect the assemblies higher up the chain should initialize first.

However in this circumstance I want them to run in reverse order, with my project firing first, then having sub-assemblies firing afterward.

The reason is that RazorGenerator adds ViewEngines to the list (which is being treated like a stack). If my project registers it's view engine last then it winds up on top of the stack and ends up hiding views from other engines lower in the stack.

@kaa
Copy link

kaa commented Jul 26, 2013

While we're talking dependency graphs, would it be an idea in that case to allow users to specify dependencies explicitly on a type basis like so?

[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(App.App_Start.MyStart), "PreStart", 
  RunBefore = typeof(ExternalAssembly.App_Start.OtherStart)
)]

Of course this requires referencing the other assembly which might be difficult and introduce circular references. So it would be better to specify dependecies by type name as a string instead.

It might certainly be overkill for most situations but it would probably make it safer to order start methods from different sources where you don't know what other packages may be included and how these are specified.

@davidebbo
Copy link
Owner

Note that there is a pending pull request #13 that could allow this is a different way, by making the Order attribute work across all assemblies.

It's not as 'clean' as creating a dependency graph by pointing to types, but it has simplicity going for it. Do you think that would achieve your needs?

@PleasantD
Copy link

Yes. We have setup our own dependency system, but it is made unreliable by
the alphabetical loading of WebActivator. If we can guarantee order will be
respected across all assemblies, then when our dependency handler runs
(last) it can handle all other loading correctly.

On Tue, Jul 30, 2013 at 12:54 AM, David Ebbo [email protected]:

Note that there is a pending pull request #13https://github.com/davidebbo/WebActivator/issues/13that could allow this is a different way, by making the Order attribute
work across all assemblies.

It's not as 'clean' as creating a dependency graph by pointing to types,
but it has simplicity going for it. Do you think that would achieve your
needs?


Reply to this email directly or view it on GitHubhttps://github.com//issues/10#issuecomment-21772852
.

@davidebbo
Copy link
Owner

Yes, we should probably get this change in.

@kaa
Copy link

kaa commented Aug 5, 2013

Great, since I control all assemblies that need to be ordered, simply ordering by the attribute value is definitely sufficient.

@davidebbo
Copy link
Owner

Ok, that change is now in WebActivatorEx 2.0.3. Could you please give it a try to make sure it works well for everyone? Thanks!

@TheFurryMonk
Copy link

What would happen if, for instance, the order for two assemblies is similar? (both assemblies with order=2) Will this throw an error or resort to some kind of a default ordering scheme?

@davidebbo
Copy link
Owner

@TheFurryMonk note that the Order is applied to each PreStart attribute, and not at the assembly level. But in any event, if multiple PreStart have the same Order, it is not an error, but the order between them is arbitrary.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants