This is a sample on how to work with .NET Core and WebSockets. The sample is about a software licensing process, where the end customer submits a license request to a RESTful API via POST, and retrieves it via GET. The License is signed by a 3rd party software that connects to the current solution via WebSockets. When connected, the solution sends the License Request and receives it back signed.
- RESTful API
- Accepting POST requests with a LicenseRequestModel. As per REST principles, returns a LicenseResponseModel) and a Location header to retrieve the resource.
- Accepting GET requests for the newly created resource that returns a LicenseResponseModel, which contains (or not yet) the Signed License.
- The state of the License (signed or not yet) is singaled with the property Status.
- WebSocket
- A middleware (LicenseSignatureGeneratorMiddleware) is handling WebSocket connections.
- A queue is used to send License Requests, one by one, when the WebSocket client (a hypothetical LicenseSignatureGenerator engine) is connected.
- The WebSocket middlware is dequeueing a message to send and upon receiving a valid message it's storing in using a storage service.
- The WebSocket messages are exchanged with a LicenseMessage model.
- Allowed Origins is in AppSettings (currently allowing all).
- Testing
- Basic Unit Testing for demo persistence layers
- Basic Integration Testing for REST API and WebSocket
- Azure
- Using Azure DevOps to Build, Test and Deploy. Azure DevOps Project is public and availble here.
- Image is pushed in Azure Container Registry
- App is runnig in Azure App Service a the address http://aspnetcore-websockets.azurewebsites.net/ (no SSL installed currently).
- Sample Page (index.html)
- Left column is an end customer sending a License Request (fields are prefilled with data)
- Right column is the 3rd party app (a License Signature App) waiting to connect to the WebSocket
- Usage
- Click 'Submit Data' on the left (only one initially). Long polling starts (every 3 secs), waiting for a signature (Status: 30).
- Click 'Connect' on the right. Upon connecting retrieves all messages awaiting.
- Click 'Send' on the right (a signed license is already prefilled).
- Long polling stops, signature arrived.