I'm self-hosting a WebApi application in order to do some integration tests.
I set up my server like this:
var httpConfig = new HttpSelfHostConfiguration(BaseAddress);
new ApiServiceConfiguration(httpConfig)
.Configure();
var server = new HttpSelfHostServer(httpConfig);
server.OpenAsync().Wait();
Server = server; //this is just a property on the containing class
ApiServiceConfiguration
is a class that allows me to abstract the config of the WebApi (So I can use it within Global.asax for IIS hosted version of the api)
Here's an extract:
public class ApiServiceConfiguration
{
private readonly HttpConfiguration _httpConfiguration;
public ApiServiceConfiguration(HttpConfiguration httpConfiguration)
{
_httpConfiguration = httpConfiguration;
}
public void Configure()
{
//setup routes
RouteConfig.RegisterRoutes(_httpConfiguration.Routes);
//setup autofac
AutofacConfig.Setup(_httpConfiguration);
My AutofacConfig.Setup
static method is just:
public static void Setup(HttpConfiguration config)
{
var builder = new ContainerBuilder();
// Register the Web API controllers.
builder.RegisterApiControllers(Assembly.GetAssembly(typeof(MyController)));
// Register dependencies.
// Build the container.
var container = builder.Build();
// Configure Web API with the dependency resolver.
//notice config is passed in as a param in containing method
config.DependencyResolver = new AutofacWebApiDependencyResolver(container);
}
However, when I call my api from my unit test:
using (var client = new HttpClient(Server))
{
var result = client.PostAsync(BaseAddress + "api/content/whatever"
var message = result.Content.ReadAsStringAsync().Result;
}
The value of message is:
An error has occurred.","ExceptionMessage":"An error occurred when trying to create a controller of type 'ContentController'. Make sure that the controller has a parameterless public constructor.","ExceptionType":"System.InvalidOperationException","StackTrace":" at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType) at System.Web.Http.Controllers.HttpControllerDescriptor.CreateController(HttpRequestMessage request) at System.Web.Http.Dispatcher.HttpControllerDispatcher.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken) at System.Web.Http.Dispatcher.HttpControllerDispatcher.d__0.MoveNext()","InnerException":{"Message":"An error has occurred.","ExceptionMessage":"Type 'My.Namespace.ContentController' does not have a default constructor","ExceptionType":"System.ArgumentException","StackTrace":" at System.Linq.Expressions.Expression.New(Type type) at System.Web.Http.Inteal.TypeActivator.Create[TBase](Type instanceType) at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage request, Type controllerType, Func`1& activator) at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)
Which indicates to me that the Autofac controller registration has not worked.
If I stick a breakpoint on the following line in my set up:
var server = new HttpSelfHostServer(httpConfig);
And use the immediate window to check httpConfig.DependencyResolver
it is indeed related to Autofac
Server.Configuration.DependencyResolver
is still the autofac dependency resolver. Otherwise try to repro this in a new project and upload it to somewhere. – nemesv