【问题标题】:ASP.NET Boilerplate: Unable to hit the service from index.jsASP.NET 样板:无法从 index.js 访问服务
【发布时间】:2018-09-16 14:24:27
【问题描述】:

我们正在尝试使用 .NET Core 多页应用程序模板实现自定义服务。尝试点击服务方法时,我们在 index.js 文件中收到错误消息。

在此处查找代码部分以供理解。对此的任何帮助都会对我们很有帮助。

public interface IMyTaskAppService : IApplicationService
{
    Task<List<string>> GetMyTasks(int input);
}

public class MyTaskAppService : IMyTaskAppService
{
    private readonly IMyTaskRepository _mytaskRepository;

    public MyTaskAppService(IMyTaskRepository mytaskRepository)
    {
        _mytaskRepository = mytaskRepository;
    }

    Task<List<string>> IMyTaskAppService.GetMyTasks(int input)
    {
        return _mytaskRepository.GetMyTasks(input);
    }

}

Index.Js

Index.Js Error (Lowercase)

Console Output Screenshot

【问题讨论】:

  • _mytaskService 是如何定义的?
  • 公共接口 IMyTaskAppService : IApplicationService { Task> GetMyTasks(int input); }
  • 公共类 MyTaskAppService : IMyTaskAppService { private readonly IMyTaskRepository _mytaskRepository;公共 MyTaskAppService(IMyTaskRepository mytaskRepository) { _mytaskRepository = mytaskRepository; } Task> IMyTaskAppService.GetMyTasks(int input) { return _mytaskRepository.GetMyTasks(input); } }
  • 那肯定不是 JavaScript。另外,编辑您的问题,而不是在 cmets 中发布代码。
  • @RSiva 如何在您的 JavaScript 中定义 _mytaskService

标签: aspnetboilerplate javascript aspnetboilerplate


【解决方案1】:
  1. MyTaskAppService 至少需要一个public 方法:

    public class MyTaskAppService : IMyTaskAppService
    {
        // ...
    
        // Task<List<string>> IMyTaskAppService.GetMyTasks(int input)
        public Task<List<string>> GetMyTasks(int input)
        {
            return null;
        }
    }
    

    您拥有的是“比 private 更受限制”的explicit implementation

  2. 服务和方法名称以 camelCase 表示Client Proxies

    myTask 使用大写的T

    var _myTaskService = abp.services.app.myTask;
    

    getMyTasks 使用小写的g

    _myTaskService.getMyTasks({
        taskID: taskID
    }).done(function (data) {
        alert(data);
    });
    

【讨论】:

  • 谢谢@aaron。两个都试过了。小写也抛出相同的错误。我在原帖中附上了错误截图(小写)。
  • 试试_mytaskService = abp.services.app.myTask;
  • 同样的问题仍然存在。
  • console.log(abp.services.app) 得到什么?
  • 我们正在接收帐户、配置、角色、租户、会话和用户对象。我的任务不见了。附上原始帖子的屏幕截图。
【解决方案2】:

添加ApplicationService 作为MyTaskAppService 类的基础。 查看我的代码;

public class MyTaskAppService : ApplicationService, IMyTaskAppService
{
    private readonly IMyTaskRepository _mytaskRepository;

    public MyTaskAppService(IMyTaskRepository mytaskRepository)
    {
        _mytaskRepository = mytaskRepository;
    }

    Task<List<string>> IMyTaskAppService.GetMyTasks(int input)
    {
        return _mytaskRepository.GetMyTasks(input);
    }    
}

TaskAppService

public interface IMyTaskAppService : IApplicationService
{
    Task<List<string>> GetMyTasks(int input);
}

查看SimpleTaskSystem的完整源代码

【讨论】:

  • 感谢@Alper Ebicoglu。我正在使用来自 github.com/aspnetboilerplate/aspnetboilerplate-samples/tree/… 的 StoredProcedureDemo 代码。此示例代码库中缺少 WebApi 项目。我正在尝试将 ASPNet 核心(多页 Web 应用程序)与 MVC 和存储过程一起使用。请帮忙。
  • 你试过我的建议了吗?
  • 是的。根据您的建议,我已将 ApplicationService 作为基础添加到 MyTaskAppService 类中,但问题仍然存在。我应该需要 WebApi 项目和 MyTaskSystemWebApiModule 吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多