carl
carl

Reputation: 375

Web API Self hosting from a test assembly

I'm currently evaluating WebAPI and NancyFx for a new project about to start. I've managed to get Nancy to self host from a test assembly (by itself it uses asp.net hosting).

Is there any way to do the same with Web API? I would like to keep the web api project hosted on IIS, but i would like to spin it up from my test assembly, so i can run tests against it.

I have found some blogposts on how to use Autofac to scan controllers from another assembly (seems a little backwards only to get hosting from another assembly to work, but if it can be done, i guess that would be an option), but i would like to keep using Structuremap ioc for this project.

Upvotes: 2

Views: 2532

Answers (1)

carl
carl

Reputation: 375

Managed to get it working with help from Mark Jones link. This is what i ended up with in my test assembly.

    private static HttpSelfHostServer _server;

    [BeforeTestRun]
    public static void Setup()
    {
        var config = new HttpSelfHostConfiguration(Settings.TestUri);
        WebApiConfig.Register(config); //map routes
        IocConfig.Bootstrap(config); //configure dependency injection
        _server = new HttpSelfHostServer(config);
        _server.OpenAsync().Wait();
    }

    [AfterTestRun]
    public static void TearDown()
    {
        _server.CloseAsync().Wait();
    }

Upvotes: 4

Related Questions