1

I'm trying to get StructureMap 3 to work with WebAPI 2 self-hosted in OWIN. Has anyone been successful in getting this to work?

I installed StructureMap 3, WebAPI 2.2, Owin Self Host, and StructureMap.WebApi2 from Nuget. Then in the WebApi service startup class, I added the following:

public class WebApiServiceStartup
{
    public void Configuration(IAppBuilder appBuilder)
    {
        var config = new HttpConfiguration();

        config.MapHttpAttributeRoutes();

        var container = IoC.Initialize();

        config.DependencyResolver = new StructureMapWebApiDependencyResolver(container);

        appBuilder.UseWebApi(config);
    }
}

However, when the application starts, I receive the following exception when the application hits the last line of code in the code sample above:

Exception: Activation error occurred while trying to get instance of type HostBufferPolicySelector, key ""
Inner Exception: Value cannot be null. Parameter name: httpContext

In addition, since the StructureMap.WebApi2 nuget package creates an App_Start folder with code that would typically be run in an IIS-hosted WebApi application during application start-up, I've also tried copying/pasting this code into the startup class's Configuration method as well:

var dependencyScope = new StructureMapDependencyScope(container);

DependencyResolver.SetResolver(dependencyScope);

DynamicModuleUtility.RegisterModule(typeof(StructureMapScopeModule));

GlobalConfiguration.Configuration.DependencyResolver = new StructureMapWebApiDependencyResolver(container);

Unfortunately, even with this additional code I still receive the same error.

I've had no issues in the past getting StructureMap 2 to work with WebAPI 2 and OWIN, so I'm not sure what has changed in StructureMap 3 that would cause this issue.

Matthew Renze
  • 634
  • 1
  • 6
  • 17

0 Answers0