How To Hit MVC Endpoint Using SQL Job

In this article, you will learn how to hit MVC endpoint from SQL Job and schedule a SQL job to run periodically.

In this article, I am going to use curl tools to hit the MVC endpoint. You can install curl tools from here and browse the location in program files to store.
 
https://curl.haxx.se/download.html
 

Why curl?

 
Curl is a tool to transfer data from or to a server, using one of the supported protocols (DICT, FILE, FTP, FTPS, GOPHER, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMB, SMBS, SMTP, SMTPS, TELNET and TFTP). The command is designed to work without user interaction.
 
Curl offers a busload of useful tricks like proxy support, user authentication, FTP upload, HTTP post, SSL connections, cookies, file transfer resume, Metalink, and more.
 

What is SQL Job?

 
A job is a specified series of actions that SQL Server Agent performs. Use jobs to define an administrative task that can be run one or more times and monitored for success or failure. A job can run on one local server or on multiple remote servers.
 
How To Hit MVC Endpoint Using SQL Job
 

Getting Started

 
First of all, let’s create an MVC endpoint and write some code so we can make sure our endpoint is working. For this sample, I have an endpoint which creates a new text file with some text and date and time.
  1. public ActionResult CreateTextFileWithDate() {  
  2.     WriteToFile(@ "C:\temp""testsamplefile.txt");  
  3.     return View();  
  4. }  
  5. static void WriteToFile(string directory, string name) {  
  6.     string filename = String.Format("{0:yyyy-MM-dd}__{1}", DateTime.Now, name);  
  7.     string path = Path.Combine(directory, filename);  
  8.     using(StreamWriter sw = System.IO.File.CreateText(path)) {  
  9.         sw.WriteLine("This text file is created on given date and time: " + DateTime.Now);  
  10.     }  
  11. }  
You can test code by hitting endpoint using the browser URL.
 
http://localhost:59815/home/CreateTextFileWithDate
 
As output, there is one text file created in the temp folder in C drive.
 
How To Hit MVC Endpoint Using SQL Job
 
If you see the file text, it should be like this. On every run, you will see the current date and time.
 
How To Hit MVC Endpoint Using SQL Job
 
Now, our endpoint is working and we have a URL. Let’s create a new SQL job. Right-click on Jobs under SQL Server Agent.
 
How To Hit MVC Endpoint Using SQL Job
 
Give a job name in the General tab.
 
How To Hit MVC Endpoint Using SQL Job
 
Go to steps tab and hit the new button, provide step and select Operating system in Type drop-down and in command give curl.exe path with commands and MVC endpoint.
 
"C:\Program Files (x86)\Curl\bin\curl.exe" --ntlm -u : -G "http://localhost:59815/home/CreateTextFileWithDate"
 
How To Hit MVC Endpoint Using SQL Job
 
The next step is to schedule this job to run. Go to the Schedule tab and click on the New button. I am scheduling this job to run every minute.
 
How To Hit MVC Endpoint Using SQL Job
 
Put a breakpoint in your code and run it. Now, click on "Start Job at Step..."
 
How To Hit MVC Endpoint Using SQL Job
 
Once you run the job code breakpoint should be enabled, so you can debug your code.
 
How To Hit MVC Endpoint Using SQL Job
 
As you can see new text file is created in the temp folder which has the latest date and time. To test more keep checking on every minute because your job is running by making sure MVC application should be in the running mode because you are hitting local endpoint with local SQL server.
 
How To Hit MVC Endpoint Using SQL Job
 

Conclusion

 
In this article, we have learned how to hit MVC endpoint through SQL server job and how to schedule a job.