Search code examples
c#asp.net-coredesktop-application

How to exchange data between ASP.NET and Desktop app?


I have (maybe not, ups) simple question about exchange data between that two platforms: ASP.NET and desktop app (best choose for my idea is WinForm, but can be console too).

My project in my mind:

  1. ASP.NET Core MVC app
  2. Desktop app for share COM ports

I have embedded system [STM32] on Virtual COM so I wanna only send/get data from this and send/get orders from server

Send orders via POST/GET etc. is easy, I know.. But! I don't have idea how to send orders to my desktop app, without user interferences.

I only open this app in background and what I say "only for share port"


Solution

  • There are numerous alternatives that you could use to accomplish this. As a general field, this is called Interprocess Communication (IPC).

    From all approaches, I find embedding a web server into a Winforms application the most flexible and easier solition but please, bear in mind that it's my personal choice. I'll try giving you other possible solutions on this post, though:

    1. Embedd a server within your Winforms application

    This is my personal choice. You can digg into this topic by searching more about embedding a HTTP server into Winforms but here are some links that might give a direction on this:

    Using this approach you'll end up with a full AspNet application and a lightweight web app that integrated directly with desktop code. You'll integrate those two as if you're integrating two web servers: using HTTP calls and more.

    2 - Named pipes & TCP

    Named pipes are very interesting but you should carefully choose between them or TCP communication as each has it's own pros and cons. You can find an example of Names Pipe / TCP here: https://github.com/jacqueskang/IpcServiceFramework

    For more information about named pipe vs tcp pros and cons, I've found this post to be really useful: http://saurabhsinhainblogs.blogspot.com/2015/09/what-is-difference-between-tcp-ip-and-Named-Pipes.html

    3 - Message queues

    It's like a silver bullet. A huge and heavy silver bullet but still, a silver bullet. This works fine with, well, pretty much any type of interprocess communication as you'll have a third player in charge of receiving and trasmitting messages and both players (server / client) will listen or publish to this queue. You can check NetMQ, RabbitMQ, MSMQ, RestBus, ActiveMQ, ZeroMQ or you can even build your own based on SQL Server for instance (although I'd not recommend that unless you're really sure of what you're doing).

    4 - Others

    You have sockets, files (with automatic monitoring physical file system) and many other possible approaches to perform integration without user interaction. Even if I put here 20 other approaches, I'd likely be missing 20 others. Just search for AspNet Core Interprocess Communication and you'll find plenty resources about that :)

    Some other useful resources:

    This post has a reliable compilation of liks that will help on each IPC approach you might wish to test: https://weblogs.asp.net/ricardoperes/local-machine-interprocess-communication-with-net