简体   繁体   English

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

[英]No reference to business logic from api controller?

The following code is a function that I wrote in my API controller to call my business logic: 以下代码是我在API控制器中编写的用于调用业务逻辑的函数:

    [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. 使我感到困惑的是, HTTPGET函数没有与我要调用的业务逻辑函数的关联引用。 Am I missing something with this? 我有什么想念的吗? I have the proper using statement needed to connect the business logic file. 我有连接业务逻辑文件所需的正确using语句。 And I did add a reference to the API project as well for my business. 我也为我的业务添加了对API项目的引用。


Edit 编辑

The purpose of these two functions were to start the build of an excel file through functions built with closedXML. 这两个函数的目的是通过使用closeXML构建的函数开始构建excel文件。 The entire closedXML function through my Business logic function is as follow: 通过我的业务逻辑功能,整个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
}

Thanks for any help 谢谢你的帮助

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 . 第一行调用async函数,一直等到它返回并将结果存储在局部变量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. 尽管尚不清楚它将如何从异步调用中受益,但因为在前台没有其他工作要做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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