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

对着背影说爱祢 提交于 2020-01-01 03:48:06

问题


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?


回答1:


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.




回答2:


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.




回答3:


WCF would work and you may also want to look at consuming / writing a web Service but depending on if you want Sync or Async seem more like the question ..



来源:https://stackoverflow.com/questions/8635851/two-way-communication-between-asp-net-web-app-and-c-sharp-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!