0
votes

I am trying to to run a self hosted web api app using owin like here

http://www.asp.net/web-api/overview/hosting-aspnet-web-api/use-owin-to-self-host-web-api

public class Program 
{ 
    static void Main() 
    { 
        string baseAddress = "http://localhost:9000/"; 

        // Start OWIN host 
        using (WebApp.Start<Startup>(url: baseAddress)) 
        { 
            Console.WriteLine("App started");
            Console.ReadLine();
        } 
    } 
} 


public class Startup 
{ 
    // This code configures Web API. The Startup class is specified as a type
    // parameter in the WebApp.Start method.
    public void Configuration(IAppBuilder appBuilder) 
    { 
        // Configure Web API for self-host. 
        HttpConfiguration config = new HttpConfiguration(); 
        config.Routes.MapHttpRoute( 
            name: "DefaultApi", 
            routeTemplate: "api/{controller}/{id}", 
            defaults: new { id = RouteParameter.Optional } 
        ); 

        appBuilder.UseWebApi(config); 
    } 
} 

public class ValuesController : ApiController 
{ 
    // GET api/values 
    public IEnumerable<string> Get() 
    { 
        return new string[] { "value1", "value2" }; 
    } 

}

which event.

I tried to install Microsoft.AspNet.WebApi (Web Api 2.1) via nuget which fails because of framework 4.5 requirement so I installed AspNetWebApi (Web Api).

But I can't find the extension method UseWebApi anywhere. Do I have to install another package or is it impossible to host a web api with Framework 4?

2
I am no expert, having only just started using WebApi and OWIN myself, but because Web Api 2 is fully async, could it ever be used on .NET 4 without a dependency on Microsoft.Bcl.Async? - Lukazoid
Well WebApi (V1) also has an ApiController class located in System.Web.Http.dll v4.0 - Jürgen Steinblock
And WebApi (V1) can be self hosted with HttpSelfHostServer (Microsoft.AspNet.WebApi.SelfHost package) but I am searching for an Owin implementation because I already host Nancyfx with owin. - Jürgen Steinblock

2 Answers

1
votes

I don't think that you can use OWIN, bute you can create a self-hosted web api using Visual Studio 2010 and .NET Framework 4.0 using this approach: http://www.asp.net/web-api/overview/older-versions/self-host-a-web-api

While the approach is not recommended for new designs, I'm forced to use .NET Framework 4.0 due to dependencies in our existing code base. The article is written with Visual Studio 2012 but I've just tested the server side with Visual Studio 2010 and it appears to work ok. I used Postman instead of the client example.

0
votes

Probably not because the self hosting package depends on System.Web.Http.Owin, which was built in .Net 4.5 . Unless, you are ready to rewrite the hosting stuff using Microsoft source code at https://katanaproject.codeplex.com/SourceControl/latest#README