简体   繁体   English

如何制作连接到WebSocket的C#Windows服务?

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

I'm new to Windows development, coming from macOS and iOS development. 我是Windows开发的新手,来自macOS和iOS开发。

I'm trying to port a Mac software I've made to Windows. 我正在尝试将我制作的Mac软件移植到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. 为此,我需要编写一个Windows服务,该服务将从Windows开始,并负责维护与服务器的WebSocket连接。 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 . 我从C#中的Windows Service项目模板开始,然后尝试使用Windows.Networking.Sockets.MessageWebSocket添加我发现的支持WebSocket的方式。

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; 但是,这会产生一个错误: using Windows.Networking.Sockets; The type or namespace name 'Windows' could not be found (are you missing a using directive or an assembly reference?) using Windows.Networking.Sockets; .

After few google work, I've might understood that Windows.Networking.Sockets might not be accessible from Windows Service template. 经过几次Google工作,我可能已经了解到Windows.Networking.Sockets可能无法从Windows Service模板访问。

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? 如果我不能将此API用于服务,我有什么选择?

Thanks 谢谢

Windows.Networking.Sockets is for the Universal Windows Platform, so it is not compatible with .NET Framework or .NET Core. Windows.Networking.Sockets适用于通用Windows平台,因此与.NET Framework或.NET Core不兼容。

That said, it is unclear from your question whether you are building a .NET Framework or .NET Core Windows service. 就是说,从您的问题中不清楚是要构建.NET Framework还是.NET Core Windows服务。

.NET Framework .NET Framework

For .NET Framework, you can use the System.Net.WebSockets namespace. 对于.NET Framework,可以使用System.Net.WebSockets命名空间。

.NET Core .NET核心

For .NET Core, see Create a websocket server in .net core console application . 对于.NET Core,请参阅在.net Core控制台应用程序中创建Websocket服务器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM