Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 297
nathfy Avatar asked Oct 30 '13 15:10

nathfy


3 Answers

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

For<HelpController>().Use( ctx => new HelpController() );
like image 98
Michael Fudge Avatar answered Oct 24 '22 08:10

Michael Fudge


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.

like image 21
seeking27 Avatar answered Oct 24 '22 09:10

seeking27


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.

like image 31
Maxime Rouiller Avatar answered Oct 24 '22 09:10

Maxime Rouiller