1

The following code is a function that I wrote in my API controller to call my business logic:

    [HttpGet]
    [Route("api/file/{id}")]
    public async Task<HttpResponseMessage> GetSysMaintExcelFile(int id)
    {
        var wb = await rbs.GetSystemMaintenanceExcelFile(id);
        return CreateFileResponse(rbs.GetSystemMaintenanceExcelFile(id));
    }

The business logic that I wanted to call is as follows:

public async Task<byte[]> GetSystemMaintenanceExcelFile(int id)
{
    Systems sysRec = await dataAccess.GetSystemById(id);
    return BuildSysMaintExcelFile(sysRec);
}

What is confusing me is on the HTTPGET function does not have a connected reference to the business logic function I am trying to call. Am I missing something with this? I have the proper using statement needed to connect the business logic file. And I did add a reference to the API project as well for my business.


Edit

The purpose of these two functions were to start the build of an excel file through functions built with closedXML. The entire closedXML function through my Business logic function is as follow:

 public class ReportBS : IReportsBS
{
    private IAppDataAccess dataAccess;
    private IAuthManager authManager;

    #region Constructor
    public ReportBS(IAppDataAccess da, IAuthManager am)
    {
        this.dataAccess = da;
        this.authManager = am;
    }
    #endregion

    #region System Maintenance Reports
    public async Task<byte[]> GetSystemMaintenanceExcelFile(int id)
    {
        Systems sysRec = await dataAccess.GetSystemById(id);
        return BuildSysMaintExcelFile(sysRec);
    }

    public byte[] BuildSysMaintExcelFile(Systems dataRec)
    {
        //Creating the workbook
        const int dataRowStart = 3;
        string templateName = "~/App_Data/SystemMaintenance_Template.xlsx";
        var workbook = new XLWorkbook(templateName);
        var worksheet = workbook.Worksheet(1);
        string folderPath = @"~\Downloads\";
        if (!Directory.Exists(folderPath))
        {
            Directory.CreateDirectory(folderPath);
        }
        string fileName = templateName + folderPath + "SystemMaintenance_" + DateTime.Now.ToString("yyyyMMddHHmm") + ".xlsx";
        var dt = BuildSysDetailLoaExcelTable(dataRec, fileName);
        worksheet.Cell(dataRowStart, 3).InsertData(dt.AsEnumerable());
        workbook.SaveAs(fileName);

        using (var ms = new MemoryStream())
        {
            workbook.SaveAs(fileName);
            return ms.ToArray();
        }
    }

    //Building the tables
    private DataTable BuildSysDetailLoaExcelTable(Systems record, string fileName)
    {
        DataTable dt = new DataTable(fileName);

        dt.Columns.Add("systemName", typeof(string));
        dt.Columns.Add("isActive", typeof(bool));
        dt.Columns.Add("localIAO", typeof(bool));
        dt.Columns.Add("localStaff", typeof(bool));
        dt.Columns.Add("informationOfficerRequired", typeof(bool));
        dt.Columns.Add("iaoRequired", typeof(bool));
        dt.Columns.Add("IAOs", typeof(string));
        dt.Columns.Add("StaffProcessors", typeof(string));
        dt.Columns.Add("StaffRevalidators", typeof(string));

        var dr = dt.NewRow();
        dr[0] = record.systemName;
        dr[1] = record.isActive;
        dr[2] = record.localIAO;
        dr[3] = record.localStaff;
        dr[4] = record.informationOfficerRequired;
        dr[5] = record.iaoRequired;
        dr[6] = record.IAOs;
        dr[9] = record.StaffProcessors;
        dr[11] = record.StaffRevalidators;

        return dt;

    }

    #endregion
}

Thanks for any help

gwatson117
  • 32
  • 6
  • do you get any error? what is that? – Rahul May 22 '18 at 13:03
  • Well, the error is that I cannot download, or at least start to build my excel file function built through closedXML. I will edit the post to include any information used – gwatson117 May 22 '18 at 13:06
  • To be more specific @Rahul when I go directly to the uri from the API, it tries to build a blank JSON file and hits zero of my breakpoints that I have set in my app when I debug it – gwatson117 May 22 '18 at 13:13

1 Answers1

0

These lines don't seem to be correct

    var wb = await rbs.GetSystemMaintenanceExcelFile(id);
    return CreateFileResponse(rbs.GetSystemMaintenanceExcelFile(id));

First line calls the async function, waits till it returns and stores the results in the local variable wb. Second line calls this async function again, but this time does not wait for the result, and just returns immediately.

I suspect you just wanted this:

    var wbTask = rbs.GetSystemMaintenanceExcelFile(id);
    var wb = await wbTask;
    return CreateFileResponse(wb);

Although it is not clear how it is going to benefit from asynchronous call, because there is no other work done in foreground.

Andrei
  • 55,890
  • 9
  • 87
  • 108
  • So when I add the function you suggested @Andrei, I get an error on the `Return`, specifically on the `(wb)` The error is as follows: Argument 1: cannot convert from byte[] to System.Threading.Tasks.Task I am not sure what this means, or how to go about fixing this – gwatson117 May 22 '18 at 13:39
  • Would adding a `Throw New Exception` take care of the conversion? – gwatson117 May 22 '18 at 13:41
  • @gwatson117, i don't have any problems compiling that code. Are you sure you are not missing `await` anywhere? – Andrei May 22 '18 at 13:50
  • I am sure, for now. I am going to dive into this because I am getting the error to convert message. @Andrei – gwatson117 May 22 '18 at 14:12
  • I am not missing an `await` tag anywhere, and still get that error – gwatson117 May 22 '18 at 17:10
  • 1
    @gwatson117 Does it work if you strip out all of the async stuff? This kind of smells like it's returning too early. – Devin Goble May 23 '18 at 15:18
  • @kettch wouldn't that be opposite of what async does? That it only returns the area of data that you select without reloading the app? – gwatson117 May 23 '18 at 16:18
  • 1
    @gwatson117 I'm just thinking of it in terms of side effects. I've found myself chasing my tail only to find out that I messed up something somewhere else, and async was masking it. – Devin Goble May 23 '18 at 16:54
  • I see what you are saying, I guess my only hesitation is the length of time that it will take this app to load the file. But to consider it to actually fetch and build the file, I may be okay with it. @kettch I will run tests on it to see if that can be the case – gwatson117 May 23 '18 at 18:20
  • @gwatson117 You can certainly add it back in later. Right now, you want to strip as many variables away as possible. – Devin Goble May 23 '18 at 18:52