0

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

ygini
  • 105
  • 1
  • 7

1 Answers1

2

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.

NightOwl888
  • 55,572
  • 24
  • 139
  • 212
  • I'm not sure either. It's for Windows 10 and it need to be a service that use websocket, rest API, JSON and local files. So if .NET Framework is compatible with it, I go for it, I'm not sure if it's the default setup for Services templates on Visual Studio. – ygini Jan 21 '18 at 09:40
  • OK, I can confirm that I'm able to import `System.Net.WebSockets` with the Service template, this is a good starting point. Thanks for your answer. Coming from outside of the Windows world, it's not yet obvious for me this system of multiple and incompatible version of the frameworks – ygini Jan 21 '18 at 09:48
  • Please note that this answer solved my question but my score level here does not allow me to mark it as solved – ygini Jan 21 '18 at 09:48
  • Apparently, [creating Windows Services are not supported directly in .NET Core](https://stackify.com/creating-net-core-windows-services/). However, the advice in the link is probably still worth considering if you want your code to be portable across frameworks. – NightOwl888 Jan 21 '18 at 09:48
  • what's the point of using .Net Core if Framework does the job? I'm writing for Windows 10 only. – ygini Jan 21 '18 at 10:18