Promoting SoC Through Application Layering

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...

Using OpenAPI Service References in .NET 8

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.

Visual Studio 2022 Project Screenshot

When creating the WebAPI project, make sure Enable OpenAPI support is checked.

Visual Studio 2022 WebAPI Template Screenshot

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.

Swagger.json Screenshot

Once complete, the project should look something like this:

Visual Studio OpenAPI .json File Screenshot

The next step is to configure the Service Reference in OpenAPIServiceReferenceDemo. Right click the OpenAPIServiceReferenceDemo project, click Add, then click Service Reference.

Visual Studio Add Service Reference Screenshot

Configure the OpenAPI Service Reference as follows:

Visual Studio Add new OpenAPI Configuration Screenshot

Adding the Service Reference will add an OpenApiReference section to the .csproj file, it should look something like:

<ItemGroup>
	<OpenApiReference Include="WidgetWebAPI\WidgetWebAPI.json" CodeGenerator="NSwagCSharp" Namespace="OpenAPIServiceReferenceDemo.WidgetWebAPI" ClassName="WidgetWebAPI" >
		<NSwagGenerateExceptionClasses>true</NSwagGenerateExceptionClasses>
	</OpenAPIServiceReferenceDemo>
</ItemGroup>

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.

Visual Studio Multiple Startup Project Configuration Screenshot

At this point, we can run the WebAPI and UI and see the results.

Application Screenshot

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.

Source code: https://github.com/jharrell-bits/OpenAPIServiceReferenceDemo