2

I'm working in an application in asp.net/C#, in which I have integrated a third party product (using their supported API).
Now in application's particular Button_Click() event, I want to do three tasks.

  1. First one is simplest, inserting data to sql server and sending the same date to that third party server, its DONE.
  2. Second and complicated one is which I elaborated below.
  3. Getting the status(data) from task2 and doing operation in database.

Here's that, I need to check that third party server for a particular switch. Switch means whether the data is present or not. I'm using a bool getData() method of that API to retrieve the status, it will return ACTIVE/INACTIVE.

But the problem is that getdata() method will take few minutes to verify that switch, like ~2-7 Mins till that the application will not be idle, the user will do anything which is provided in the application.

What I have did is this:

Done the Task1, and made the task2 in separate thread, it works fine for some time so that the third task also does it job.

But threads are not working as expected, if the user does any other operation like reloading the page or anything else, thread gets interrupted and stopped. So both the task2 and task3 fails.

Any other solution for this?

VMAtm
  • 27,943
  • 17
  • 79
  • 125
Mark ben
  • 105
  • 1
  • 8
  • 2
    windows service may be my guess – Balaji Aug 14 '15 at 13:07
  • 3
    Firing off threads in asp.net is never going to be reliable. You probably want to consider another approach such as signalling a windows service to complete the long running task. – Ben Robinson Aug 14 '15 at 13:07
  • yes, but i want to pass the id parameter to check the status, is it possible in windows service? @BenRobinson – Mark ben Aug 14 '15 at 13:09
  • Yes there are various ways, you could add a row to a table in a database that the windows service checks periodically, or you could host some kind of web api in you windows service and call that from your asp.net page/controller. – Ben Robinson Aug 14 '15 at 13:13

1 Answers1

1

As any other web-oriented architecture, ASP.NET is a completely multi-threaded oriented environment, and all requests from client are being handled by a new (or pooled) Thread, and all the background or children threads being created during such handle are destroyed (aborted) after the request handle is done.

So you can't be sure that the operation in background thread will complete. As in comments already been said, you have to create a place which will run all needed work and provide results back to the client. It is very easy to setup a windows service as a WCF end point (google can provide you a dozens of links), but I suggest you to deep analysis for what exactly type of binding do you need, as there are many variants, and I think that TCP or HTTP binding don't fit your needs.

I suggest you to chose a named pipes as your web-application and window service are both .NET applications. SO already have questions regarding that, for example, you can see this one: WCF named pipe minimal example

Community
  • 1
  • 1
VMAtm
  • 27,943
  • 17
  • 79
  • 125