StructureMap Exception after adding the WebApi.HelpPage to a webApi project
Asked Answered
P

3

9

I followed the instructions here to add the webApi.HelpPage area and views to an existing project, which uses structureMap - but when accessing the /Help url:

StructureMap Exception Code:  202 No Default Instance defined for PluginFamily System.Web.Http.HttpRouteCollection, System.Web.Http, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35

So I'm missing something on the structureMap configure:

ObjectFactory.Configure(x => x.Scan(scan =>
            {
                scan.TheCallingAssembly();
                scan.AssembliesFromApplicationBaseDirectory();
                scan.AddAllTypesOf<IHttpModule>();
                scan.WithDefaultConventions();
            }));

Can anyone point a structureMap newbie in the right direction?

Plexus answered 30/10, 2013 at 15:59 Comment(1)
This bug has been reported to stuctureMap devs too on github - hoepfully they will update with fix: github.com/webadvanced/Structuremap.MVC4/issues/6Plexus
O
9

In structuremap 3.x I used the following in my Registry, with success:

For<HelpController>().Use( ctx => new HelpController() );
Onstage answered 24/2, 2015 at 16:48 Comment(2)
thanks for that - we've moved to using swagger.io now for api documentation and testing - but hope this helps othersPlexus
Thanks, Being a beginner with StructureMap, your post helped me a lotDisremember
U
6

I also had the same problem. What I found to be the issue was that there are two constructors in the HelpController. One that takes an HttpConfiguration, and another that takes a GlobalConfiguration. I forced StructureMap to call the GlobalConfiguration constuctor by making the Http constructor private.

    public HelpController()
        : this(GlobalConfiguration.Configuration)
    {
    }

    private HelpController(HttpConfiguration config)
    {
        Configuration = config;
    }

That seemed to do the trick.

Underplot answered 28/4, 2014 at 13:39 Comment(0)
P
4

Ensure to skip System.Web.* assemblies from your assembly scanner.

ObjectFactory.Configure(x => x.Scan(scan =>
    {
        scan.TheCallingAssembly();
        scan.AssembliesFromApplicationBaseDirectory(assembly => !assembly.FullName.StartsWith("System.Web"));
        scan.AddAllTypesOf<IHttpModule>();
        scan.WithDefaultConventions();
    }));

It is a bug and we both commented on the Github of StructureMap. I hope that we won't be needed this in the future but for now, it's a quickfix.

Paling answered 5/11, 2013 at 18:2 Comment(3)
This worked like a charm. The only suggestion I would have is to separate the excluded assemblies in a separate call (in case you have many assemblies being excluded), i.e. - scan.ExcludeNamespace("System.Web");Williamson
This doesn't work for me at all. I need to use seeking27's suggestion.Imaginal
Please note that this was for an MVC 4 web app with basic assembly names. If you are using something different (MVC 5 or MVC 6), you millage may vary.Paling

© 2022 - 2024 — McMap. All rights reserved.