小编Anu*_*rma的帖子

多线程c#应用程序中的懒惰单例

我正在使用一个多线程的c#应用程序,它正在使用WCF Web服务.与webservice的连接将具有我们可以定义的特定超时,然后它将关闭.我希望使用singleton类存储与Web服务的连接.我试图得到如下实例:

CLazySingleton ins = CLazySingleton.Instance;
string connection = CLazySingleton.abc;
Run Code Online (Sandbox Code Playgroud)

下面是单例类的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LazySingleton
{
    public class CLazySingleton
    {
        private static readonly Lazy<CLazySingleton> _instance
            = new Lazy<CLazySingleton>(() => new CLazySingleton());
        private static readonly object ThreadLock = new object();
        public static string abc;  
        //I will use the service connection object in place of 'abc' in the application
        //assume that 'abc' is storing the connection object    

        private CLazySingleton()
        { }

        public static CLazySingleton Instance
        { …
Run Code Online (Sandbox Code Playgroud)

.net c# wcf singleton multithreading

12
推荐指数
3
解决办法
2万
查看次数

标签 统计

.net ×1

c# ×1

multithreading ×1

singleton ×1

wcf ×1