[英]How do I create Singleton instance for a library referenced from nuget
任務是多次調用第三方服務/包裝並獲得響應。 下面是示例代碼,我試圖弄清楚如何為服務創建單例實例。
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;
}
}
您可以只聲明一個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.