【问题标题】:Is it possible to use owin to self host WebApi in visual studio 2010 / Framework 4是否可以在 Visual Studio 2010/Framework 4 中使用 owin 自托管 WebApi
【发布时间】:2014-05-14 08:17:20
【问题描述】:

我正在尝试像这里一样使用 owin 运行一个自托管的 Web api 应用程序

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" }; 
    } 

}

哪个事件。

我尝试通过 nuget 安装 Microsoft.AspNet.WebApi (Web Api 2.1),但由于框架 4.5 的要求而失败,所以我安装了 AspNetWebApi (Web Api)。

但是我在任何地方都找不到扩展方法UseWebApi。我是否必须安装另一个软件包,或者是否无法使用 Framework 4 托管 Web api?

【问题讨论】:

  • 我不是专家,我自己才刚刚开始使用 WebApi 和 OWIN,但是由于 Web Api 2 是完全异步的,它是否可以在不依赖 Microsoft.Bcl.Async 的情况下在 .NET 4 上使用?
  • Well WebApi (V1) 还有一个位于System.Web.Http.dll v4.0的ApiController 类
  • WebApi (V1) 可以使用 HttpSelfHostServer (Microsoft.AspNet.WebApi.SelfHost package) 自托管,但我正在寻找 Owin 实现,因为我已经使用 owin 托管了 Nancyfx。

标签: c# .net asp.net-web-api


【解决方案1】:

我不认为您可以使用 OWIN,但是您可以使用 Visual Studio 2010 和 .NET Framework 4.0 使用以下方法创建一个自托管 Web api: http://www.asp.net/web-api/overview/older-versions/self-host-a-web-api

虽然不建议将这种方法用于新设计,但由于我们现有代码库中的依赖性,我不得不使用 .NET Framework 4.0。这篇文章是用 Visual Studio 2012 编写的,但我刚刚用 Visual Studio 2010 测试了服务器端,它似乎工作正常。我使用 Postman 而不是客户端示例。

【讨论】:

    【解决方案2】:

    可能不是因为自托管包依赖于 System.Web.Http.Owin,它是在 .Net 4.5 中构建的。除非,您准备好使用https://katanaproject.codeplex.com/SourceControl/latest#README 的 Microsoft 源代码重写托管内容

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-20
      • 2015-10-06
      相关资源
      最近更新 更多