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.
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.
- public ActionResult CreateTextFileWithDate() {
- WriteToFile(@ "C:\temp", "testsamplefile.txt");
- return View();
- }
- static void WriteToFile(string directory, string name) {
- string filename = String.Format("{0:yyyy-MM-dd}__{1}", DateTime.Now, name);
- string path = Path.Combine(directory, filename);
- using(StreamWriter sw = System.IO.File.CreateText(path)) {
- sw.WriteLine("This text file is created on given date and time: " + DateTime.Now);
- }
- }
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.
If you see the file text, it should be like this. On every run, you will see the current date and time.
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.
Give a job name in the General tab.
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"
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.
Put a breakpoint in your code and run it. Now, click on "Start Job at Step..."
Once you run the job code breakpoint should be enabled, so you can debug your code.
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.
Conclusion
In this article, we have learned how to hit MVC endpoint through SQL server job and how to schedule a job.