简体   繁体   中英

Is it possible to use owin to self host WebApi in visual studio 2010 / Framework 4

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?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM