【问题标题】:Preventing a class method from executing more than once at a time防止类方法一次执行多次
【发布时间】:2015-06-16 01:10:54
【问题描述】:

我有一个 asp.net mvc 控制器,它在执行时会调用服务类中的方法来打开文件并开始将记录导入数据库。我想以某种方式限制此方法或类,以便无法创建另一个实例并防止该方法再次被调用,直到它完成运行。我已经阅读了单例模式,但我不确定它是否可以实现,因为该类是通过依赖注入实例化的。

依赖注入由 Autofac 处理

服务类:

namespace Nop.Plugin.Misc.CAImport.Services
{    
    public class CAImportService: ICAImportService
    {
        #region Fields
        private IProductService _productService;
        private ICategoryService _categoryService;
        private IManufacturerService _manufacturerService;

        #endregion

        #region Constructor
        public CAImportService(IProductService productService, 
            ICategoryService categoryService,
            IManufacturerService manufacturerService)
        {
            this._productService = productService;
            this._categoryService = categoryService;
            this._manufacturerService = manufacturerService;
        }
        #endregion

        #region Methods
        /// <summary>
        /// Import products from tab delimited file
        /// </summary>
        /// <param name="fileName">String</param>
        public virtual void ImportProducts(string fileName, bool deleteProducts)
        {
            //Do stuff to import products

        }

        #endregion
    }
}

控制器类:

namespace Nop.Plugin.Misc.CAImport.Controllers
{
    public class MiscCAImportController: BasePluginController
    {
        private ICAImportService _caImportService;

        public MiscCAImportController(ICAImportService caImportService)
        {
            this._caImportService = caImportService;
        }

        [AdminAuthorize]
        public ActionResult ImportProducts()
        {
            return View("~/plugins/misc.caimport/views/misccaimport/ImportProducts.cshtml");
        }

        [HttpPost]
        [AdminAuthorize]
        public ActionResult ImportProducts(ImportProductsModel model)
        {
            string fileName = Server.MapPath(model.Location);

            _caImportService.ImportProducts(fileName, model.DeleteProducts);                    

            return View("~/plugins/misc.caimport/views/misccaimport/ImportProducts.cshtml", model);
        }
    }
}

【问题讨论】:

  • 拥有一个单例对象不会阻止该对象的实例方法同时运行多次。即使对象只有一个实例,不同的线程也可以同时调用该方法。您需要lock 或一些类似的机制,而不是单例模式。或者,更有可能的是,您的设计需要重新考虑。

标签: c# asp.net-mvc dependency-injection singleton


【解决方案1】:

下面的代码在您的服务中添加了一个锁,以防止其他线程同时执行包含的语句。

public class CAImportService: ICAImportService
{

    // OTHER METHODS AND SECTIONS REMOVED FOR CLARITY

    // Define static object for the lock
    public static readonly object _lock = new object();

    /// <summary>
    /// Import products from tab delimited file
    /// </summary>
    /// <param name="fileName">String</param>
    public virtual void ImportProducts(string fileName, bool deleteProducts)
    {
        lock (_lock)
        {
            //Do stuff to import products
        }
    }
}

【讨论】:

  • 谢谢!正是我需要的
猜你喜欢
  • 2013-03-11
  • 2018-02-13
  • 1970-01-01
  • 2010-09-30
  • 2012-12-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多