Creating a layered design in an application is a fundamental element of modern software architecture. The goal is to promote the Separation of Concerns (SoC) design principle. Separation of Concerns The ideas behind SoC date back to Dijkstra's 1974 paper "On the role of scientific thought" . In computer science, separation of concerns (sometimes abbreviated as SoC) is a design principle for separating a computer program into distinct sections. Each section addresses a separate concern, a set of information that affects the code of a computer program. A concern can be as general as "the details of the hardware for an application", or as specific as "the name of which class to instantiate". A program that embodies SoC well is called a modular program. Modularity, and hence separation of concerns, is achieved by encapsulating information inside a section of code that has a well-defined interface. - Wikipedia SoC is a broad design principal th...
Accessing WebAPIs that support OpenAPI is easy with Visual Studio 2022 and ASP.NET. Let's walk through a simple application.
This solution consists of two projects, WidgetWebAPI (WebAPI) and OpenAPIServiceReferenceDemo (UI). We want to configure the UI to call the WebAPI and display the results.
When creating the WebAPI project, make sure Enable OpenAPI support is checked.
At this point, set the startup project to be WidgetWebAPI, then run it in IIS Express. We'll need to do two things once the WebAPI is running. The first thing that we need to do it take a note of the port number the WebAPI site is running under (we'll need that for the UI client to call the WebAPI later). The second thing we must do is save a copy of the swagger.json file for the UI project to use. Copy the swagger.json file from the browser to OpenAPIServiceReferenceDemo/WidgetWebAPI/WidgetWebAPI.json.
Once complete, the project should look something like this:
The next step is to configure the Service Reference in OpenAPIServiceReferenceDemo. Right click the OpenAPIServiceReferenceDemo project, click Add, then click Service Reference.
Configure the OpenAPI Service Reference as follows:
Adding the Service Reference will add an OpenApiReference section to the .csproj file, it should look something like:
Note: The NSwagGenerateExceptionClasses tag was manually added to the .csproj file, this is required when adding more than one OpenAPI Service Reference to your project.
At this point, we can call the WebAPI from our UI project. To call the WebAPI, we need the base URL (including the port number we noted earlier) and an HttpClient. To keep things simple, we'll just hardcode the base URL and use the default injected HttpClient. Here is sample code from Index.cshtml.cs:
public List<Widget> Widgets { get; set; } = new List<Widget>();
public async Task OnGetAsync()
{
var widgetAPI = new WidgetWebAPI("https://localhost:44325", _httpClient);
Widgets = (await widgetAPI.GetWidgetsAsync()).ToList();
}
Since we're using IIS Express, we need to configure the solution to run both projects when we start debugging. To do this, right click the solution, click "Configure Startup Projects..." and select both the UI and WebAPI to start up as below.
At this point, we can run the WebAPI and UI and see the results.
This example was created using Visual Studio 2022 running an ASP.NET Core Web App project and an ASP.NET Web API project using IIS Express.