api控制器没有引用业务逻辑?

时间:2018-05-22 13:01:18

标签: c# asp.net asp.net-mvc asp.net-web-api closedxml

以下代码是我在API控制器中编写的函数,用于调用我的业务逻辑:

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

我想调用的业务逻辑如下:

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

令我感到困惑的是HTTPGET函数没有连接到我试图调用的业务逻辑函数的引用。我错过了什么吗?我有连接业务逻辑文件所需的正确使用语句。我确实为我的业务添加了对API项目的引用。

修改

这两个函数的目的是通过使用closedXML构建的函数开始构建excel文件。通过我的业务逻辑函数的整个closedXML函数如下:

 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
}

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

这些线条似乎不正确

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

第一行调用异步函数,等待它返回并将结果存储在局部变量wb中。第二行再次调用此异步函数,但这次不等待结果,只是立即返回。

我怀疑你只是想要这个:

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

虽然目前尚不清楚如何从异步调用中受益,因为前台没有其他工作。