简体   繁体   English

Windows Service或Asp.net Web API RESTful服务?

[英]Windows Service or Asp.net web api RESTful service?

I'm developing a C# solution with .NET Framework 4.5.1. 我正在使用.NET Framework 4.5.1开发C#解决方案。

I need to have a program (an object instance) running all the time waiting to receive commands. 我需要一直在运行一个程序(一个对象实例)以等待接收命令。 This program will have a state and depending on this state it will allow some commands or others. 该程序将具有一个状态,并根据该状态将允许某些命令或其他命令。

For example, it will have an open and close command. 例如,它将具有打开和关闭命令。 If the state is not openned and the program receives the close command, it won't do anything. 如果状态未打开,并且程序收到关闭命令,则它将不执行任何操作。

I've thought to create a windows service or a ASP.NET MVC WebApi 2.2 app. 我曾考虑过创建Windows服务或ASP.NET MVC WebApi 2.2应用程序。

If I choose windows service, the program will be running always but I don't know how to communicate with it (maybe adding it a TCP/IP server). 如果我选择Windows服务,则该程序将始终运行,但我不知道如何与之通信(可能将其添加到TCP / IP服务器)。

If I use ASP.NET MVC WebApi 2.2 I can send the commands using POST or PUT messages, but I don't know if I can have an object instance running while the web service is up. 如果使用ASP.NET MVC WebApi 2.2,则可以使用POST或PUT消息发送命令,但是我不知道在Web服务启动时是否可以运行对象实例。

The program is a class that I have to instanced it. 该程序是我必须实例化的类。 This instance will keep its state and a communication with a WCF service. 该实例将保持其状态并与WCF服务进行通信。

What do you recommend me? 你推荐我什么?

Options from which to choose are strange a little. 从中选择的选项有些奇怪。

You can self-host Web API in windows service, and communicate with service using HTTP. 您可以在Windows服务中自托管Web API,并使用HTTP与服务进行通信。 Note, that Web API is about stateless, so, if you need to preserve the state, this should be a persistable data. 请注意,Web API是关于无状态的,因此,如果需要保留状态,则该数据应该是可持久的数据。

As to a way for communicate with windows service, using something other, than Web API - there are number of ways to do this. 至于与Windows服务进行通信的方式,可以使用Web API之外的其他方式-有许多方法可以做到这一点。 WCF with TCP/Named Pipes, pure Named Pipes (without WCF), sockets, memory mapped files. 具有TCP /命名管道,纯命名管道(无WCF),套接字,内存映射文件的WCF。 You just need to select most appropriate for your requirements. 您只需要选择最适合您的要求。

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

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