转换的Web API使用自托管

编程入门 行业动态 更新时间:2024-10-24 21:36:55
本文介绍了转换的Web API使用自托管的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想现有的ASP.NET Web API项目(目前在IIS托管)转换成一个可以使用SelfHost框架。我在实际的细节有点模糊,但我明白可以在控制台窗口中运行一个自主机服务器,然后在它上面运行服务。我遇到的问题是,我的项目是一个MVC项目,而不是一个控制台之一。我与控制台熟悉/ Windows应用受到一定限制,因为我一般用在IIS上托管的项目。

I am trying to convert an existing ASP.NET Web API project (currently hosted in IIS) into one that can use the SelfHost framework. I'm a bit fuzzy on the actual details but understand I can run a self-host server in a console window and then run the service on top of it. The problem I'm having is that my project is an MVC project and not a console one. My familiarity with console/Windows apps is somewhat limited as I generally work with projects to be hosted in IIS.

我有点困惑什么的是我是否需要将我现有的Web API转换项目在Visual Studio到一个新的控制台应用程序,或者是否有创造一个可以作为行动的解决方案的另一个控制台应用程序项目的方式对于Web API服务的Web服务器,或者更确切地说,如果有一种方法来添加一个控制台元素和一个main()入口点到现有的MVC项目(覆盖在Global.asax切入点。)

What I'm a bit confused on is whether I need to convert my existing Web API project in Visual Studio into a new console application, or if there's a way to create another console application Project in the solution which can act as the web server for the Web API services, or rather if there's a way to add a console element with a Main() entry point to the existing MVC project (overriding the Global.asax entry point.)

搜索并没有产生多少信息,可以帮助我填补这一知识差距。希望有人可以点我在正确的方向。即使在一个较高的水平。

Search didn't yield much information that helps me fill this knowledge gap. Hoping someone can point me in the right direction. Even at a high level.

推荐答案

我最近有一个Web API项目转换为使用OWIN自托管服务(在Visual Studio中2013)。我做了如下:

I recently had to convert a Web API project into a self-hosted service using OWIN (on Visual Studio 2013). I did that as follows:

(1)手动添加的Program.cs和Startup.cs文件在项目的根。含有这里描述code这两个文件:www.asp/web-api/overview/hosting-aspnet-web-api/use-owin-to-self-host-web-api.

(1) Manually added Program.cs and Startup.cs files at the root of the project. Both files containing code as described here: www.asp/web-api/overview/hosting-aspnet-web-api/use-owin-to-self-host-web-api.

(2)又到了网络API项目的属性。在应用程序一节中,我指出,输出类型为控制台应用程序,并设置程序类的启动对象。

(2) Went to the properties of the Web API project. On the "Applications" section, I stated "Output Type" as "Console Application", and set the "Program" class as the "Startup object".

虽然不是必需的,我稍微修改内Program.Main()中的使用块以如下所示:

Although not required, I slightly modified the "using" block within Program.Main() to look as follows:

// Start OWIN host using (WebApp.Start<Startup>(url: baseAddress)) { // Create HttpCient and make a request to api/values HttpClient client = new HttpClient(); var response = client.GetAsync(baseAddress + "api/values").Result; if (response != null) { Console.WriteLine("Information from service: {0}", response.Content.ReadAsStringAsync().Result); } else { Console.WriteLine("ERROR: Impossible to connect to service"); } Console.WriteLine(); Console.WriteLine("Press ENTER to stop the server and close app..."); Console.ReadLine(); }

最后,而不是在Startup.Configuration()调用config.Routes.MapHttpRoute()多次,你可以参考你已经写了Web API的路线:

Finally, instead of calling config.Routes.MapHttpRoute() multiple times within Startup.Configuration(), you can refer to the routes you already wrote for the Web API:

// Configure Web API for self-host. var config = new HttpConfiguration(); WebApiConfig.Register(config); app.UseWebApi(config);

更多推荐

转换的Web API使用自托管

本文发布于:2023-11-15 04:13:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1591048.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Web   API

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!