I have a web application running on ASP.NET and using SignalR I support real-time communication between the clients and the server.
I need some type of a worker process that does work in a loop and notifies the clients as needed using the SignalR connections and the web application must be able to call the worker process. (two-way communication between a worker process and the web application)
What would you recommend for communicating between the web application and the worker ?
A scenario would be:
The request comes in from the client -> I notify the worker -> the worker does his job -> calls back to the web application -> notifies the client.
or
The worker notifies -> web application, uses the SignalR to notify -> clients
Can I share instances somehow ? Can I run this worker in the same application domain inside IIS ? How might I approach this ?
Thank you !
Usually I do communication between ASP.NET and another process (a scheduled console application in most of my cases) through a database.
I.e. the ASP.NET application writes some jobs into a jobs table and the console application (which is being ran every 10 minutes or so through Windows Task Scheduler) picks up the jobs from the database table and processes them.
You could use an asynchronous IIS-hosted WCF or Web Service where the request is initiated from the client and the response is handled in the request completion callback.
Since IIS could instantiate a separate instance of your service class, this class could call the appropriate worker directly since the user won't be directly waiting on the response.
Related
I have a question, I have been following this post:
IoT core background webserver
to build my background webserver. However, this post along with most that I read are just a headless background service. In my case, I do have a main app with a UI as well that the user can view and interact with. So, I am not headless, I want the app to start and show the UI when the device starts up. But, I also want a web server running in the background at all times (from within the app) to listen for JSON messages to be posted. For example, if I have text on the UI that I want to change by sending a JSON message to the device/app.
I understand that this is typically referred to as an in-process web server. So, I took most of the link above to implement the web server class and then from my MainPage.xaml.cs code behind, I did this to instantiate the web server class on another thread to listen for messages:
private async void StartServer()
{
var webServer = new WebServer();
await ThreadPool.RunAsync((workItem) =>
{
webServer.Start();
});
}
Is this the right way to start the web server in a background thread? As I read the code (and be easy on me, as I am still learning all of this stuff), it seems that the web service will start in the thread pool but then stop after it receives one message. Is that right or will it continue to run indefinitely (or as long as the app is running)?
Thanks!
Yes,it is a right way to start your web server in a background thread in headed UWP Application.In the thread pool,the web service will not stop after it receives messages,because the object of WebServer is not disposed.
Firstly, please refer to the Object Lifetime in .Net Framework.This topic is also about the life cycle of an object. The webServer object will not be disposed until the application exits.
Secondly, HTTP 1.1 implementa persistent connections.In the code you referred,each connection will be closed after web server responses to the client(web browser). So a separate TCP connection will be established to fetch each request in this web server.
var header = $"HTTP/1.1 200 OK\r\nContent-Length: {bodyStream.Length}\r\nConnection: close\r\n\r\n";
I have developed a WCF service for serving our customers and hosted it on IIS. We have a requirement to log all the requests received and responses sent from WCF in to a database.
But, because of this logging, we don't want to interrupt main flow of requests and responses. So, we are using threads (Threading.Thread and Thread.IsBackground = true) to call procedures to insert/log the requests and responses to database.
I just want to know if there will be problems in implementing/invoking threads on a WCF service. If so, what will be a good solution for this?
Yes, there can be a problem. The application pool in IIS can get recycled which means that the background thread will be killed, even if it's in the middle of some processing.
In reality that will only be a problem when you update your application (as the logger should be done when the app pool is stopped due to the idle timeout).
So if you can live with lost log entries during updates you do not have a problem.
I have some legacy ASMX IIS hosted service. Client applications make subscribe or unsubscribe to the web service. Via some internal to the web service logic it needs to send messages to the subscribed applications periodically.
What is the best way to do the part of the long running task ? I understand opening Thread with long running task not a good idea to do under IIS.
ASMX services cannot do what you're asking for: they cannot just decide to send a message to the client. All they can do is respond if the client requests it.
You can hack around and come up with one method to start the long-running task, and another method to poll for the status of the task. This works, but it can be expensive.
The better model is to perform the long-running task in a separate Windows Service. Have that service host a simple WCF service which will only be used by the main service (the one that talks to the clients). The main (WCF) service would use a Duplex channel to communicate with the clients. That way, it can "call" the clients whenever there is news about one of the long-running tasks.
Usually in such cases when you don't have a way to push the result back, create an unique ID for the long running task and sent it back to the client, after that run the task and have a table in database or something else where you store the status of the task. The client will pull periodically the service to see the task' status by given ID. Once it finds the task is completed it will retrieve the result.
And is completely fine to have a thread running inside IIS doing its job.
I am building a WCF service that will expose several operations, it will run in IIS because it needs HTTPS endpoints. Most of the operations will perform within seconds or less; however, one or two of these operations will take between 5-90 minutes.
The primary consumer of this service will be an ASP.NET MVC application; what is the correct way to do handle this?
Should I jackup the timeout and do some ajax calls? Should I add a table to my database, and have the long running operations update this database, and have the web interface poll this table every minute? I'm not sure what (if there is) the generally accepted best practice for this.
I wrote something similar for my senior project, basically a job scheduling framework.
I chose to go down the path of storing the "status" of the "job" in the database.
I wrote a manager windows service that implemented a WCF client (proxy)
I wrote a WCF Service that implemented my "worker host".
The manager service would read the queue from the database, and hand out work to all of my "worker hosts". The reason I had windows service perform this task as opposed to just having the UI talk directly to the worker host, was because it gave an extra level of control over the whole process.
I didn't like the idea of having "the network cable unplugged" from my worker host, and never getting a status update again from this specific job. So, the windows service gives me the ability to constantly monitor the progress of the WCF worker host, and if a connection error ever occurs (or something else unexpected), I can update the status to failed. Thus, no orphaned jobs.
Take a look at this
WCF Long Running Operations
There could be other options but they are nearly the same. You can also come up with some push notifications (I assume no data is returned) as one int the following link
WCF Push
I am currently developing a C# Windows Form Application that I intend to let it interact with a server. The server will receive posting from a mobile application that I have developed and whenever a posting is received, my Windows Form Application should be notified and give me a notification. In order to do this, I intend to use WCF duplex service for it.
E.g. My mobile application sends an posting over to my server. Once my server reads and receives the new posting, the service should send a message over to my winform app to alert me that a posting is received. And the UI of the winform app should update accordingly to what I want to updated. (e.g. adding new panels)
This is basically how I wish for it to work
They way this would work is
WCF Service in running on my server
Windows Form connects to my server's WCF service using Duplex Contract
Mobile app posts to a webpage
Once the webpage receives the posting, the asp.net will invoke the WCF service
WCF duplex service receives the posting and sends the information to the winform app
My winform Application aka WCF Client updates UI with this new message received
My question is, how does step 4 proceed to step 5? To be specific, how does the service sends the information over to the winform app upon receiving the posting.
To be even more specific, once the posting is received from the webpage, the service contract is invoked and the information is sent and received by the service, how does the service make use of the call back channel to send the information over to the winform app and update the UI accordingly?
The answer to this question depends on how your WCF service is hosted and how "big" the service will eventually be (in terms of number of simultaneous clients).
The simplest scenario is a self-hosted WCF service (meaning hosted in a Windows Service or as a desktop application--not in IIS). In this case, you can use InstancePerSession mode and make your service use sessions. In this case, you'll have a 1:1 correspondence between clients and instances of your service class. When a client connects, retrieve the callback reference and store it in a static list outside of the service class. When you need to send a message to one or more clients, simply iterate over (or find the desired client in) your list and call the appropriate function on the callback contract
If you need to host your service in IIS, then the situation is trickier because you have the possibility of multiple processes hosting your service, so your list can potentially get fragmented (or blown away in the event of an app pool recycle). In this case, you'll have to use something external to your service (MSMQ, perhaps) to notify other application pool processes that a message needs to be sent.
In terms of a duplex connection, you are really just able to communicate two way over that one connection, not with all connections of the service without doing some tricky thread stuff and shutting the door on any scalability (or using something outside the service to handle to pub/sub).
One solution though that may work a lot more along the lines of what you want to do would be SignalR. It allows a single client to make a request and then you can broadcast data from that request to other clients (or target it). Take a look at its info, its sole purpose is real time communication in .NET with multiple clients.
Also another note, is that you will want to use some sort of BackgroundWorker or something for your listening thread in WinForms so that the UI is not locked while the background operations are running.