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

WebApi MapHttpAttributeRoutes causes error in IComponent #4775

Closed
Padbury opened this issue Feb 27, 2019 · 7 comments
Closed

WebApi MapHttpAttributeRoutes causes error in IComponent #4775

Padbury opened this issue Feb 27, 2019 · 7 comments

Comments

@Padbury
Copy link

Padbury commented Feb 27, 2019

This is similar to issue #4464 but slightly different hence the new issue.

So, in v7 to register my own WebApi routes I'd create a class that implements IApplicationEventHandler and register my routes via the OnApplicationStarted method like this:

GlobalConfiguration.Configuration.MapHttpAttributeRoutes();

In v8, it's suggested to use a class that implements IUserComposer then append a component. Then it's suggested to use the Initialize() method to execute startup functions.

However, if I put GlobalConfiguration.Configuration.MapHttpAttributeRoutes(); inside Initialize() I get the following error:

The object has not yet been initialized. Ensure that HttpConfiguration.EnsureInitialized() is called in the application's startup code after all other initialization code.

If I move the call into the constructor for the class

public RouteConfigComponent() { GlobalConfiguration.Configuration.MapHttpAttributeRoutes(); }

Then the error goes away and routes are registered correctly using attributes e.g.

[Route("api/question/{id}")] public HttpResponseMessage Get(int id) { return Request.CreateResponse(HttpStatusCode.OK); }

The workaround is to have this in the constructor but I'm told this is probably not the desired behaviour and to register it here for review.

@zpqrtbnk
Copy link
Contributor

zpqrtbnk commented Mar 4, 2019

Notes for self: odd that it works in the constructor and not in Initialize as Initialize runs after the constructor. Going to look into it.

@zpqrtbnk
Copy link
Contributor

zpqrtbnk commented Mar 4, 2019

Reproduced. Putting MapHttpAttributeRoutes in the Initialize method does trigger the exception.

@zpqrtbnk
Copy link
Contributor

zpqrtbnk commented Mar 4, 2019

The problem is caused by WebRuntimeComponent doing:

// ensure WebAPI is initialized, after everything
GlobalConfiguration.Configuration.EnsureInitialized();

in its own Initialize method - and we do need to do this for everything to run.

But because your component runs after the WebRuntimeComponent, it invokes MapHttpAttributeRoutes after EnsureInitialized has been invoked, hence the exception.

Short term, registering your component as follows would work:

public class MyComposer : IComposer
{
  public void Compose(Composition composition)
  {
    composition.Components().Insert<TempComponent>();
  }
}

because it would insert your component in the first position, ie even before WebRuntimeComponent - but then, you have to be careful with what you do in the component, as strictly nothing will be initialized (no content cache, no nothing).

Longer term, it feels that EnsureInitialized should run after every other component has had a chance to do things - not before. Looking into it.

@zpqrtbnk
Copy link
Contributor

zpqrtbnk commented Mar 4, 2019

Going to fix it properly - in progress.

@zpqrtbnk
Copy link
Contributor

zpqrtbnk commented Mar 5, 2019

PR: #4856

Test: with this in place, a user component should be able to MapHttpAttributeRoutes because EnsureInitialized will run last (after every other components).

Note: renaming some composers etc = breaking.

@Shazwazza
Copy link
Contributor

I can confirm this works. The breaking names are not going to affect anyone ... well except for @aaronpowell ;)

@ghost ghost removed the state/review label Mar 7, 2019
@aaronpowell
Copy link
Contributor

Haha I think I'm good for this one 😛

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

No branches or pull requests

4 participants