简体   繁体   English

ASP.NET Web API如何自托管?

[英]How feature complete is asp.net web api self-hosting?

I have a requirement that I need to be able to have a stand-alone version of application, as well as an online version. 我要求我必须能够拥有应用程序的独立版本以及在线版本。 One possible way of doing this would be to have a WPF version, which would satisfy stand-alone, and an MVC Web version. 实现此目的的一种可能方法是拥有一个WPF版本(可以满足独立要求)和一个MVC Web版本。

Obviously, that would require two code bases (though admittedly they should be identical except the front end code). 显然,这需要两个代码库(尽管公认,除了前端代码外,它们应该是相同的)。 Is Web API self-hosting stable enough that I could just host a full-blown web app inside of it if I needed to? Web API自托管是否足够稳定,如果需要,我可以在其中托管一个功能完善的Web应用程序?

Web API can run selfhosted but for running ASP.NET (MVC) you require a server like IIS (Express) . Web API可以运行自托管,但要运行ASP.NET(MVC),则需要IIS(Express)之类的服务器。

What you can do is have IIS Express installed on a machine, host your Web app in that and have Web API self hosted if needed. 您可以做的是在计算机上安装IIS Express,在其中托管您的Web应用程序,并在需要时自行托管Web API。 Of course if you already have IIS Express installed you might simply want to opt to run everything in it: both Web app and Web API. 当然,如果您已经安装了IIS Express,则可能只想选择运行其中的所有内容:Web应用程序和Web API。

Provided you're ok with using both internal/external apps using the same database/service you could host a single version of the WebAPI server in IIS, and simply use it for API Controllers. 如果您可以使用相同的数据库/服务使用内部/外部应用程序,则可以在IIS中托管单个版本的WebAPI服务器,并将其简单地用于API控制器。

You could then use two identical ASP.Net MVC sites (rather than Desktop & Web), which make calls into your WebAPI service. 然后,您可以使用两个相同的ASP.Net MVC站点(而不是Desktop&Web站点)来调用您的WebAPI服务。 One hosted on the intranet, one on the internet - both hitting your WebAPI for business functionality & data persistance. 一台托管在Intranet上,一台托管在Internet上-两者都可以通过WebAPI实现业务功能和数据持久性。

The goal here is to reduce the amount of code maintained. 这里的目标是减少所维护的代码量。 Essentially, it's two projects - your WebAPI project, and an ASP.Net MVC site. 本质上,这是两个项目-您的WebAPI项目和一个ASP.Net MVC站点。

I use Web API for self-hosting a number of projects that are in production. 我使用Web API来自动托管生产中的许多项目。 The web site http://www.hypermediaapi.com is a self hosted Web API. 网站http://www.hypermediaapi.com是一个自托管的Web API。

No need to have two or more code bases for this scenario. 对于这种情况,不需要两个或多个代码库。 What you need is a library what provides the API for your applications. 您需要一个为应用程序提供API的库。 The library can be used directly in the standalone application what I assume is a desktop app. 该库可以直接在独立应用程序中使用,我认为这是桌面应用程序。

You can then create a WCF or Web API or even both layer what isn't anything more than a wrapper around the same library. 然后,您可以创建WCF或Web API,甚至可以在这两个层上创建,而不仅仅是围绕同一库的包装。 The WCF/Web API contracts can be the same as your DTOs so the WCF service implementation would be something like this: WCF / Web API合同可以与您的DTO相同,因此WCF服务实现将如下所示:

SomeObject IMyService.DoStuff(string param)
{
  var myLibrary = new MyLibrary();
  var someObject = myLibrary.DoStuff(param);
  return someObject;
}

The only overhead would be that when an interface changes in library the changes have to be repeated in service interfaces too, but no actual business logic would be duplicated. 唯一的开销是,当库中的接口更改时,更改也必须在服务接口中重复,但是不会重复任何实际的业务逻辑。 You can even share the interfaces, ie the API would expose the same interfaces what are implemented by the WCF service if you don't mind having the contract attributes on library interfaces. 您甚至可以共享接口,即,如果您不介意在库接口上具有合同属性,则API将公开与WCF服务实现的接口相同的接口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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