0

I am using StructureMap.WebApi2 nuget package for Web API 2 project for managing the depedency injection. The Web API controllers use constructor injection to inject a UrlHelper dependency which should be resolved by StructureMap Ioc. I am trying the following approach to set the UrlHelper for web api controller:

public class FooController : ApiController
{
    private UrlHelper _UrlHelper;

    public ModelFactory(HttpRequestMessage request)
        {
            _UrlHelper = new UrlHelper(request);
            }
}

But with the above code I am getting the following error:

No default Instance is registered and cannot be automatically determined for type 'System.Net.Http.HttpMethod' There is no configuration specified for System.Net.Http.HttpMethod

Can anyone suggest me the best possible ways to resolve the above issue?

santosh kumar patro
  • 7,231
  • 22
  • 71
  • 143

1 Answers1

0

Your problem is that StructureMap tries to resolve the greediest constructor first, in this instance taking a look at the source code for HttpRequestMessage reveals the following constructors:

public HttpRequestMessage(HttpMethod method, string requestUri);
public HttpRequestMessage(HttpMethod method, Uri requestUri);

This is where your HttpMethod issue is coming from. StructureMap tries to create an instance of HttpRequestMessage but has no idea how to resolve the method and requestUri dependencies.

In this instance you need to manually configure your dependency within your StructureMap configuration so it knows how to create an instance of HttpRequestMessage like so:

this.For<HttpRequestMessage>().Use(new HttpRequestMessage());

Alternatively, to create instances of overloaded constructors then you'll need to manually resolve the overloaded dependencies (I would recommend using a factory for this in order to keep your SM configuration nice and clean. Here is an example of how you can do this).

Community
  • 1
  • 1
Joseph Woodward
  • 9,191
  • 5
  • 44
  • 63