ygini
ygini

Reputation: 135

How to make a C# Windows Service that connect to a WebSocket?

I'm new to Windows development, coming from macOS and iOS development.

I'm trying to port a Mac software I've made to Windows.

To do that, I need to write a Windows Service that will start with Windows and will be in charge to maintain a WebSocket connection to my server. It's basically an custom push notification service.

On demand from the server, the service will start an update mechanism of a local database that will be read by another app later. It need to be on the system side, not the user side, since the product goal is to somehow manage some system features.

I've started with the Windows Service project template in C# and then tried to add what I found to be the way to support WebSocket, with Windows.Networking.Sockets.MessageWebSocket.

However, this generate an error: The type or namespace name 'Windows' could not be found (are you missing a using directive or an assembly reference?) for the line using Windows.Networking.Sockets;.

After few google work, I've might understood that Windows.Networking.Sockets might not be accessible from Windows Service template.

Since I'm not sure I'm here to ask what's the starting point here to achieve such a goal. Why did I get this error? If it's a project misconfiguration, what's the fix? If I can't use this API for a service, what's the options I have?

Thanks

Upvotes: 1

Views: 4729

Answers (1)

NightOwl888
NightOwl888

Reputation: 56849

Windows.Networking.Sockets is for the Universal Windows Platform, so it is not compatible with .NET Framework or .NET Core.

That said, it is unclear from your question whether you are building a .NET Framework or .NET Core Windows service.

.NET Framework

For .NET Framework, you can use the System.Net.WebSockets namespace.

.NET Core

For .NET Core, see Create a websocket server in .net core console application.

Upvotes: 3

Related Questions