0
我目前在C#中的靜態字段存在問題,問題在於EasyPost實例化它們的ClientManger的方式,但是我認爲對靜態字段有更好理解的人可能會幫我。靜態字段 - EasyPost - ClientManager初始
我正在創建一個插件,允許多個用戶訪問EasyPost以追蹤包裹。
我已經寫了一個單元測試來測試多人在同一時間使用它的場景。
單元測試:
[TestMethod()]
public void TestMultiInit()
{
var Client2 = new cpEasyPost("123");
var Client = new cpEasyPost("123456qq785412");
var Shipment = Client.CreateShipment(
new EasyPost.Address
{
street1 = "417 MONTGOMERY ST",
street2 = "FLOOR 5",
city = "SAN FRANCISCO",
state = "CA",
zip = "94104",
country = "US",
company = "EasyPost"
},
new EasyPost.Address
{
street1 = "417 MONTGOMERY ST",
street2 = "FLOOR 5",
city = "SAN FRANCISCO",
state = "CA",
zip = "94104",
country = "US",
company = "EasyPost"
},
new EasyPost.Parcel
{
length = 20.2,
width = 10.9,
height = 5,
weight = 65.9
});
var Shipment2 = Client2.CreateShipment(
new EasyPost.Address
{
street1 = "417 MONTGOMERY ST",
street2 = "FLOOR 5",
city = "SAN FRANCISCO",
state = "CA",
zip = "94104",
country = "US",
company = "EasyPost"
},
new EasyPost.Address
{
street1 = "417 MONTGOMERY ST",
street2 = "FLOOR 5",
city = "SAN FRANCISCO",
state = "CA",
zip = "94104",
country = "US",
company = "EasyPost"
},
new EasyPost.Parcel
{
length = 20.2,
width = 10.9,
height = 5,
weight = 65.9
});
}
的問題是客戶端2有不正確的密鑰,所以如果我試圖創建一個裝運它應該然而失敗,因爲ClinetManager是靜態的被使用,因爲如果後來它的客戶端初始化。
這裏是我的構造函數:
public cpEasyPost(string secretKey)
{
SecretKey = secretKey;
//Original way of init Client
//ClientManager.SetCurrent(SecretKey);
//Create ClientManager
ClientManager.SetCurrent(() => new Client(new ClientConfiguration(SecretKey)));
}
這裏是我的方法:
public Shipment CreateShipment(Address AddressFrom, Address AddressTo, Parcel Parcel, CustomsInfo customs = null, string CustomReference = "", double? InsauranceAmount = null)
{
//Validate Adress
var isValidFrom = ValidateAddress(AddressFrom);
var isValidTo = ValidateAddress(AddressFrom);
if (!isValidFrom.isSuccess)
throw new Exception("Address From is not Valid");
if (!isValidTo.isSuccess)
throw new Exception("Address To is not Valid");
//Validate Pacrcel
var isValidParcel = ValidateParcel(Parcel);
if (!isValidFrom.isSuccess)
throw new Exception("Parcel is not Valid");
//Create Shipment
Shipment shipment = new Shipment()
{
reference = CustomReference,
to_address = AddressTo,
from_address = AddressFrom,
parcel = Parcel,
customs_info = customs
};
//ClientManager.SetCurrent(SecretKey); **
shipment.Create();
//Add Insurance
if (InsauranceAmount != null)
shipment.Insure(InsauranceAmount.Value);
return shipment;
}
無我有問題是ClinetManager是靜態的,這是一個鎖定的DDL,所以我可以」不要修改這個。在該方法中,我考慮在每次通話之前先安排經理,但這似乎並不是最好的解決方案,因爲它理論上可能會導致我用**標記該問題。
任何幫助將不勝感激。謝謝你adnvacne。