
NET clients that can consume data from our Unix Socket-based backend.
DOCKER UNIX DOMAIN SOCKET HOW TO
In this section, you will learn how to build. With the command line tool curl we just saw one of many possibilities to request data from our server application. The right terminal window shows a client request using the curl command curl -w "\n" -unix-socket /tmp/foo.sock [ The output represents the expected response from our server application.Ĭonsuming an API using Unix Domain Sockets The left terminal window shows the running ASP.NET application executed via dotnet run, confirming that we are serving the API using the defined Unix Socket.
DOCKER UNIX DOMAIN SOCKET CODE
The screenshot below shows the previously presented sample executed within a Visual Studio Code WSL 2 remote session. The remaining parts of the snippet follow the common syntax when creating minimal APIs using. After some sanity checks, Kestrel is advised to use the defined socket file via ListenUnixSocket With this simple line of code all pre-defined registrations of TCP/IP stack-related settings are replaced.

Within this code extract, the constant UnixSocketPath defines the path that is used to create the socket on the file system.

Then we replace the entire content of the Program.cs file using the next code snippet. To get started with our first server application we create a new minimal API using the dotnet CLI: dotnet new webapi -minimal -o SocketDemo We will look at some practical use cases later in this article.īut for now, we will walk through server and client application code which demonstrates how to work with Unix Sockets. With a few lines of code, you can build and run an API that uses Unix Domain Sockets instead of TCP/IP Sockets based on ASP.NET. Adjusting the configuration via the well-designed interface is very easy. That makes sense because APIs are usually created for communication between applications over a network.īut it’s just the default. By default, Kestrel is set up based on a TCP/IP Socket using the parameters specified within different environment variables, for example using ASPNETCORE_URLS for interface and port bindings. NET Minimal API over Unix SocketsĪpplications based on ASP.NET are served via the build-in cross-platform web server named Kestrel. That’s why they are not as fast and lightweight as Unix Domain Sockets. Because of the purpose of inter-machine communication, TCP/IP sockets have to consider operations like routing, among others. By using the loopback interface, communication can take place on the same machine too. In contrast, TCP/IP sockets allow communication between applications over a network. When applications are running on the same Unix-based host, Unix Domain Sockets should be preferred because they are more lightweight and faster than TCP/IP sockets. Hence, access can be controlled via file system permissions and communication is limited to applications running on the same machine. Like almost everything on a Unix-based operating system, Unix Sockets are file-based. Use cases at the end of the article clarify the usage based on practical examples.īefore diving into code samples, let’s have a brief look at Unix Domain Sockets (or Unix Socket) and how they differ from a TCP/IP socket.Ī Unix Domain Socket is an inter-process communication mechanism that allows bidirectional data exchange between multiple applications. NET applications to serve and consume APIs based on Unix Domain Sockets. If you are faced with this type of scenario, Unix Domain Sockets might be a suitable alternative to the default behavior. In some cases, more performant solutions exist, especially when applications need to exchange data and run on the same machine. Have you already thought about changing this behavior?

That makes sense due to APIs interacting over a network regularly. APIs built on ASP.NET typically serve the app via TCP/IP using a specific interface and port. NET became easier with the evolution towards. Creating cross-platform capable APIs using.
