创建多次执行应用服务调用

时间:2017-12-20 22:13:42

标签: c# aspnetboilerplate

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

       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。但执行代码会跳转,然后突然忽略私有方法中的true并仍然执行插入操作。 我已经在客户端和服务器端调试了这几十次。当我通过C#代码运行VS2017调试器时,有时调试器似乎忘记了当前执行代码的位置。然后它显示了NursingOps17EntityFrameworkCoreModule类中的代码,并进入下面显示的方法。它不断触及IF现有连接检查的其他条件。

       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调用过程中丢失了?基于上面的方法,我只是猜测一下。

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

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服务方法的触发器。我意识到这可能很难有人帮助,只有这么多的信息。我是一个独立的开发人员,专门为客户开发应用程序。任何人可以帮助或解决这个问题都会有所帮助。

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

2 个答案:

答案 0 :(得分:2)

  

尽管付出了很多努力,我似乎​​无法找到导致这些多次调用companyAddress API服务方法的触发器。

根据上一个问题的答案,您只能在<#list entries as entry> (here I recover each promo printed by the template) ${entry_index} ---> the promo index that i have to put in the event of the promo </#list> 而不是.off中拨打save

cancel移至.off

this.init = ...

答案 1 :(得分:0)

首先,这完全脱离了Asp.net Boilerplate框架。如果不触及项目,很难猜出问题是什么。但我的建议是;尝试在新方法中创建一个单独的save方法并插入/更新实体。检查记录的ID以了解它是否插入或保存操作。据我所知,你没有在客户端设置新插入记录的Id。最后将DuplicateCheck方法合并到CreateCompanyAddress方法中,以了解编译器的内联优化是否出错。

相关问题