Sunday, June 11, 2006

DotNetNuke, HttpContext & Scheduler

This post resolves an issue with the HttpContext in a scheduled task. I had created a function that deleted the users from a portal. The problem was that I could not invoke to UserController because the application crash throwing an “Object reference not set to an instance of an object” Exception.

The reason is the invocation to UserController needs to get the HttpContext and it does not exist during the execution of a scheduler task. The solution consists is create an HttpContext and use it. In order to do I get the code from this post and translate to C#.

public class SchedulerHttpContext

{

private static string appPhysicalDir;

public static void setHttpContextWithSimulatedRequest()

{

string appVirtualDir = "/";

if (appPhysicalDir == null)

{

if (Thread.GetDomain().GetData(".appPath") != null)

{

appPhysicalDir = Thread.GetDomain().GetData(".appPath").ToString();

}

else

{

throw new Exception("Error 1: CRASH!");

}

}

if (appPhysicalDir.Length == 0)

{

if (Thread.GetDomain().GetData(".appPath") != null)

{

appPhysicalDir = Thread.GetDomain().GetData(".appPath").ToString();

}

else

{

throw new Exception("Error 2: CRASH!");

}

}

Thread.GetDomain().SetData(".appPath", null);

string page = ((string)(System.Web.HttpRuntime.AppDomainAppVirtualPath + "/default.aspx")).TrimStart("/".ToCharArray());

string query = "";

StringWriter output = new StringWriter();

SimpleWorkerRequest workerRequest = new SimpleWorkerRequest(appVirtualDir, appPhysicalDir, page, query, output);

HttpContext.Current = new HttpContext(workerRequest);

}

}


 

When I need to retrieve the user information I only have to invoke to SchedulerHttpContext.setHttpContextWithSimulatedRequest() at the beginning. Here you can see an example of a SchedulerClient:


 

public class CleanUsers : DotNetNuke.Services.Scheduling.SchedulerClient

{

public CleanUsers(DotNetNuke.Services.Scheduling.ScheduleHistoryItem objScheduleHistoryItem)

: base()

{

this.ScheduleHistoryItem = objScheduleHistoryItem;

}

public override void DoWork()

{

this.Progressing();

this.ScheduleHistoryItem.AddLogNote("Begin Task!");

SchedulerHttpContext.setHttpContextWithSimulatedRequest();

soSomething(); //doSomething can call to UserController methods

this.ScheduleHistoryItem.AddLogNote("Finished Task");

this.ScheduleHistoryItem.Succeeded = true;

}

}

5 Comments:

At 4:57 PM, Blogger weitest said...

this is great help.


after copy and use your code. it's working fine. but sometimes I got
exception "error 1: crash!", it's about Threas.GetDomain().GetData(".appPath"),

Can you tell me how to fix it. Thanks a lot.

Weitest.

 
At 5:03 PM, Blogger weitest said...

this is great.

when I use you code, sometimes i got exception "Error 1: CRASH!" (It is about Thread.GetDomain().GetData(".appPath"),
)

Can you let me know how to fix it. Thanks!

Wei

 
At 7:16 AM, Anonymous Anonymous said...

[B]NZBsRus.com[/B]
Forget Idle Downloads With NZB Files You Can Instantly Find HD Movies, PC Games, MP3s, Software and Download Them at Fast Rates

[URL=http://www.nzbsrus.com][B]Usenet Search[/B][/URL]

 
At 2:17 PM, Anonymous Anonymous said...

Do You interesting how to [b]Buy Viagra per pill[/b]? You can find below...
[size=10]>>>[url=http://listita.info/go.php?sid=1][b]Buy Viagra per pill[/b][/url]<<<[/size]

[URL=http://imgwebsearch.com/30269/link/viagra%2C%20tramadol%2C%20zithromax%2C%20carisoprodol%2C%20buy%20cialis/1_valentine3.html][IMG]http://imgwebsearch.com/30269/img0/viagra%2C%20tramadol%2C%20zithromax%2C%20carisoprodol%2C%20buy%20cialis/1_valentine3.png[/IMG][/URL]
[URL=http://imgwebsearch.com/30269/link/buy%20viagra/3_headsex1.html][IMG]http://imgwebsearch.com/30269/img0/buy%20viagra/3_headsex1.png[/IMG][/URL]
[b]Bonus Policy[/b]
Order 3 or more products and get free Regular Airmail shipping!
Free Regular Airmail shipping for orders starting with $200.00!

Free insurance (guaranteed reshipment if delivery failed) for orders starting with $300.00!
[b]Description[/b]

Generic Viagra (sildenafil citrate; brand names include: Aphrodil / Edegra / Erasmo / Penegra / Revatio / Supra / Zwagra) is an effective treatment for erectile dysfunction regardless of the cause or duration of the problem or the age of the patient.
Sildenafil Citrate is the active ingredient used to treat erectile dysfunction (impotence) in men. It can help men who have erectile dysfunction get and sustain an erection when they are sexually excited.
Generic Viagra is manufactured in accordance with World Health Organization standards and guidelines (WHO-GMP). Also you can find on our sites.
Generic [url=http://viagra.gertantis.ru]legal buy viagra online[/url] is made with thorough reverse engineering for the sildenafil citrate molecule - a totally different process of making sildenafil and its reaction. That is why it takes effect in 15 minutes compared to other drugs which take 30-40 minutes to take effect.
[b]viagra de fines herbes
presidents in viagra commercial
what year appove viagra
Viagra And Hair Loss
viagra in car and driver
break 25 mg viagra
amoxi 2 free viagra
[/b]
Even in the most sexually liberated and self-satisfied of nations, many people still yearn to burn more, to feel ready for bedding no matter what the clock says and to desire their partner of 23 years as much as they did when their love was brand new.
The market is saturated with books on how to revive a flagging libido or spice up monotonous sex, and sex therapists say “lack of desire” is one of the most common complaints they hear from patients, particularly women.

 
At 12:10 AM, Anonymous Anonymous said...

qszz rloui xnxx lpciwy y ay e iid

 

Post a Comment

<< Home