创建多次执行的应用服务调用
Create app service call executing multiple times
我正在 ASPNETZERO MVC JQuery .Net core 2.0 模板 上构建一个 SAAS 应用程序。
此问题是我之前发布的问题 的后续问题。所以我确实在我的应用程序服务中添加了方法,以不允许在我的“companyAddress”实体 table 中出现重复记录。在创建方法执行 insertAsync 之前,我调用了一个私有方法来检查 table 中的给定记录。这是应用程序服务代码。
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 中 运行 代码时,我可以看到我的私有方法找到了重复的条目并且 return 为真。但是执行代码跳来跳去,然后突然间它忽略了私有方法中的 true 并仍然执行插入。
我已经在客户端和服务器端调试了数十次。当我通过 C# 代码 运行 VS2017 调试器时,有时调试器似乎无法跟踪当前执行代码的位置。然后显示 NursingOps17EntityFrameworkCoreModule class 中的代码,并进入如下所示的方法。它不断满足 IF 现有连接检查的 else 条件。
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 创建方法。日志片段如下所示。
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 实体已正确更新。
然后我关闭公司编辑模式(单击取消按钮)并 return 到我的公司索引视图。然后我打开同一公司或不同公司的编辑公司模式。然后我重复相同的步骤来创建一个新地址。当 companyAddress 应用程序服务复制和触发 2 到 4 次时,就在编辑公司模式的“第二次”打开之后添加地址。它非常随机,有时会重复两次,有时会重复三次或更多次。
如果我从公司实体导航到应用程序中的任何其他页面,然后返回公司索引页面,然后打开编辑公司模式以将地址添加到任何公司,它就可以正常工作。很明显,当“第一次”访问公司编辑模式时,一切正常。当用户多次进入公司索引视图并重新打开编辑模式时,就会出现应用服务重复。
首先,这完全超出了 Asp.net 样板框架。而且在不接触项目的情况下,很难猜到问题出在哪里。但我的建议是;尝试在新方法中制作一个单一的保存方法和 insert/update 实体。检查记录的 Id 以了解它是插入还是保存操作。据我所知,您没有在客户端设置新插入记录的 ID。最后将 DuplicateCheck 方法合并到 CreateCompanyAddress 方法中,以了解编译器的内联优化是否有问题。
Despite all my efforts I cannot seem to locate the trigger that is causing these multiple calls to the companyAddress API service method.
根据你上一个问题的答案,你只在 save
中调用了 .off
而没有在 cancel
中调用。
将 .off
移动到 this.init = ...
。
this.init = function (modalManager) {
_modalManager = modalManager;
// ...
_modalManager.onClose(function () {
abp.event.off('addressCreated', addressCreated); // Turn off this handler
});
};
我正在 ASPNETZERO MVC JQuery .Net core 2.0 模板 上构建一个 SAAS 应用程序。
此问题是我之前发布的问题
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 中 运行 代码时,我可以看到我的私有方法找到了重复的条目并且 return 为真。但是执行代码跳来跳去,然后突然间它忽略了私有方法中的 true 并仍然执行插入。 我已经在客户端和服务器端调试了数十次。当我通过 C# 代码 运行 VS2017 调试器时,有时调试器似乎无法跟踪当前执行代码的位置。然后显示 NursingOps17EntityFrameworkCoreModule class 中的代码,并进入如下所示的方法。它不断满足 IF 现有连接检查的 else 条件。
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 创建方法。日志片段如下所示。
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 实体已正确更新。 然后我关闭公司编辑模式(单击取消按钮)并 return 到我的公司索引视图。然后我打开同一公司或不同公司的编辑公司模式。然后我重复相同的步骤来创建一个新地址。当 companyAddress 应用程序服务复制和触发 2 到 4 次时,就在编辑公司模式的“第二次”打开之后添加地址。它非常随机,有时会重复两次,有时会重复三次或更多次。 如果我从公司实体导航到应用程序中的任何其他页面,然后返回公司索引页面,然后打开编辑公司模式以将地址添加到任何公司,它就可以正常工作。很明显,当“第一次”访问公司编辑模式时,一切正常。当用户多次进入公司索引视图并重新打开编辑模式时,就会出现应用服务重复。
首先,这完全超出了 Asp.net 样板框架。而且在不接触项目的情况下,很难猜到问题出在哪里。但我的建议是;尝试在新方法中制作一个单一的保存方法和 insert/update 实体。检查记录的 Id 以了解它是插入还是保存操作。据我所知,您没有在客户端设置新插入记录的 ID。最后将 DuplicateCheck 方法合并到 CreateCompanyAddress 方法中,以了解编译器的内联优化是否有问题。
Despite all my efforts I cannot seem to locate the trigger that is causing these multiple calls to the companyAddress API service method.
根据你上一个问题的答案,你只在 save
中调用了 .off
而没有在 cancel
中调用。
将 .off
移动到 this.init = ...
。
this.init = function (modalManager) {
_modalManager = modalManager;
// ...
_modalManager.onClose(function () {
abp.event.off('addressCreated', addressCreated); // Turn off this handler
});
};