How to get your own IoC integration test host to integrate with Web-Api?

I took the tips from here to come up with this integration test for my new Web Api project. I am trying to create a web service for recreation, and I have a client assistant that I plan to release to users of the API. This is the type of ExampleClientHelper . Oh and btw, these are all the wires to the ValuesController that come with the project template for the MVC4 Web Api Visual Studio project - I simplify everything until I understand it.

ExampleClientHelper replaces the entire request / response in the above reference example. It uses RestSharp internally.

 [Test] [Ignore] public void ValuesHelper_ShouldReturn_value1_And_value2_AsTypedObject() { // IoC prep var builder = new ContainerBuilder(); var container = builder.Build(); // web server prep var baseUri = new Uri("http://localhost:8080"); var config = new HttpSelfHostConfiguration(baseUri); config.DependencyResolver = new AutofacWebApiDependencyResolver(container); // yes, the routing needs to be copied over. it not compatible with the MVC routes config.Routes.MapHttpRoute("Api", "api/{controller}/{id}", new { id = RouteParameter.Optional, namespaces = new[] { typeof(ValuesController).Namespace } }); // start the server and make a request new HttpSelfHostServer(config) .OpenAsync() .ContinueWith(task => { var client = new ExampleClientHelper(baseUri); var values = client.GetValues(); // then test the response Assert.AreEqual("value1", values.ElementAt(0)); Assert.AreEqual("value2", values.ElementAt(1)); }) .Wait(); } 

The code above works fine until you change the ValuesController . i.e. it stays with an implicit parallelless constructor.

The problem I am facing is that the host server itself cannot create an instance of ValuesController when I modify it to require a dependency. The problem is that I get the same exception from the response of my assistant client whether I connect Autofac DependencyResolver or not. This is what returns in the Content of the response, nicely formatted as JSON thanks to RestSharp:

{"ExceptionType": "System.ArgumentException", "Message": "Type" Embed.ECSApi.RestServer.Controllers.ValuesController 'does not have a default constructor "," StackTrace ":" in System.Linq.Expressions.Expression.New (type type) \ r \ n in System.Web.Http.Internal.TypeActivator.Create [TBase] (type instanceType) \ r \ n in System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create (request HttpRequestMessage, HttpControllerDescript Type controllerType) "}

Thus, the host server itself is trying to create a ValuesController , but it cannot. What for? I thought I connected DependencyResolver correctly. I expect to get an Autofac exception instead, which complains about my dependency that I haven't configured.

+4
source share
1 answer

It looks like you forgot to register the controller with the container, and the web API is trying to create an instance for you.

 var builder = new ContainerBuilder(); // Register API controllers using assembly scanning. builder.RegisterApiControllers(typeof(ValuesController).Assembly); var container = builder.Build(); 

You can also register a separate controller.

+2
source

All Articles