将数据从 Windows 服务反复传输到控制台应用程序
Transfer data from Windows Service to Console Application repeatedly
这是我的场景,我有一个每 20 分钟运行一次任务的 windows 服务,任务是:从远程网站托管的 API 请求更新。
响应是一个JSON个对象的列表,当服务接收到这个列表时,它执行一组操作然后追加更多JSON个对象,最后服务必须将列表推送到运行 控制台应用程序。
我非常具体的问题是:如何将这些数据从windows服务传输到控制台应用程序直接 专业
直接我的意思是没有中间解决方案就像写入临时文件或保存在SQLtable ...等等
专业 我的意思是最好的最佳解决方案,尤其是没有 p/Invoke 从服务到控制台应用程序。
一种漂亮、干净、'modern' 的方法是直接在控制台应用程序中托管 Web API,并接受 JSON 输入。
这相对容易设置,而且非常容易测试和使用。
其他方法包括 .NET 远程处理(不再很现代)、一些其他类型的服务,如 WCF,或众多 windows IPC 方法中的任何一种。
您肯定需要一种媒介来在这两个进程之间进行通信。可以在同一系统上以多种方式进行通信。
根据您在问题中的解释,这看起来像是一种沟通方式。也许您可以通过套接字(原始级别)进行进程间通信或使用消息传递框架进行通信(WCF/SignalR),或者您甚至可以使用消息队列系统(MSMQ/RabbitMQ)等。
如果你能缩小你的问题范围,你会得到一个具体的答案。
我写了一个答案 here,其中有一些适用的代码。基本上,那里的 OP 想要将字符串从 Windows Forms 应用程序发送到控制台应用程序,并让控制台应用程序打印字符串。
我的建议是使用消息队列。
一些简短的说明:首先,如果您从未这样做过,则可能必须 enable the feature in Windows。另外,我想在 Windows 的某些配置下,您不能直接从 C# 创建消息队列;如果是这种情况,您可以手动创建它(或者可能有一种方法可以将其作为安装脚本或其他东西的一部分)。
这是 Windows 表单代码:
private void button1_Click(object sender, EventArgs e)
{
// Or whatever name you end up calling it if you created the queue manually
const string myQueue = ".\myQueue";
// It's possible that this won't work on certain computers
// If not, you'll have to create the queue manually
// You'll also need to turn the Message Queueing feature on in Windows
// See the following for instructions (for Windows 7 and 8): https://technet.microsoft.com/en-us/library/cc730960(v=ws.11).aspx
if (!MessageQueue.Exists(myQueue))
{
MessageQueue.Create(myQueue);
}
using (MessageQueue queue = new MessageQueue(myQueue))
{
queue.Formatter = new XmlMessageFormatter(new[] { typeof(string) });
queue.Send("Test");
}
}
控制台应用程序:
static void Main(string[] args)
{
// Or whatever name you use
const string myQueue = ".\myQueue";
// See my comment on the corresponding line in the Windows Forms application
if (!MessageQueue.Exists(myQueue))
{
MessageQueue.Create(myQueue);
}
MessageQueue queue = new MessageQueue(myQueue);
queue.Formatter = new XmlMessageFormatter(new[] { typeof(string) });
while (true)
{
Message message = queue.Receive();
string messageText = message.Body.ToString();
// Close if we received a message to do so
if (messageText.Trim().ToLower() == "exit")
{
break;
}
else
{
Console.WriteLine(messageText);
}
}
}
这是我的场景,我有一个每 20 分钟运行一次任务的 windows 服务,任务是:从远程网站托管的 API 请求更新。
响应是一个JSON个对象的列表,当服务接收到这个列表时,它执行一组操作然后追加更多JSON个对象,最后服务必须将列表推送到运行 控制台应用程序。
我非常具体的问题是:如何将这些数据从windows服务传输到控制台应用程序直接 专业
直接我的意思是没有中间解决方案就像写入临时文件或保存在SQLtable ...等等
专业 我的意思是最好的最佳解决方案,尤其是没有 p/Invoke 从服务到控制台应用程序。
一种漂亮、干净、'modern' 的方法是直接在控制台应用程序中托管 Web API,并接受 JSON 输入。
这相对容易设置,而且非常容易测试和使用。
其他方法包括 .NET 远程处理(不再很现代)、一些其他类型的服务,如 WCF,或众多 windows IPC 方法中的任何一种。
您肯定需要一种媒介来在这两个进程之间进行通信。可以在同一系统上以多种方式进行通信。
根据您在问题中的解释,这看起来像是一种沟通方式。也许您可以通过套接字(原始级别)进行进程间通信或使用消息传递框架进行通信(WCF/SignalR),或者您甚至可以使用消息队列系统(MSMQ/RabbitMQ)等。
如果你能缩小你的问题范围,你会得到一个具体的答案。
我写了一个答案 here,其中有一些适用的代码。基本上,那里的 OP 想要将字符串从 Windows Forms 应用程序发送到控制台应用程序,并让控制台应用程序打印字符串。
我的建议是使用消息队列。
一些简短的说明:首先,如果您从未这样做过,则可能必须 enable the feature in Windows。另外,我想在 Windows 的某些配置下,您不能直接从 C# 创建消息队列;如果是这种情况,您可以手动创建它(或者可能有一种方法可以将其作为安装脚本或其他东西的一部分)。
这是 Windows 表单代码:
private void button1_Click(object sender, EventArgs e)
{
// Or whatever name you end up calling it if you created the queue manually
const string myQueue = ".\myQueue";
// It's possible that this won't work on certain computers
// If not, you'll have to create the queue manually
// You'll also need to turn the Message Queueing feature on in Windows
// See the following for instructions (for Windows 7 and 8): https://technet.microsoft.com/en-us/library/cc730960(v=ws.11).aspx
if (!MessageQueue.Exists(myQueue))
{
MessageQueue.Create(myQueue);
}
using (MessageQueue queue = new MessageQueue(myQueue))
{
queue.Formatter = new XmlMessageFormatter(new[] { typeof(string) });
queue.Send("Test");
}
}
控制台应用程序:
static void Main(string[] args)
{
// Or whatever name you use
const string myQueue = ".\myQueue";
// See my comment on the corresponding line in the Windows Forms application
if (!MessageQueue.Exists(myQueue))
{
MessageQueue.Create(myQueue);
}
MessageQueue queue = new MessageQueue(myQueue);
queue.Formatter = new XmlMessageFormatter(new[] { typeof(string) });
while (true)
{
Message message = queue.Receive();
string messageText = message.Body.ToString();
// Close if we received a message to do so
if (messageText.Trim().ToLower() == "exit")
{
break;
}
else
{
Console.WriteLine(messageText);
}
}
}