I created a simple C# console application (net7.0) as a server for my multiplayer card game. It uses HttpListener to receive connection requests, then upgrades to a websocket connection.
// Start listening for websocket requests
public async void Start()
{
HttpListener listener = new();
listener.Prefixes.Add("http://+:80/cardgame/");
listener.Start();
#if DEBUG
Console.WriteLine("Listening...");
#endif
while (true)
{
HttpListenerContext listenerContext = await listener.GetContextAsync();
if (listenerContext.Request.IsWebSocketRequest)
{
ProcessRequest(listenerContext);
}
else
{
listenerContext.Response.StatusCode = 400;
listenerContext.Response.Close();
}
}
}
This works on my windows laptop, when running the app as admin or using the netsh add urlacl url=http://+:80/cardgame user=DOMAIN\user in CMD beforehand.
I want to host this in Azure Cloud as a Webapp using the 'Free' settings. Deployment was successful, but when starting the webapp it throws an access denied exception because of the HttpListener. The exact scenario: Image
Unhandled exception. System.Net.HttpListenerException (5): Access is denied.
at System.Net.HttpListener.SetupV2Config()
at System.Net.HttpListener.Start()
I also tried it with listener.Prefixes.Add("http://+:80/"); and with the public IP of the webapp at the place of the '+' sign, but to no avail.
Azure Kudu environment doesn't run the app in admin mode, but it also doesn't allow the netsh command to be configured, so I'm stating to feel like this is not possible to do... But then what is this webapp environment good for if you can't even listen to a port? (for ASP.NET I suppose...)
I would really appreciate if anybody could help me out with this:
- Is there a way to configure a websocket connection on Azure Webapp environment using a C# console application?
- What other solution would allow me to host my server in cloud this way (preferably free)?
No, it is not possible to configure a WebSocket connection on Azure Web App environment using a C# console application.
Use Azure Web PubSub Service and Azure SignalR as WebSockets in Azure.
The following code creates a WebSocket connection and allows the user to send and receive messages.
Using Azure Web PubSub Service in Blazor:
Local:
Azure:
For Azure SignalR with Blazor :
dotnet add package Microsoft.Azure.SignalRStartup.cs
appsetting.json:
Local: