Bill
Bill

Reputation: 83

How do I use a SQL CLR Method to call a Webpage?

I have an asp.net mvc application that take a while to load on my production server. I would like to write a script to call my pages every 10 minutes to avoid the pages from being scrapped on the server, which would then cause the server to reload them.

I was thinking of using a SQL Server stored procedure to call my pages every 10 minutes to keep the pages alive.

I've read that I can do this using CLR, but I am not sure how. Does anyone have an example of how to call webpages in a SQL Stored Procedure using CLR?

Upvotes: 0

Views: 1056

Answers (3)

Tod Thomson
Tod Thomson

Reputation: 4929

I have no idea why you would want to use a stored procedure for this.

Just write a simple console application to "call" the page. Then use a scheduled task to run the console application.

You could do that (of course) but that's not what the question was asking ;)

Jafin's answer is correct for the question.

The best answer (IMHO) would be to fix your production server (i.e. reconfigure the settings) so that it doesn't "scrap" your pages every 10 minutes.

Why use a jackhammer (console app or .net inside the database or whatever) when a regular one will do?

Upvotes: 0

Jafin
Jafin

Reputation: 4371

Code untested but something like this should work.

You'll probably also need TRUSTWORTHY SET.

ALTER DATABASE Foo SET TRUSTWORTHY ON;

Code:

public partial class WebProc
{
   [SqlFunction()]
   public static string WebQuery()
   {
      WebRequest request = HttpWebRequest.Create("http://www.google.com");
      WebResponse response = request.GetResponse();
      Stream dataStream = response.GetResponseStream();
      StreamReader reader = new StreamReader (dataStream);
      string responseFromServer = reader.ReadToEnd();
      return responseFromServer;
   }
}

Upvotes: 1

John Saunders
John Saunders

Reputation: 161773

I have no idea why you would want to use a stored procedure for this.

Just write a simple console application to "call" the page. Then use a scheduled task to run the console application.

Upvotes: 6

Related Questions