静态字段 - EasyPost - ClientManager Init
Static Fields - EasyPost - ClientManager Init
我目前在 C# 中遇到静态字段问题,问题在于 EasyPost 实例化其 ClientManger 的方式,但是,我认为对静态字段有更好理解的人可能会帮助我。
我正在创建一个允许多个用户访问 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
});
}
问题是 Client2 的密钥不正确,所以如果我尝试用它创建发货应该会失败,但是因为 ClinetManager 是静态的,所以使用了它的 Client init,因为如果稍后。
这是我的负责人:
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。
最后,我只是将他们的 SDK 中的代码重建为更符合我需求的代码。没有其他办法解决这个问题。
我目前在 C# 中遇到静态字段问题,问题在于 EasyPost 实例化其 ClientManger 的方式,但是,我认为对静态字段有更好理解的人可能会帮助我。
我正在创建一个允许多个用户访问 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
});
}
问题是 Client2 的密钥不正确,所以如果我尝试用它创建发货应该会失败,但是因为 ClinetManager 是静态的,所以使用了它的 Client init,因为如果稍后。
这是我的负责人:
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。
最后,我只是将他们的 SDK 中的代码重建为更符合我需求的代码。没有其他办法解决这个问题。