简体   繁体   中英

C# method not working when called via published WebAPI

I have simplified my method for testing purposes. It works fine when running locally in VS 2013 debug mode.

Here is my APIController:

    namespace Citizen.Hub.WebAPI.Controllers
{
    public class ReportsController : ApiController
    {
        [HttpGet]
        public void RunAttendanceByYear(string school, string fromdate, string todate)
        {
            var runreport = new AttendanceReports();

            runreport.WebAPITest(school, fromdate, todate);
        }
    }
}

Here is my simplified method for testing:

public void WebAPITest(string school, string fromdate, string todate)
        {
            //Create new instance of Excel, Workbook and Worksheet
            Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
            Workbook xlWorkbook = xlApp.Workbooks.Add();
            Sheets xlSheets = xlWorkbook.Sheets as Sheets;

            //Create new worksheet for school
            Worksheet xlNewSheet = (Worksheet)xlSheets.Add(xlSheets[1], Type.Missing, Type.Missing, Type.Missing);

            //Worksheet headers
            xlNewSheet.Cells[1, "A"] = "School";
            xlNewSheet.Cells[1, "B"] = "From";
            xlNewSheet.Cells[1, "C"] = "To";

            //Populate sheet with data
            xlNewSheet.Cells[2, "A"] = school;
            xlNewSheet.Cells[2, "B"] = fromdate;
            xlNewSheet.Cells[2, "C"] = todate;

            //Define filename and file path
            string fileName = string.Format(@"{0}\Test.xlsx", Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory));

            //Save this data as a file
            xlWorkbook.SaveAs(fileName);

            //Quit Excel application
            xlApp.Quit();

            //Release COM objects
            if (xlApp != null)
                System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);

            if (xlWorkbook != null)
                System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkbook);

            xlApp = null;
            xlWorkbook = null;
            GC.Collect();
        }

Works fine when ran in debug mode and outputs "Test.xlsx" to the desktop.

When I publish the WebAPI locally and assign it port number 1111, it just doesn't want to work. I believe IIS is configured correctly because when I browse to it I get the default VS WebAPI home page.

My call is: http://localhost:1111/api/reports?school=TestSchool&fromdate=2013-09-01&todate=2014-02-07

I get an 'Aborted' result in IE and Response is HTTP/1.1 204 No Content

Any advice? Thanks.

RouteConfig:

public class RouteConfig 
{ 
    public static void RegisterRoutes(RouteCollection routes) 
    { 
         routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 
         routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", 
         defaults: new {controller = "Home", action = "Index", id =UrlParameter.Optional});
    } 
}

The issue was with IIS and permissions. I basically followed the steps in this link here: http://www.bloing.net/2011/01/how-to-make-iis7-play-nice-with-office-interop/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM