本文介绍了创建多次执行的应用服务调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 ASPNETZERO MVC JQuery .Net core 2.0模板上构建SAAS应用程序.本期是我在此处上发布的上一期的后续文章.因此,我确实在我的应用程序服务中添加了该方法,以不允许在"companyAddress"中添加重复记录.实体表.在create方法执行insertAsync之前,我调用一个私有方法来检查表中的给定记录.这是应用程序服务代码.

I'm building a SAAS app on the ASPNETZERO MVC JQuery .Net core 2.0 template.This issue is a follow-up to my earlier issue posted here. So I did add the method in my app service to NOT allow the duplicate record in my "companyAddress" entity table. Before the create method executes the insertAsync, I call a private method to check for that given record in my table. Here is the app service code.

       public async Task CreateCompanyAddress(CreateCompanyAddressInput input)
    {
        //Check for duplicate before inserting
        if (DuplicateCheck(input))
        {
            return;
        }
        else
        {
            //Get current address data
            var addr = await _addressRepository.GetAsync(input.AddressId);

            //Get current company data
            var comp = await _cmpRepository.GetAsync(input.CompanyId);

            input.Company = comp;
            input.Address = addr;
            var CA = input.MapTo<CompanyAddress>();

            await _cmpaddressRepository.InsertAsync(CA);
            Logger.Info("CompanyAddressService -  NEW entry detected. Company Id=" + input.CompanyId + " Address Id=" + input.AddressId);
        }
    }

     private bool DuplicateCheck(CreateCompanyAddressInput input)
    {
        var duplicate = _cmpaddressRepository.GetAll()
            .Where(C => C.Company.Id == input.CompanyId && C.Address.Id ==  input.AddressId);
        var total = duplicate.Count();
        if (total > 0)
        {
            Logger.Info("CompanyAddressService -  Duplicate entry detected. Company Id=" + input.CompanyId + " Address Id=" + input.AddressId);
            return true;
        }
        return false;
    }

当我在DEBUG中运行代码时,可以看到我的私有方法找到重复的条目并返回true.但是正在执行的代码跳来跳去,然后突然间它忽略了private方法中的true并仍然执行插入操作.我已经在客户端和服务器端调试了数十次.当我通过C#代码运行VS2017调试器时,有时调试器似乎无法跟踪当前执行代码的位置.然后,它显示NursingOps17EntityFrameworkCoreModule类中的代码,并进入下面显示的方法.它会继续击中IF现有连接检查的else条件.

When I run the code in DEBUG, I can see my private method finding the duplicate entry and returning true. But the executing code jumps around and then all of a sudden it ignores the true from the private method and still does the insert.I have debugged this dozens of times, on the client side and on the server side. When I run the VS2017 debugger through the C# code, there are occasions where the debugger seems to lose track of the where the current executing code is. Then it shows the code in the NursingOps17EntityFrameworkCoreModule class, and goes into the method shown below. It keeps hitting the else condition of the IF existing connection check.

       public override void PreInitialize()
    {
        if (!SkipDbContextRegistration)
        {
            Configuration.Modules.AbpEfCore().AddDbContext<NursingOps17DbContext>(options =>
            {
                if (options.ExistingConnection != null)
                {
                    NursingOps17DbContextConfigurer.Configure(options.DbContextOptions, options.ExistingConnection);
                }
                else
                {
                    NursingOps17DbContextConfigurer.Configure(options.DbContextOptions, options.ConnectionString);
                }
            });
        }
    }

看起来在API调用过程中连接丢失了吗?基于上述方法,我只是一个猜测.

So it looks like the connection is getting lost in the middle of the API call? Just a guess on my part, based on the above method being called.

当我查看MVC应用程序的日志文件时.我可以看到多次调用companyAddress的API创建方法.日志的片段如下所示.

When I look at the log file for the MVC app. I can see the API create method for companyAddress getting called multiple times. Snippet of the log is shown below.

