I've trawled through multiple blogs etc trying to find out how to configure StructureMap with Web API 2 and none of the implementations worked for me. The confusion seems to be around the different IDependency Resolver that MVC uses and the one that Web API uses.
Firstly, which is the correct Nuget package and secondly, how do you configure it with a pure Web API 2 project?
Thanks
This is what I have so far and it seems to be working. Is this correct?
public class StructureMapControllerActivator : IHttpControllerActivator
{
private readonly IContainer _container;
public StructureMapControllerActivator(IContainer container)
{
if (container == null) throw new ArgumentNullException("container");
_container = container;
}
public IHttpController Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)
{
try
{
var scopedContainer = _container.GetNestedContainer();
scopedContainer.Inject(typeof(HttpRequestMessage), request);
request.RegisterForDispose(scopedContainer);
return (IHttpController)scopedContainer.GetInstance(controllerType);
}
catch (Exception e)
{
// TODO : Logging
throw e;
}
}
}
Configuring Web API with ASP.NET Hosting In an ASP.NET application, configure Web API by calling GlobalConfiguration. Configure in the Application_Start method. The Configure method takes a delegate with a single parameter of type HttpConfiguration. Perform all of your configuration inside the delegate.
In the Solution Explorer, select the folder App_Start and choose Add New Item... from the Project main menu. Select Class as the new item and name it WebApiConfig. cs. Confirm with Add.
The WebApiConfig. cs is configuration file for Web API. You can configure routes and other things for web API, same like RouteConfig. cs is used to configure MVC routes. It also creates Web API controller ValuesController.
Adding StructureMap. In the Web API project, let's add a NuGet package called StructureMap.WebApi2. On doing so, we'll see that the structure of the project has changed: The new files are what allow StructureMap to operate in our Web API project, and if you'd like more information about them, check out the GitHub documentation.
In an ASP.NET application, configure Web API by calling GlobalConfiguration.Configure in the Application_Start method. The Configure method takes a delegate with a single parameter of type HttpConfiguration. Perform all of your configuration inside the delegate. Here is an example using an anonymous delegate:
The Route attribute can be applied on any controller or action method. In order to use attribute routing with Web API, it must be enabled in WebApiConfig by calling config.MapHttpAttributeRoutes () method. Consider the following example of attribute routing.
The project template creates a file named WebApiConfig.cs inside the App_Start folder. This code file defines the delegate where you should put your Web API configuration code.
There are few links, were I've tried to explain how we can use (easily) StructureMap and Web API together:
We have to implement the IHttpControllerActivator
, which could profit from already configured StructureMap ObjectFactory
:
public class ServiceActivator : IHttpControllerActivator
{
public ServiceActivator(HttpConfiguration configuration) {}
public IHttpController Create(HttpRequestMessage request
, HttpControllerDescriptor controllerDescriptor, Type controllerType)
{
var controller = ObjectFactory.GetInstance(controllerType) as IHttpController;
return controller;
}
}
And then, we have to just register our implementation (global.asax):
protected void Application_Start()
{
...
HttpConfiguration config = GlobalConfiguration.Configuration;
config.Services
.Replace(typeof(IHttpControllerActivator), new ServiceActivator(config));
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With