我是否需要在ASP.NET Core中锁定单例? [英] Do I need to lock singleton in ASP.NET Core?

查看:88
本文介绍了我是否需要在ASP.NET Core中锁定单例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

public class RouteSingleton
{
    private IDictionary<string, string> _dealCatLinks;
    private IDictionary<string, string> _sectionLinks;
    private IDictionary<string, string> _categoryLinks;
    private IDictionary<string, string> _materials;
    private IDictionary<string, string> _vendors;
    public RouteSingleton(IDealService dealService
        , ICategoryService categoryService
        , IVendorService vendorService)
    {


        this._dealCatLinks = dealService.GetDealCatLinks("PLV").Distinct().ToDictionary(x => x, x => x);
        this._sectionLinks = categoryService.GetSectionLinks("PLV").Distinct().ToDictionary(x => x, x => x);
        this._categoryLinks = categoryService.GetMainCategoryLinks("PLV")
            .Where(x => !_sectionLinks.ContainsKey(x)).Distinct().ToDictionary(x => x, x => x);
        this._vendors = _vendorService.GetVendorLinks("PFB").Distinct().ToDictionary(x => x, x => x);

    }

    public bool IsDealCategory(string slug)
    {
        return _dealCatLinks.ContainsKey(slug);
    }

    public bool IsSectionUrl(string slug)
    {
        return _sectionLinks.ContainsKey(slug);
    }

    public bool IsCategory(string slug)
    {
        return _categoryLinks.ContainsKey(slug);
    }       

    public bool IsVendor(string slug)
    {
        return _vendors.ContainsKey(slug);
    }
}

这是我在startup.cs中注册的方式:

Here is how I register in startup.cs:

services.AddSingleton<RouteSingleton, RouteSingleton>();

我在route constraints中使用singleton就像这样:

routes.MapRoute("category", "{slug}", defaults: new { controller = "Category", action = "Index" }, constraints: new { slug = new CategoryConstraint(app.ApplicationServices.GetRequiredService<RouteSingleton>()) });

  1. 我想知道我是否需要在RouteSingleton.cs中使用lock threads,否则我的代码在应用程序启动时对很多用户来说都可以正常工作吗?
  2. 如果我需要锁定您可以向我建议的方式?
  3. 如果我不这样做会怎样?
  1. I wonder do I need to lock threads in my RouteSingleton.cs or my code will work fine under lots of users on application start?
  2. If I need to lock what way you can suggest to me?
  3. What will happen if I don't?

推荐答案

不,您不需要锁定任何内容.它是一个单例,只会被构造一次,并且您在多个线程中同时使用私有字典的唯一操作是同时调用ContainsKey,这应该非常安全,因为在调用<时,没有其他东西可以修改字典了. c5>.

No, you don't need to lock anything. It is a singleton and will only be constructed once, and the only thing you are doing with your private dictionaries in multiple threads simultaneously is calling ContainsKey, which should be quite safe since nothing else can be modifying the dictionary while you are calling ContainsKey.

但是,如果要在构造函数之后修改这些字典,那就完全不一样了-您要么必须使用lock/mutex/etc.以保护对它们的访问或使用线程安全字典,例如ConcurrentDictionary.如目前所写,您应该没事.

However, if you were modifying those dictionaries after the constructor, it would be an entirely different story-- you would either have to use a lock/mutex/etc. to protect access to them or use a thread safe dictionary, such as ConcurrentDictionary. As it is currently written, you should be fine.

这篇关于我是否需要在ASP.NET Core中锁定单例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