INFO  2017-12-19 15:45:46,121 [9    ] ore.Mvc.Internal.ControllerActionInvoker - Executed action EXLNT.NursingOps17.NursingOps.AddressAppService.CreateAddress (EXLNT.NursingOps17.Application) in 18.1348ms
INFO  2017-12-19 15:45:46,122 [9    ] soft.AspNetCore.Hosting.Internal.WebHost - Request finished in 27.7701ms 200 application/json; charset=utf-8
INFO  2017-12-19 15:45:46,128 [4    ] soft.AspNetCore.Hosting.Internal.WebHost - Request starting HTTP/1.1 POST http://localhost:62114/api/services/app/CompanyAddress/CreateCompanyAddress application/json 31
INFO  2017-12-19 15:45:46,131 [9    ] soft.AspNetCore.Hosting.Internal.WebHost - Request starting HTTP/1.1 POST http://localhost:62114/api/services/app/CompanyAddress/CreateCompanyAddress application/json 31
INFO  2017-12-19 15:45:46,133 [10   ] soft.AspNetCore.Hosting.Internal.WebHost - Request starting HTTP/1.1 POST http://localhost:62114/api/services/app/CompanyAddress/CreateCompanyAddress application/json 31
INFO  2017-12-19 15:45:46,136 [4    ] tion.Cookies.CookieAuthenticationHandler - AuthenticationScheme: Identity.Application was successfully authenticated.
INFO  2017-12-19 15:45:46,138 [10   ] tion.Cookies.CookieAuthenticationHandler - AuthenticationScheme: Identity.Application was successfully authenticated.
INFO  2017-12-19 15:45:46,140 [9    ] tion.Cookies.CookieAuthenticationHandler - AuthenticationScheme: Identity.Application was successfully authenticated.
INFO  2017-12-19 15:45:46,146 [4    ] ore.Mvc.Internal.ControllerActionInvoker - Executing action method EXLNT.NursingOps17.NursingOps.CompanyAddressAppService.CreateCompanyAddress (EXLNT.NursingOps17.Application) with arguments (EXLNT.NursingOps17.NursingOps.Dto.CreateCompanyAddressInput) - ModelState is Valid
INFO  2017-12-19 15:45:48,709 [9    ] ore.Mvc.Internal.ControllerActionInvoker - Executing action method EXLNT.NursingOps17.NursingOps.CompanyAddressAppService.CreateCompanyAddress (EXLNT.NursingOps17.Application) with arguments (EXLNT.NursingOps17.NursingOps.Dto.CreateCompanyAddressInput) - ModelState is Valid
INFO  2017-12-19 15:45:49,657 [10   ] ore.Mvc.Internal.ControllerActionInvoker - Executing action method EXLNT.NursingOps17.NursingOps.CompanyAddressAppService.CreateCompanyAddress (EXLNT.NursingOps17.Application) with arguments (EXLNT.NursingOps17.NursingOps.Dto.CreateCompanyAddressInput) - ModelState is Valid
INFO  2017-12-19 15:47:12,504 [10   ] ps17.NursingOps.CompanyAddressAppService - CompanyAddressService -  NEW entry detected. Company Id=2 Address Id=3
INFO  2017-12-19 15:47:12,533 [51   ] ps17.NursingOps.CompanyAddressAppService - CompanyAddressService -  NEW entry detected. Company Id=2 Address Id=3
INFO  2017-12-19 15:47:12,533 [12   ] ps17.NursingOps.CompanyAddressAppService - CompanyAddressService -  NEW entry detected. Company Id=2 Address Id=3

尽管我付出了所有努力,但似乎无法找到导致对companyAddress API服务方法多次调用的触发器.我意识到仅凭这么多的信息,某人可能很难提供帮助.我是一名独立开发者,独自为客户开发应用程序.任何人都可以在此问题上寻求帮助或帮助,将非常有帮助.

Despite all my efforts I cannot seem to locate the trigger that is causing these multiple calls to the companyAddress API service method. I realize this maybe difficult for someone to help, with just this much information. I'm an independent devloper working alone on a app for a client. Any help or light anyone can shed on this issue would be very helpful.

这是该问题通常发生的时间.我打开公司的编辑公司模式.我单击创建地址按钮以打开地址创建模式.我这样做两次或三遍,以添加公司地址.这工作正常,companyAddress实体已正确更新.然后,我关闭公司编辑模式(单击取消按钮)并返回到我的公司索引视图.然后,我为同一公司或不同公司打开编辑公司模式.然后,我重复相同的步骤来创建一个新地址.就在此第二"秒之后.当companyAddress应用服务正在复制并触发2到4次时,打开编辑公司模式以添加地址.它非常随机,有时会重复两次,两次或多次.如果我从公司实体导航到应用程序中的任何其他页面,然后返回公司索引页面,然后打开编辑公司"模式以将地址添加到可以正常工作的任何公司.很明显,当第一"造访公司编辑模式时,一切正常.用户进入公司索引视图并多次重新打开编辑模式时,才发生应用程序服务重复.

Here is when the issue typically occurs.I open the edit company modal for a company. I click on create address button to open address create modal. I do this two or three times to add addresses for the company. This works just fine, the companyAddress entity is updated properly.I then close the company edit modal (cancel button click) and return to my company index view. Then I open the edit company modal for the same company or different company. I then repeat the same steps to create a new address. It is right after this "second" opening of the edit company modal to add addresses when the companyAddress app service is duplicating and triggering anywhere from 2 to 4 times. Its very random, sometimes it repeats twice other times three or more times.If I navigate away from the company entity to any other page in the application and then I come back to the company index page and then open the edit company modal to add addresses to any company it works just fine. So clearly when the "first" visit to the company edit modal occurs things work fine. It is when the user has been on the company index view and reopens the edit modal multiple times that the app service duplication occurs.

推荐答案

从上一个问题的答案中,您只能在save中呼叫.off,而不在cancel中呼叫.

From the answer on your previous question, you only call .off in save and not in cancel.

.off移至this.init = ....

this.init = function (modalManager) {
    _modalManager = modalManager;

    // ...

    _modalManager.onClose(function () {
        abp.event.off('addressCreated', addressCreated); // Turn off this handler
    });
};

这篇关于创建多次执行的应用服务调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 08:28