简体   繁体   中英

Two-way communication between ASP.NET Web App and C# Application

I need to build a website and an application that communicate together, in both directions. I will be building the website with ASP.NET, and the application in C#.

I will be hosting the website myself, and it will be running on the same machine as the application.

I don't know what's the best technique to use to send data between the two. The C# app will need to be running all the time. Should I build a C# Console App, and then hide the console window? Or would some other kind of app be more appropriate?

I've been looking round the Web and found several different suggestions, including Sockets, Message Queues, Remoting and WCF. A few pointers would be much appreciated - I'm new to all of this.

Thank you.

EDIT The request-response pattern will be used, with the Web App always being the one instantiating the requests. This is what I meant by two-way communication.

The web app will send a request to the back-end app, the back-end app will perform some processing, and then send a response back to the web app. JSON will be used to send data to and fro.

I will be using SQL Server Express 2008 R2, and the back-end app will be the only one communicating with the database. The web app will mostly be concerned with the Presentation Layer.

The back-end app will have in-memory objects that are instantiated when the app is started (with data loaded from the DB), and then persisted to the DB (during execution and before closing). Will a C# Console App be ideal for this kind of thing?

What you describe in your comment is typical three-tier application.

  • Front-end: ASP.NET application hosted in IIS
  • Back-end: .NET application running as Windows service (or WCF application hosted in IIS/WAS) with exposed web services. Front-end application communicates with this application using web services (or remoting).
  • Database: Accessed only by Back-end application.

Just to make it clear. Two-way has many meanings but common in this scenarios is that front-end makes call to back-end and back-end respond (request-response pattern). Back-end never calls front-end - such communication is very hardly achievable with ASP.NET application.

ASP.NET works on per request basis. Client calls your ASP.NET application and there is processing of the client request = that's where all logic runs. When the request is processed, the processing ends. So calling your ASP.NET application from back-end doesn't fit this unless you expose some special web service for back-end processing but even after that it will not correlate with your clients request processing without some internal state hold in ASP.NET application.

WCF might be a choice. You can build what kind of connection you need with WCF.

Also, I reccomend you to take a look at SignalR . It is a project which enables you to build a persistent connection between your web app and client app. (your C# app in this case.). In official words, SignalR is an async signaling library for ASP.NET to help build real-time, multi-user interactive web applications.

WCF可以工作,您可能还希望研究使用/编写Web服务,但是取决于您是希望Sync还是Async看起来更像是一个问题。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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