Create app service call executing multiple times

孤街浪徒 提交于 2020-01-04 05:27:08

问题


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;
    }

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);
                }
            });
        }
    }

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.

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

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.

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.


回答1:


Despite all my efforts I cannot seem to locate the trigger that is causing these multiple calls to the companyAddress API service method.

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

Move the .off into this.init = ....

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

    // ...

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



回答2:


First of all this is completely out of Asp.net Boilerplate framework. And it`s hard to guess what the problem is without touching the project. But my advice is; try to make one single save method and insert/update entity in the new method. Check the Id of the record to understand whether it's insert or save action. As far as I see you don't set the Id of newly inserted record in client-side. Lastly merge DuplicateCheck method into CreateCompanyAddress method, to understand if something goes wrong with the compiler's inline optimization.



来源:https://stackoverflow.com/questions/47915120/create-app-service-call-executing-multiple-times

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!