Moving a time taking process away from my asp.net

2019-05-19 23:17发布

My Asp.net application generates a dynamic pdf. Sometimes this takes a while and is a quite heavy process. Actually i dont want my users to wait for the pdf, just send it to there mail after it generated.

So I tried a webservice. I'm passing an id (to get the data from the database) and some strings to the websercice's method.

But also with a webservice (even with asynchronous calls) the client only receives its response after the pdf is generated. So the user still has to wait.

So I'm kinda stuck, there must be a way i'm overlooking.

3条回答
放我归山
2楼-- · 2019-05-19 23:37

there are two ways which i know

First ways; In asp.net code behind (in xxx.aspx.cs file) you can define a void method then you can call the method by starting a thread like below.

protected void SenMail(object prms)
{
    int id = int.Parse(prms.ToString());
    //mail sending proces
}


//starting SendMail method asynchronous
Thread trd = new Thread(SenMail);
trd.Start(idValue);

Second way;

You can create and mail sender page like "SendMail.aspx", then you can make an ajax request in javascript and no need to wait any response. you can pass id value to aspx page as request parameter.

查看更多
欢心
3楼-- · 2019-05-19 23:51

Issue is that in your ASP.NET page code, you must be invoking the web service synchronously so the page waits till web service returns. You should try invoking the web service asynchronously (or on the different thread) and then don't wait for it to complete. Typically, visual studio generated proxy already has asynchronous overloads that you may use.

Alternately, you may modify your web service code - essentially, when request to your web method comes, you can start PDF generating on a different thread so that your web method may end indicating your client (page in this case) that request has been successfully scheduled for processing.

查看更多
Lonely孤独者°
4楼-- · 2019-05-19 23:58

You don't need a webservice in order to get the ability to make asynchronous invocations.

You can just use ThreadPool.QueueUserWorkItem() as a fire-and-forget approach in the ASPX page, then return a reply with some sort of "work item id" - like a receipt or an order number.

Generate the PDF in the WaitCallback you pass to QUWI. when the pdf is ready, that WaitCallback can send an email, or whatever.


Use a webservice if you want the function to be accessible, outside the webpage. Don't use it strictly for asynchrony.

查看更多
登录 后发表回答