[英]How do I create Singleton instance for a library referenced from nuget
Task is to call a third party service/wrapper multiple times and get the response. 任务是多次调用第三方服务/包装并获得响应。 Below is the sample code and I am trying to figured out how to create singleton instance for the service. 下面是示例代码,我试图弄清楚如何为服务创建单例实例。
class Program
{
static void Main(string[] args)
{
List<string> trips = new List<string>();
trips.Add("ABC");
trips.Add("XYZ");
foreach (string s in trips) {
Test.TestMethod(s);
}
}
}
public static class Test
{
public static bool TestMethod(string trip)
{
BridgeApiClient bridgeApiClient = new BridgeApiClient("http://localhost/Service.svc", "username", "password");
TripRequest tr = new TripRequest();
tr.TripNumber = trip;
var response = bridgeApiClient.GetTrip(tr);
return true;
}
}
You can just declare a static
member variable and use that: 您可以只声明一个static
成员变量并使用它:
public static class Test
{
private static readonly BridgeApiClient bridgeApiClient = new BridgeApiClient("http://localhost/Service.svc", "username", "password");
public static bool TestMethod(string trip)
{
TripRequest tr = new TripRequest();
tr.TripNumber = trip;
var response = bridgeApiClient.GetTrip(tr);
return true;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.