我正在使用一个多线程的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)