2017-02-23 1 views
0

Ich habe derzeit ein Problem, wenn es um statische Felder in C# geht, das Problem liegt in der Art, wie EasyPost ihren ClientManger instanziiert, aber ich denke jemand mit einem besseren Verständnis in Bezug auf statische Felder könnte um mir zu helfen.Statische Felder - EasyPost - ClientManager Init

Ich erstelle ein Plugin, mit dem mehrere Benutzer auf EasyPost zugreifen können, um Pakete zu verfolgen.

Ich habe einen Komponententest geschrieben, um das Szenario zu testen, in dem mehrere Personen es gleichzeitig verwenden.

Einheit Test:

[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 
      }); 


    } 

Das Problem ist Client2 falsche Taste hat also, wenn ich versuche, eine Sendung erstellen damit sollte jedoch scheitern, weil der ClinetManager statisch sein wird, verwendet Client-init davon, denn wenn später war.

Hier ist meine Ctor:

public cpEasyPost(string secretKey) 
    {    
     SecretKey = secretKey; 
     //Original way of init Client 
     //ClientManager.SetCurrent(SecretKey); 

     //Create ClientManager 
     ClientManager.SetCurrent(() => new Client(new ClientConfiguration(SecretKey))); 
    } 

Und hier ist meine Methode:

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; 

    } 

das No Problem, das ich habe ist, dass die ClinetManager statisch ist, ist dies ein gesperrter DDL so kann ich‘ t das ändern. Bei der Methode habe ich erwogen, den Manager vor jedem Aufruf zu setzen, aber das scheint nicht die beste Lösung zu sein, da es theoretisch noch zu Problemen führen könnte, die ich mit ** markiert habe.

Jede Hilfe würde sehr geschätzt werden. Vielen Dank in adnvacne.

Antwort

0

Am Ende, ich habe nur den Code von ihrem SDK in etwas, das meine Bedürfnisse besser angepasst. Es gibt keinen anderen Weg.

Verwandte Themen