Search code examples
c#asp.netexcelnpoi

Convert aspx.cs code behind into an executable file


I am a junior level developer and I have a situation where a previous developer wrote an aspx page, named with a guid, so as not to be accidentally accessed by a user. Its function is to call stored procedures to get a list of recipients, and populate a DataTable. Then use NPOI.dll to export the dt to an excel workbook and email it to the recipient list. There are no controls in the front end, it's all triggered on page load. This page is called once a week by setting this page as the default page on the servers browser, and setting a job on the server to start the browser. All of this actually worked well for quite a while, however, now the client would like to remove the page from the site and use an alternate method to generate and send the weekly report. Here's my question. Can I compile this code-behind into an .exe application that can be called by a server job? Or is there a better way to approach this? Any recommendations and/or guidance would be appreciated!I have searched and could find no relevant discussions.


Solution

  • You have to create a separate Console Application and move the code manually. It will involve copy/paste code from code behind to your Console app. Once your console app is done, then you can schedule the app to run weekly via Operating System Schedule tasks.


    As a starting point copy the code from Page_Load method to Main method in the console app, You will also need to add the references for required dlls like NPOI.dll. Once you are able to compile your console application successfully, then you can test it by sending out some test emails. That console app will give you an executable that you can schedule on a machine to execute ever week.

    See: Schedule a task