C# 静态字段-EasyPost-ClientManager Init
我目前在C#中涉及到静态字段时遇到了一个问题,问题在于EasyPost实例化其客户经理的方式。但是,我认为对静态字段有更好理解的人可能可以帮助我 我正在创建一个插件,允许多个用户访问EasyPost以跟踪包裹 我编写了一个单元测试来测试多人同时使用它的场景 单元测试:C# 静态字段-EasyPost-ClientManager Init,c#,static,static-libraries,static-variables,easypost,C#,Static,Static Libraries,Static Variables,Easypost,我目前在C#中涉及到静态字段时遇到了一个问题,问题在于EasyPost实例化其客户经理的方式。但是,我认为对静态字段有更好理解的人可能可以帮助我 我正在创建一个插件,允许多个用户访问EasyPost以跟踪包裹 我编写了一个单元测试来测试多人同时使用它的场景 单元测试: [TestMethod()] public void TestMultiInit() { var Client2 = new cpEasyPost("123"); var Client =
[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是静态的,所以它使用它的客户端init,因为if是更晚的
这是我的简历:
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,所以我不能修改它。在该方法中,我考虑在每次通话前设置经理,但这似乎不是最好的解决方案,因为从理论上讲,这仍然可能导致我用**标记的问题
任何帮助都将不胜感激。谢谢你的支持 最后,我只是将他们SDK中的代码重新编译成更符合我需要的代码。这是没有别的办法的